Hubot Slack attachment fields - coffeescript

Basically,
this is what I want to achieve in Slack using Hubot. I've tried using
attachment =
fields: [
{
title: "User info"
value: json.user
short: false
}
]
But this doesn't work.
Does someone have an example of how I can make this work?
Thanks in advance ^^

Solved it by using
$attachments = [
'text' => "Active codebases: (total = $total)",
'attachments' => [
[
'color' => '#3333ff',
'fields' => [
]
]
]
];
And then inserted data using
$items = $codebases;
foreach ($items as $item)
{
if(LinkedUser::where('codebase_id', $item->id)->get() !== null) {
$linkedusers = LinkedUser::where('codebase_id', $item->id)->get();
$userlist = "";
$i = 0;
$len = count($linkedusers);
foreach ($linkedusers as $linkeduser)
{
if ($i == $len - 1) {
$userlist .= $linkeduser->user_name;
} else {
$userlist .= $linkeduser->user_name . ",\n";
}
$i++;
}
$a = [
'title' => $item->name,
'value' => $userlist,
'short' => true
];
$attachments['attachments'][0]['fields'][] = $a;
}
}

Related

Codeigniter 4 Rest API - 301 Moved Permanently

I have Codeigniter 4 web app that run REST API with firebase/php-jwt on Laragon 5.0.0210523 environment that run Apache-2.4.47, PHP-8.1.7, and MongoDB-4.0.28. I followed a tutorial and it works fine both server REST API and it REST client. After day work, i stop laragon server. In the next day i try run REST API server then tried then run the client but it failed and gave 301 moved permanently error, but i still can access it from postman.
REST API server side
composer.json
***
"require": {
"php": "^7.4 || ^8.0",
"codeigniter4/framework": "^4.0",
"mongodb/mongodb": "^1.12",
"firebase/php-jwt": "^6.3"
},
***
.env file
***
JWT_SECRET_KEY = SomeThing$089
JWT_TIME_TO_LIVE = 3600
app.baseURL = 'http://ci4-api.localhost'
***
Route.php
***
$routes->get('/', 'Home::index');
$routes->resource('api/users');
$routes->post('api/auth', [\App\Controllers\Api\Auth::class, 'index']);
***
JWT_Helper.php
use App\Models\ModelUsers;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
/**
* #throws Exception
*/
function getJWT($authHeader)
{
if (is_null($authHeader)){
throw new Exception("Authentication JWT failed");
}
return explode(" ", $authHeader)[1];
}
function validateJWT($encodedToken)
{
$key = getenv('JWT_SECRET_KEY');
$decodedToken = JWT::decode($encodedToken, new Key($key, 'HS256'));
$modelUsers = new ModelUsers();
$modelUsers->get_email($decodedToken->email);
}
function createJWT($email): string
{
$timeRequest = time();
$timeToken = getenv('JWT_TIME_TO_LIVE');
$timeExpired = $timeRequest + $timeToken;
$payload = [
'email' => $email,
'iat' => $timeRequest,
'exp' => $timeExpired,
];
return JWT::encode($payload, getenv('JWT_SECRET_KEY'), 'HS256');
}
FilterJWT.php
namespace App\Filters;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Config\Services;
use Exception;
class FilterJWT implements FilterInterface
{
use ResponseTrait;
public function before(RequestInterface $request, $arguments = null)
{
$header = $request->getServer('HTTP_AUTHORIZATION');
try {
helper('jwt');
$encodedToken = getJWT($header);
validateJWT($encodedToken);
return $request;
} catch (Exception $ex) {
return Services::response()->setJSON(
[
'error' => $ex->getMessage(),
]
)->setStatusCode(ResponseInterface::HTTP_UNAUTHORIZED);
}
}
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
// TODO: Implement after() method.
}
}
Filters.php
***
public $aliases = [
'csrf' => CSRF::class,
'toolbar' => DebugToolbar::class,
'honeypot' => Honeypot::class,
'invalidchars' => InvalidChars::class,
'secureheaders' => SecureHeaders::class,
'auth' => FilterJWT::class,
];
public $filters = [
'auth' => [
'before' => [
'api/users/*',
'api/users'
]
]
];
***
ModelUsers.php
namespace App\Models;
use App\Libraries\MongoDb;
class ModelUsers
{
private $database = 'ci4_api';
private $collection = 'user';
private $conn;
function __construct()
{
$mongodb = new MongoDb();
$this->conn = $mongodb->getConn();
}
function get_user_list() {
try {
$filter = [];
$query = new \MongoDB\Driver\Query($filter);
$result = $this->conn->executeQuery($this->database. '.' . $this->collection, $query);
return $result->toArray();
} catch (\MongoDB\Driver\Exception\RuntimeException $ex) {
show_error('Error while fetching users: ' . $ex->getMessage(), 500);
}
}
***
Auth.php
namespace App\Controllers\Api;
use App\Controllers\BaseController;
use App\Models\ModelUsers;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Validation\Validation;
use Config\Services;
class Auth extends BaseController
{
use ResponseTrait;
private ModelUsers $model;
private Validation $validation;
function __construct()
{
$this->model = new ModelUsers();
$this->validation = Services::validation();
}
public function index()
{
$email = $this->request->getVar('email');
$password = $this->request->getVar('password');
$password_hash = password_hash($password, PASSWORD_DEFAULT);
$data1 = [
'email' => $email,
'password' => $password
];
if (!$this->validation->run($data1, 'login')) {
$errors = $this->validation->getErrors();
$response = [
'status' => 201,
'error' => null,
'messages' => [
'errors' => [
$errors
]
],
];
return $this->respond($response);
}
$data1 = $this->model->get_email($email);
//return $this->respond($data1, 200);
if (!$data1) {
$response = [
'status' => 201,
'error' => null,
'messages' => [
'error' => 'Data user atau password tidak ada1'
],
];
return $this->respond($response, 200);
}
$password_user = $data1->password;
if (password_verify($password_hash, $password_user) != 0){
$response = [
'status' => 201,
'error' => null,
'messages' => [
'error' => 'Data user atau password tidak ada2'
],
];
return $this->respond($response, 200);
}
helper('jwt');
$response = [
'message' => 'Auth berhasil dilakukan',
'data' => $data1,
'access_token' => createJWT($email)
];
return $this->respond($response, 200);
}
***
users.php
namespace App\Controllers\Api;
use App\Controllers\BaseController;
use App\Models\ModelUsers;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\HTTP\Response;
use CodeIgniter\Validation\Validation;
use Config\Services;
class Users extends BaseController
{
use ResponseTrait;
private ModelUsers $model;
private Validation $validation;
function __construct()
{
$this->model = new ModelUsers();
$this->validation = Services::validation();
}
public function index(): Response
{
$data = $this->model->get_user_list();
$count = count($data);
if ($count <= 0) {
$data = [
'status' => 201,
'error' => null,
'message' => [
'success' => 'Tidak ada data daftar pegawai'
],
];
}
return $this->respond($data, 200);
}
***
REST Client
.env file
***
app.baseURL = 'http://ci4-test.localhost'
***
Routes.php
***
$routes->get('/rest', [\App\Controllers\Rest\RestClient::class, 'index']);
***
RestClient.php
namespace App\Controllers\Rest;
use App\Controllers\BaseController;
use Config\Services;
class RestClient extends BaseController
{
public function index()
{
$client = Services::curlrequest();
$token = "someToken";
$url = "http://ci4-api.localhost/api/users/";
$headers = [
'Authorization' => 'Bearer ' . $token,
];
$response = $client->request('GET', $url, ['headers' => $headers, 'http_errors' => false]);
return $response->getBody();
}
}
Postman
api auth
api all user list
I have already tried some simple solution, like reload all laragon service like apache server and mongodb, restart the windows and tried find online, but it only suggest that the url is incorectly used like in this one []https://stackoverflow.com/questions/56700991/codeigniter-301-moved-permanently[3]
Is there anyone have same issue or solution, thanks in advance.
After trying some few more time, i found the problem. It still around about url similiar like in case of Codeigniter 301 Moved Permanently, but my problem i added "/" on my url.
eg
RestClient.php
//Read all users
$url = "http://ci4-api.localhost/api/users/";
Maybe i added it after copy paste process
so the correct url is
RestClient.php
//Read all users
$url = "http://ci4-api.localhost/api/users";
hopefully help some people facing same problem

yii2 blank page issue in all crud create and update actions

Please help me.
I have problem as follow:
At the localhost everything ok.
But, at hosting after model save method, controller redirect method return blank page without any error.
my code here:
public function actionCreate()
{
$model = new Project();
$file = UploadedFile::getInstance($model, 'file');
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if($file) {
$path = 'uploads/projects/' . $file->baseName . '.' . $file->extension;
$file->saveAs($path);
$model->image = $path;
$model->save(false);
}
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
But redirect method works normally outside model save() method.
Thanks
Try this.
Edited:
public function actionCreate()
{
$model = new Project();
if ($model->load(Yii::$app->request->post())) {
$model->file = UploadedFile::getInstance($model, 'file');
if($file) {
$path = 'uploads/projects/' . $file->baseName . '.' . $file->extension;
$file->saveAs($path);
$model->image = $path;
$model->save(false);
}
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}

Create deep hash mapping in perl

Below is my Code with the Hash
#!/usr/bin/perl
use warnings;
use JSON::PP; # Just 'use JSON;' on most systems
my %name = (
'sl' => {
'fsd' => {
'conf' => {
'ul' => '/sl/fsd/conf/ul',
'si' => '/sl/fsd/conf/si',
'ho1' => '/sl/fsd/conf/ho1'
}
}
},
're' => {
'fsd' => {
'cron' => {
'README' => '/re/fsd/cron/README'
},
'bin' => {
'db' => {
'smart.p_add_tag' => '/re/fsd/bin/db/smart.p_add_tag',
'smart.p_tag_partition' => '/re/fsd/bin/db/smart.p_tag_partition',
'smart.p_add_tag_type' => '/re/fsd/bin/db/smart.p_add_tag_type'
}
},
'doc' => {
'SMART' => '/re/fsd/doc/SMART',
'README' => '/re/fsd/doc/README'
},
'data' => {
'README' => '/re/fsd/data/README'
},
'conf' => {
'al1' => '/re/fsd/conf/al1',
'file' => '/re/fsd/conf/file',
'ho' => '/re/fsd/conf/ho',
'al3' => '/re/fsd/conf/al3',
'hst' => '/re/fsd/conf/hst',
'us' => '/re/fsd/conf/us',
'README' => '/re/fsd/conf/README',
'al2' => '/re/fsd/conf/al2'
}
}
}
);
(my $root) = keys %name;
my %nodes = ();
my %tree = ();
my #queue = ($root);
list_children(\%name, \#queue, \%nodes) while #queue;
my $tree = build_tree($root, \%nodes);
my $json = JSON::PP->new->pretty; # prettify for human consumption
print $json->encode($tree);
sub list_children {
my $adjac = shift;
my $queue = shift;
my $nodes = shift;
my $node = shift #$queue;
my #children = keys %{$adjac->{$node}};
#children = grep { ! exists $nodes->{$_}} #children;
$nodes->{$node} = \#children;
push #$queue, #children;
}
sub build_tree {
my $root = shift;
my $nodes = shift;
my #children;
for my $child (#{$nodes->{$root}}) {
push #children, build_tree($child, $nodes);
}
my %h = ('text' => $root,
'children' => \#children);
return \%h;
}
I'm trying to output JSONified hash, but it is only traversing upto two levels. whereas i need it to traverse all upto the last child node of each parent. Can someone please help to achieve this.
Below is current output
{
"text" : "sl",
"children" : [
{
"text" : "fsd",
"children" : []
}
]
}
Normally, transforming the hash, and then json-ing is not the most efficient idea, because you're going to make one traversal to transform the hash and JSON's going to make one to json-ify it, and JSON is a type of transform of a hash.
However, JSON is usually done with XS, which means that the second traversal is faster, at least. That and JSON behavior is standardized.
use 5.016;
use strict;
use warnings;
use Data::Dumper ();
use JSON;
my $hash
= {
'Foods' => {
'fruits' => {
'orange' => '1',
'apple' => '2',
},
'Vegetables' => {
'tomato' => '3',
'carrot' => '1',
'cabbage' => '2',
}
}
};
sub descend {
my ( $structure, $block ) = #_;
my $res;
while ( my ( $k, $v ) = each %$structure ) {
$block->( $structure, $k, $v );
if ( ref( $v ) eq 'HASH' ) {
$res = descend( $v, $block );
}
}
return $res;
}
my $new = {};
my $curr = $new;
descend( $hash => sub {
my ( $lvl, $k, $v ) = #_;
my $node = { text => $k };
$curr->{children} //= [];
push $curr->{children}, $node;
if ( ref( $v ) eq 'HASH' ) {
$curr = $node;
}
else {
$node->{children} = { text => $v };
}
});
# allow for the root-level special case, and retrieve the first child.
$new = $new->{children}[0];
say Data::Dumper->Dump( [ $new ], [ '$new' ] );
say JSON->new->encode( $new );

Redirect while error in form ZF2

I have a ZF2 form and validators.
I located at http://example.com/public/questions/edit/5730/2770,
where 5730 - $_GET['variant_id'], 2770 - $_GET['test_id'].
When I set input to empty value and submit form - I have an error, and ZF2 redirect me to http://example.com/public/questions/edit/5730 - without test_id (/2770).
How I can redirect me to a valid url? Thank all for answers.
Action:
public function editAction()
{
$language = 'EN';
$request = $this->getRequest();
$this->layout()->setVariable('messenger', 'Edit Questions');
$id = $this->params()->fromRoute('id');
$variantId = $this->params()->fromRoute('variant_id');
$sm = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$stCategories = new CategoriesTable($sm);
$conFunc = new FunctionsController();
$form = new Form\AddQuestionsForm();
$stCVariants = new ContestVariantsTable($sm);
$stQuestions = new QuestionsTable($sm);
$categoryList = $stCategories->getCategories($language);
$categories = $conFunc->_getSubs(null, $categoryList);
$config = $this->getServiceLocator()->get('config');
$folder = $config['settings']['url'] . $config['settings']['media_files'];
if (empty($id) && !$request->isPost()) {
$this->redirect()->toRoute('stickynotes', array('controller' => 'stickynotes', 'action' => 'contests'));
} elseif ($request->isPost()) {
$form->setInputFilter($stQuestions->getAddQuestionsFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$arrPost = (array) $request->getPost();
$arrFile = (array) $request->getFiles();
$postData = array_merge_recursive($arrPost, $arrFile);
$tryAddQuestion = $stQuestions->updateQuestions($postData);
if ($tryAddQuestion['query'] && $tryAddQuestion['exception'] === null) {
$isUpload = new \Zend\Validator\File\UploadFile();
if ($isUpload->isValid($postData['filebutton'])) {
$path_parts = pathinfo($postData['filebutton']['name']);
$extension = '.' . $path_parts['extension'];
$filename = $tryAddQuestion['lastId'];
$config = $this->getServiceLocator()->get('config');
$folder = $config['settings']['media_files'];
if ($postData['AddQuestionMedia'] == 'image') {
$validator = new \Zend\Validator\File\IsImage();
$validator2 = new \Zend\Validator\File\Extension(array('png', 'jpeg', 'jpg', 'gif'));
if ($validator->isValid($postData['filebutton'])
&& $validator2->isValid($postData['filebutton'])) {
$filter = new \Zend\Filter\File\Rename($folder . 'images/' . $filename . $extension);
$filter->filter($postData['filebutton']);
chmod($config['settings']['media_files'] . 'images/' . $filename . $extension, 0644);
$stQuestions->updateQuestionsFile($filename, $extension);
}
} elseif ($postData['AddQuestionMedia'] == 'video') {
$validator = new \Zend\Validator\File\Extension(array('mp4'));
if ($validator->isValid($postData['filebutton'])) {
$filter = new \Zend\Filter\File\Rename($folder . 'videos/' . $filename . '.mp4');
$filter->filter($postData['filebutton']);
chmod($config['settings']['media_files'] . 'videos/' . $filename . '.mp4', 0644);
$stQuestions->updateQuestionsFile($filename, $extension);
}
} elseif ($postData['AddQuestionMedia'] == 'audio') {
$validator = new \Zend\Validator\File\Extension(array('mp3'));
if ($validator->isValid($postData['filebutton'])) {
$filter = new \Zend\Filter\File\Rename($folder . 'sounds/' . $filename . '.mp3');
$filter->filter($postData['filebutton']);
chmod($config['settings']['media_files'] . 'sounds/' . $filename . '.mp3', 0644);
$stQuestions->updateQuestionsFile($filename, $extension);
}
} else {
$this->layout()->setVariable('messenger_error', 'Uploaded file have incorrect format');
}
}
$this->layout()->setVariable('messenger_info', 'Question has been updated!');
$this->redirect()->toRoute('questions', array('controller' => 'questions', 'action' => 'edit',
'id' => $id, 'variant_id' => $postData['question_id']));
} elseif(!empty($tryAddContest['exception'])) {
$this->layout()->setVariable('messenger_error', 'Failed with DB while update question. Please try again. ' . $tryAddQuestion['exception']);
} else {
$this->layout()->setVariable('messenger_error', 'Failed while update question. Please try again.');
}
if (!empty($postData['variant_id'])) {
$stCVariants->addEntity($postData['variant_id'], $tryAddQuestion['lastId']);
}
}
} else {
if (!empty($variantId)) {
$getContest = $stCVariants->getSingleContest($variantId);
$progressions = array();
$used_progressions = $stCVariants->getUsedProgressions($id);
for ($i=$getContest['progression_start']; $i<=$getContest['progression_stop']; $i++) {
if (!in_array($i, $used_progressions))
$progressions[] = $i;
}
$form->remove('AddQuestionProgression');
$form->getInputFilter()->remove('AddQuestionProgression');
$select = new \Zend\Form\Element\Select('AddQuestionProgression');
$select->setValueOptions($progressions)->setAttributes(array(
'id' => 'progression_id',
'class' => 'form-control',
'style' => 'width: 408px;',
));
$form->get('variant_id')->setValue($id);
$form->add($select);
}
$getQuestion = $stQuestions->getSingleQuestion($id);
switch ($getQuestion['media_type']) {
case 'image':
$folder .= 'images/' . $getQuestion['media_content'];
break;
case 'video':
$folder .= 'videos/' . $getQuestion['media_content'];
break;
case 'audio':
$folder .= 'sounds/' . $getQuestion['media_content'];
break;
}
$form->get('variant_id')->setValue($id);
$form->get('question_id')->setValue($variantId);
}
return array(
'form' => $form,
'categories' => $categories,
'url' => $folder,
'id' => $id
);
}

How to show data in different sequence in database in zend framework?

I am using datatables to list my data. It showing my data in the same sequence as it is in database tables . But I want to show it in a different sequence.
My code:
$userData = array();
$row = array();
$action = array();
foreach ($students as $key => $student) {
foreach ($student as $key => $value) {
switch ($key) {
case 'firstName' :
$firstName = $value;
break;
case 'lastName' :
$lastName = $value;
$row[] = $lastName;
break;
case 'phone' :
$row[] = $value;
break;
case 'email' :
$row[] = $value;
break;
case 'studentId':
$row['DT_RowId'] = $value;
$action[] = "<a id = {$value} class = 'update'>Edit</a>&nbsp | &nbsp<a id = {$value} class = 'delete'>Delete</a> ";
}
}
$userData[] = array_merge($row, $action);
unset($row);
unset($action);
}
I am not getting exactly but if you want column in different order, i do like this
view:-
<?php foreach $zendDbRowObject->getTable()->getDisplayOrder() as $fieldName): ?>
<?php echo $zendDbRowObject->$fieldName; ?>
<?php endforeach; ?>
controllar:-
public function getDisplayOrder() {
// fake column names obviously... use yours here.
return array(
'column5',
'column1',
'column4',
'column2',
'column3'
);
}