Not receiving Slack interactive payload - laravel

I have set up a notification with action buttons, but I do not receive the in interactive payload. I have configured the webhook URL here:
When I post to that URL in Postman everything is fine. But when I press the buttons I get no callback to that URL I get nothing(I'm logging all requests to that URL). I also get a warning triangle next to my buttons like this:
I get no other feedback and no Ideas where I should debug.
The massage with the buttons is published with Laravel's Slack notification channel plus this package for block support: https://github.com/nathanheffley/laravel-slack-blocks
This is the code for sending the notification, but I don't think it is relevant:
public function toSlack()
{
$model = $this->model;
$message = (new SlackMessage())
->error()
->content('New report!');
$message->attachment(function ($attachment) use ($model) {
$attachment->block(function ($block) {
$block
->type('actions')
->elements([
[
'type' => 'button',
'text' => [
'type' => 'plain_text',
'text' => 'Approve',
'emoji' => false,
],
'value' => 'click_me_123',
'action_id' => 'approve_x',
'style' => 'primary',
],
[
'type' => 'button',
'text' => [
'type' => 'plain_text',
'text' => 'Reject',
'emoji' => false,
],
'value' => 'click_me_1234',
'action_id' => 'reject_x',
'style' => 'danger',
],
]);
});
});
return $message;
}
Any ideas how I should proceed with debugging?

Today, I noticed that I got some more information if I hoovered the triangle, but I'm pretty sure that didn't work yesterday. I got the message "500 server error" there.
The problem was that the payload JOSN is sent as a form-data field and not in the POST request body like normal. Why do they send the payload like this? It makes no sense.

Related

Sending automated email with dynamic content template

Well, I want to send an automated email every Friday using MailChimp/Mandrill.
Email Template :
Hi {{firstName}},
You weekly weekend pass is {{code}}.
Here, My email content is dynamically generated. So, before sending automated email, there should be some mechanism where I call the rest api from where I get the firstName and code. Then email is sent to different user.
{
[{
firstName: 'test',
code: 'Xewetwerx'
},
{
firstName: 'test2',
code: 'Xewetwrtd'
}]
}
I know we can write code in our backend server and call the API every Friday using cronjob(to send an email every Friday) but I don't want to use any cronjob.
Is it possible to make API call and send automated mail using MailChimp or mandrill ???
Outcome:
2 email should be sent with different code.
Mandrill implements scheduled emails through the sent_at parameter.
It's possible to send an email using a template and specify date and time when the message should be sent as a UTC timestamp in YYYY-MM-DD HH:MM:SS format. If you specify a time in the past, the message will be sent immediately. Note that for the sent_at feature you will need a paid (non free) account
If you choose PHP as the programming language for your backend,
you can use the official PHP API library.
example: (the code is not tested and could contain errors)
require_once '/path/to/MailchimpTransactional/vendor/autoload.php';
$mailchimp = new MailchimpTransactional\ApiClient();
$mailchimp->setApiKey('YOUR_API_KEY');
$msg = array(
'subject' => 'My subject',
'from_email' => 'ellery#example.com',
'to' => array(array('email' => 'recipient1#example.com', 'name' => 'Recipient 1'),array('email' => 'recipient2#example.com', 'name' => 'Recipient 2')),
'merge_vars' => array(array(
'rcpt' => 'recipient1#example.com',
'vars' =>
array(
array(
'name' => 'firstName',
'content' => 'test'),
array(
'name' => 'code',
'content' => 'Xewetwerx')
)),
array(
'rcpt' => 'recipient2#example.com',
'vars' =>
array(
array(
'name' => 'firstName',
'content' => 'test2'),
array(
'name' => 'code',
'content' => 'Xewetwrtd')
))));
$response = $mailchimp->messages->sendTemplate([
"template_name" => "template_name",
"template_content" => [[]],
"message" => $msg,
"send_at" => "2021-06-23 09:05:00" ,
]);
print_r($response);

Guzzle Post Null/Empty Values in Laravel

I've been trying to work with Guzzle and learn my way around it, but I'm a bit confused about using a request in conjunction with empty or null values.
For example:
$response = $client->request('POST',
'https://www.testsite.com/coep/public/api/donations', [
'form_params' => [
'client' => [
'web_id' => NULL,
'name' => 'Test Name',
'address1' => '123 E 45th Avenue',
'address2' => 'Ste. 1',
'city' => 'Nowhere',
'state' => 'CO',
'zip' => '80002'
],
'contact' => [],
'donation' => [
'status_id' => 1,
'amount' => $donation->amount,
'balance' => $donation->balance,
'date' => $donation->date,
'group_id' => $group->id,
],
]
]);
After running a test, I found out that 'web_id' completely disappears from my request if set to NULL. My question is how do I ensure that it is kept around on the request to work with my conditionals?
At this point, if I dd the $request->client, all I get back is everything but the web_id. Thanks!
I ran into this issue yesterday and your question is very well ranked on Google. Shame that it has no answer.
The problem here is that form_params uses http_build_query() under the hood and as stated in this user contributed note, null params are not present in the function's output.
I suggest that you pass this information via a JSON body (by using json as key instead of form_params) or via multipart (by using multipart instead of form_params).
Note: those 2 keys are available as constants, respectively GuzzleHttp\RequestOptions::JSON and GuzzleHttp\RequestOptions::MULTIPART
Try to define anything like form_params, headers or base_uri before creating a client, so:
// set options, data, params BEFORE...
$settings = [
'base_uri' => 'api.test',
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'ajustneedatokenheredontworry'
],
'form_params' => [
'cash' => $request->cash,
'reason' => 'I have a good soul'
]
];
// ...THEN create your client,
$client = new GuzzleClient($settings);
// ...and finally check your response.
$response = $client->request('POST', 'donations');
If you check $request->all() at the controller function that you are calling, you should see that were sent successfully.
For those using laravel, use this:
Http::asJson()

Nexmo Laravel connect to PSTN endpoint not working

My code is shown below. Everything works for me, besides connecting a call to a PSTN endpoint. I'm thinking possibly that maybe the "connect" functions aren't included in the Laravel Nexmo packages that I'm using. I'm using these:
https://github.com/Nexmo/nexmo-laravel
Which is built on another nexmo package:
https://github.com/Nexmo/nexmo-php
My code:
public function getNexmoAnswer(Request $request)
{
return
[
[
'action' => 'talk',
'voiceName' => 'Justin',
'text' => 'Welcome to our great site’
],
[
'action' => 'talk',
'voiceName' => 'Justin',
'text' => "Press # to search now the e.t.a status of your latest order.",
'bargeIn' => true
],
[
'action' => 'connect',
'from' => '17181234567’,
'endpoint' => [
'type' => 'phone',
'number' => '18451234567’
]
]
];
}
The connect action is part of Nexmo's NCCO system and is unrelated to Nexmo-Laravel and Nexmo-PHP. Your NCCO looks to be correct to me.
To connect to another number, you'll need to have activated your account by adding credit. In addition, the from number you're using (17181234567) must be a number purchased from Nexmo

how to stop execution of ctp file in cakephp 2.x after validating the url

In my CakePHP application, I have applied Url validations so that admin can access only those actions which are defined for admin and same as with users.
In my application, "surveylist" is the action of admin and when any user directly access that action(surveylist), URL validations work(Unauthorized access msg is displayed).
But below that message ctp file of surveylist executes forcefully and show errors because I have validated URL through the try-catch block and it cannot get the set variables of action.
I want that ctp file should not execute if unauthorize error comes.
My code for surveylist is:-
public function surveylist($pg=null){
try{
if($this->checkPageAccess($this->params['controller'] . '/' . $this->params['action'])){
$this->Paginator->settings = array(
'Survey' => array(
'limit' => 5,
'order' => 'created desc',
'conditions'=>array('is_deleted'=> 0),
'page' => $pg
)
);
$numbers = $this->Paginator->paginate('Survey');
$this->set(compact('numbers'));
}else{
$this->Flash->set(__('Unauthorised access'));
}
}catch(Exception $e){
$this->Flash->set(__($e->getMessage()));
}
}
I don't want the ctp file of surveylist to execute if control comes to else.
Plz, help me out......
Thanx in advance...
I suppose you are using prefix to separate admin and users, if not please do that it is great way to handle and restrict methods.
After doing that you have to make condition to check which prefix(admin, user) is currently active and according that load Auth component and allow action in allow() method of Auth.
Example:
$this->loadComponent('Auth',[
/*'authorize' => [
'Acl.Actions' => ['actionPath' => 'controllers/']
],*/
'loginRedirect' => [
'controller' => 'Users',
'action' => 'index'
],
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'unauthorizedRedirect' => [
'controller' => 'Users',
'action' => 'login',
'prefix' => false
],
'authError' => 'You are not authorized to access that location.',
]);
if ($this->request->params['prefix']=='admin') {
// Put actions you want to access to admin in allow method's array
$this->Auth->allow(array('add', 'edit', etc...));
} else if ($this->request->params['prefix']=='user') {
// Put actions you want to access to user in allow method's array
$this->Auth->allow(array('login', 'view', etc...));
}
This way you can restrict actions for particular role.
Hope this helps!

ZF2 using inputFilter

I wonder what I've made wrong, I want to save in db some values whats not come form POST or GET:
public function saveAction()
{
$wikiTable = $this->getServiceLocator()->get('WikiTable');
$data = array('source' => $someVal);
$form = new WikiForm();
$inputFilter = new \MyApp\Form\WikiFilter();
$form->setInputFilter($inputFilter);
$form->setData($data);
$this->saveWiki($form->getData());
//$this->saveWiki($data);
}
WikiFilter:
$this->add(
array(
'name' => 'source',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
)
)
);
And Form:
$this->add(array(
'name' => 'source',
'type' => 'Zend\Form\Element\Hidden',
'options' => array(
'label' => 'source',
)
));
In response I recive error:
Zend\Form\Form::getData cannot return data as validation has not yet
occurred
After this line:
$form->setData($data);
You need to put the rest of your code into something like this:
if($form->isValid()){
$this->saveWiki($form->getData());
}
Otherwise your form isn't validated and you won't get any data back from it by calling $form->getData()
So whenever you work with a form (not matter if the data come from a POST request or not) make sure to call the function isValid() on the form variable because otherwise you won't get the data back and you will get the error you wrote before

Resources