Slack Incoming Webhooks - Change Action URL? - php

I'm unclear after reading through Slack's documentation around Incoming Webhooks. It seems like there's some ambiguity around Incoming Webhooks vs. posts through their chat API. It sounds like if I call it from https://my-application.yxz/api/notify?key=123123123123, and post to Slack from there, then Slack's action buttons will reply to that same URL, but I can't confirm that.
https://api.slack.com/docs/message-attachments
I'm successfully sending messages to the specified channel, but I can't figure out how to get a response back, or how to change the URL the actions will send their own payload to. Is that even possible?
For context of the code below, I'm using Laravel. Ideally, I wanted to only retain a Slack Webhook URL in the user settings for their store's location so they could receive Slack notifications triggered by our API being hit.
This is the payload I'm sending to the webhook and it works fine, however I would like to add actionable buttons and post the payload back to my external site. I'm not sure if Incoming Webhooks are even the right approach. Can someone shed some light on this?
# $helpdesk => contains webhook URL and other details
# $prompt => just some text
# $prompt_id => id associated with prompt object passed into the function this is found in
# $device => the source of this prompt
$payload = json_encode([
"text" => "A customer at $device->name requires attention",
"username"=>"Our Alert Thing",
"icon_emoji"=>":wave:",
"attachments"=> [
[
"fallback"=> $prompt,
"author_name"=> "Customer # $device->name",
"title"=> $prompt,
"text"=> "Let them know if you can help",
"callback_id"=> "csprompt_".$prompt_id,
"color"=> "#24ACE2",
"actions"=> [
[
"name"=> "action",
"type"=> "button",
"text"=> "I call dibs",
"style"=> "primary",
"value"=> "helping",
],
]
]
]
]);
$res = $client->post($helpdesk->slack_webhook, ['body'=>$payload]);

You can not programmatically set your action URL. Slack will send all interactive message requests (e.g. from a user clicking on one of your buttons) to the "request URL" that you have configured for your Slack app.
Check this link for details on where to find the request URL.
I think technically you can use incoming webhooks, but I would recommend using the API with chat.postMessage, because incoming webhooks are very limited. e.g. they can only be used with one pre-configured channel.

Related

Yii2 - Do a http form POST on external URL and redirect to it

I have an application on Yii2.
I have an external vendor i want to redirect. For example, i am encrypting the current user info, and want to send that to the external vendor, so the user information is automatically populated on his website.
Everything works fine if i do that on the front end side using JS.
The problem i have is only my app server is whitelisted to the vendor. So i need to make the redirection happen on the backend.
I tried several method, and even with Guzzle HTTP, and it doesn't redirect.
TO be clear, i am not looking for a simple redirect, it is a form post on external URL.
I went though this post, but the problem remain the same...
Yii2 how to send form request to external url
Does anybody faced this issue?
If I understand you correctly, you want to POST data received from your frontend to the server of a third party company (let's call them ACME). Your webserver is whitelisted by ACME but your clients are not (which is good). You want to show a page on your server and not a page on the ACME server after the POST, right?
You have to send the data with a separate request from your server to the ACME server. You cannot create a HTML <form> and put the ACME server in the action parameter of it, because the request would be sent from the client and not your backend, failing at the whitelist. After you validate your client input, you can send it with guzzle to ACME.
With the status code you can decide if your request was successful or not. You can read more about guzzle here
The code sample below shows how you could POST the $payload (a fictional blog post if you will) as JSON to the ACME server and output a line with the request result.
$payload = [
'username' => 'john.doe',
'postContent' => 'Hello world',
];
$client = new GuzzleHttp\Client();
$response = $client->post('https://acme.example.org/endpoint',['http_errors' => false, 'json' => $payload]);
if($response->getStatusCode() === 200){
echo "Request OK";
}else{
echo "Request failed with status ".$response->getStatusCode();
}

Web browser audio call using Nexmo PHP Laravel

I want to make a call from a web application(PHP/Laravel) to any mobile number from (purchased Nexmo Number). It's like a two-way communications call.
The scenario assumes in the web application(PHP/Laravel) page display the call driver options placed the call icon. Once customers click the call icon then call to driver number from (purchased Nexmo Number).
I have used this API to create a call.
$ncco = [
[
'action' => 'talk',
'voiceName' => 'Joey',
'text' => 'This is a text-to-speech test message.'
]
];
$call = new \Nexmo\Call\Call();
$call->setTo('XXXXXXXXXXXX')
->setFrom('XXXXXXXXXXXX')
->setNcco($ncco);
$response = $client->calls()->create($call);
echo $response->getId();
The Nexmo Voice API here one-way communication only working fine for me. For example, text to speech call works for me, the above voice API code run the call automatically reached to destination number from (purchased Nexmo Number).
Is anybody has done this scenario? when you click on the phone icon it will call customers + you can talk with customer using a web portal?
There are two ways to go about this.
Call Bridging
You can bridge two numbers together by having the system call you, and if you answer then call someone else to bridge them in. This can all be done on the server side much like what you have above, the NCCO just changes slightly.
$ncco = [
[
'action' => 'connect',
'endpoint' => [
[
'type' => 'phone',
'number' => DRIVER_NUMBER
]
]
]
];
$call = new \Nexmo\Call\Call();
$call->setTo(CUSTOMER_NUMBER)
->setFrom(VONAGE_NUMBER)
->setNcco($ncco);
$response = $client->calls()->create($call);
echo $response->getId();
The only real problem with this is the user experience. The user probably expects the call work like a real phone call (click the button, hear ringing, hope the driver connects). You would need to add some additional NCCO options like streaming a ring tone, checking to see if the other person declines the call or never answers and responding appropriately, etc, but it can be done by pushing some NCCOs around and watching the voice events.
In Browser/In App
The other option is our Client SDK, which is available for front-end JavaScript, iOS, and Android. This can be used to place a call from a browser or app, and do functionally the same but from within a dedicated interface. A short tutorial can be found at https://developer.nexmo.com/client-sdk/tutorials/app-to-phone/introduction/javascript.

Receiving Json Data in a Yii2 basic application controller

I am integrating Orange Money Payment gateway with my Yii2 basic Application to be able to receive local payments on this application.
In this API, when the user initiates a transaction, I receive some data after sending a curl request to the Orange money API. I store this data in my Database with a key call notif_token. The user is then redirected to orange payment portal where the payment is done. when the User completes a payment process on their portal, they send me json response to a particular url call Notifcation URL. I am suppose to receive this data, update my Database and grant access to this user to some resources.
Everything works well till the level of receiving the feedback in from them through the notification URL.
I have tried all I know to receive this information but to no avail since this action is not an api url. I have written my action as shown below but I do not know what I am missing.( might be a configuration for this action or something).
public function actionOnotification(){
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$request = \yii::$app->request->post();
$transaction =OrangeFeedback::findOne(['notif_token'=>$request['notif_token']]);
$transaction->status = $request['status'];
$transaction->txnid = $request['txnid'];
$transaction->save();
//do some processing here
}
I do not know how to solve this problem as I feel I am missing a fundamental concept here( might be on how to configure a Yii2 basic application action to receive json data, might be how to convert that action into an API call URL or something which I can not yet figure out). Any help on this will be greatly appreciated as I can't find any resources online to help me.
To receive JSON data you need to configure your request component in the config:
'components' => [
...
'request' => [
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
],
...
]
See the docs for more information

When using twilio's php notify API, how do you set your callback URL?

I have the following code and it sends SMS notifications to my phone:
$notification = $twilio->notify->services($serviceSid)
->notifications->create([
'toBinding' => $batch,
'body' => $txt,
'statusCallback' => 'http://postb.in/b/jarblegarble' // <-- this doesn't work
]);
However, even though the sending works, I can't seem to figure out their callbacks.
I'm scouring through their docs and I can't find how to set the callback URL. I see some of their resources use "url" while others use "statusCallback" (heck, one seems to use "redirect"). That being said, I can't seem to post to postb.in using them -- there must be a way to check the status of my notification.
So it turns out I was wrong on two fronts.
1) The callback URL needs to be passed to your messaging service this way:
$notification = $twilio->notify->services($serviceSid)
->notifications->create([
'toBinding' => $bindings,
'body' => $txt,
'sms' => ['status_callback' => 'http://your_callback_url' ]
]);
2) postb.in wasn't working! I was testing the code above, after being assured by twilio support that it was valid, I decided to try and post to my own server and just capture the POSTed content. Sure enough, it was working as they suggested.
Edit: It wasn't clear to me at the time but the callback URL will be called for each SMS sent out for each status update. So that means queued, sent, and delivered. I initially thought that I'd just get a status update for the batch itself as I don't necessarily care for the status of up to 10,000 txt messages.
Your example passes the statusCallback parameter of the individual SMS service API to the universal notify API. This mixing won't work. The individual SMS service sets up a callback for that one particular message, which isn't efficient for batch sends. The universal notify API, in contrast, relies on web hooks, which are globally configured per service.
The simplest thing to do, in your case, is to use the individual SMS service API:
$message = $twilio->messages->create('+15551234567', [ 'body' => 'Hi',
'from' => '+15559876543',
'statusCallback' => 'http://postb.in/b/jarblegarble' ]);
To use the universal notify API, you'll need to set the PostWebhookUrl to the target URL when creating the notification service, and arrange for the code at that URL to handle onMessageSent messages. More at the "web hooks" URL above.
Caveat emptor: haven't tried any of this, and I haven't used Twilio in literally eight years, but the above is my theoretical understanding.

Twilio Callback URL not Called - PHP & Wordpress

I'm trying to integrate Twilio onto my Wordpress site.
The idea is to allow users to type in their phone number to get a download link to our app. We've put up a simple form here - http://evntr.co/download - and upon submitting the form, the EvntrPHP.php code is run.
Using template code, we've easily been able to get the form to send a message to a verified number (the one currently in the To field) using a free Twilio number. However, when we add the StatusCallback parameter, it never calls our callback.php code. Both EvntrPHP.php and callback.php are in the root directory - evntr.co/.
<?php
require 'twiliophp/Services/Twilio.php';
$AccountSid = "--------";
$AuthToken = "---------";
$client = new Services_Twilio($AccountSid, $AuthToken);
$phonenum = $_POST["phonenum"];
$callbackURL = "https://evntr.co/callback.php";
$client->account->messages->create(array(
'To' => "XXXXXXXXXX",
'From' => "+XXXXXXXXXX",
'Body' => "Test Message",
'StatusCallback' => "https://evntr.co/callback.php",
));
?>
My understanding is that the flow should be like this:
user navigates to evntr.co/download
user submits the form with their number
form calls EvntrPHP.php and sends a text message to their number
Twilio POSTs to callback.php whenever the status of the message changes (Sent, Delivered, Canceled, etc).
However, every time I submit the form, the message is sent and the page just stays at evntr.co/EvntrPHP.php and never loads callback.php. Maybe this is a misunderstanding on my part with how Callback URLs work? Or maybe the StatusCallback parameter doesn't work with a free Twilio number?
Twilio developer evangelist here.
You are correct that the Twilio callbacks are not working as you expect. As McCann points out, the request is made asynchronously from Twilio to the URL you supply. You can use the callback to keep track of the progress of the message, but not affect the request the user has made.
So, in your example you either want to render something after you have sent the message:
<?php
require 'twiliophp/Services/Twilio.php';
// other stuff
$client->account->messages->create(array(
'To' => $phonenum,
'From' => "+14708655xxx",
'Body' => "Test Message",
'StatusCallback' => "https://evntr.co/callback.php",
));
?>
<h1>You should receive an SMS, click the link in the SMS to download the app on the platform of choice.</h1>
With some more style than a plain <h1> of course! Or, you could redirect to a page with a success message on. (PHP is not my strongest subject, but discussions of redirects are rife on this StackOverflow question)
Good luck with the app, and let me know if you have any more Twilio questions!
[edit]
As discussed in the comments, if you want to see if the API request was successful you'll want to do something like:
<?php
require 'twiliophp/Services/Twilio.php';
// other stuff
try {
$client->account->messages->create(array(
'To' => $phonenum,
'From' => "+14708655xxx",
'Body' => "Test Message",
'StatusCallback' => "https://evntr.co/callback.php",
));
// Success! Redirect to success page!
header("Location: http://evntr.co/success.php");
die();
} catch (Services_Twilio_RestException $e) {
// Something went wrong!
// Do something about it!
}
?>
So, wrapping the API call in a try/catch block and responding appropriately should catch most errors from incorrect phone numbers or other API errors. It won't guarantee that the SMS has been delivered (you'll get that from the callback webhook) but it will guarantee that you've done all you can to get the SMS sent.
The callback doesn't work like you think it does.
Call End Callback (StatusCallback) Requests
After receiving a call, requesting TwiML from your app, processing it, and finally ending the call, Twilio will make an asynchronous HTTP request to the StatusCallback URL configured for the called Twilio number (if there is one). By providing a StatusCallback URL for your Twilio number and capturing this request you can determine when a call ends and receive information about the call.
-- https://www.twilio.com/docs/api/twiml/twilio_request

Categories