How can I get the phone number from Twilio in fulfillment dialogflow - node.js

I'm working in a whastapp bot between Twilio and Dialogflow. In one of the intents, I'm sending media files from dialogflow to the user. Here it's the problem, now it's working with my mobile number hardcoded, but I need to access to the user phone number in each case to send the media file.
I develop this in fulfillment dialogflow, inline editor, using some nodejs code, but there I can't access to the user number.
Fulfillment node js
function sendAudioMensaje(agent) {
client.messages.create({
body: 'Body message',
to: 'whatsapp:+34---------', // Text to this number
from: 'whatsapp:+14155238886', // From a valid Twilio number
mediaUrl: 'https://demo.twilio.com/owl.png'
}).then((message) => agent.add(message.sid));
}
I expect that the message would be sent to the current number in each conversation

Twilio developer evangelist here.
As far as I can tell, the Dialogflow webhook request comes with a originalDetectIntentRequest property in the JSON. This contains a OriginalDetectIntentRequest object, which has a payload property that contains the original request.
I'd start by logging the contents of the webhook request to see what is fully available to you though.
Let me know if that helps at all.

In the payload of the OriginalDetectIntent request you will see a JSON like this in the webhook if it's enabled.
{
"source": "twilio",
"data": {
"SmsSid": "",
"Body": "",
"SmsStatus": "received",
"MessageSid": "",
"ApiVersion": "2010-04-01",
"From": "",
"AccountSid": "",
"NumMedia": "0",
"To": "",
"SmsMessageSid": "",
"NumSegments": "1"
}
}

Related

How can i get the call-sid from response of callback in twilios AddOn's "IBM Watson"

We enable the Twilios AddOn "IBM Watson Speech to Text" for recording transcribe.and we set our server webhook while enabling the addon. Now twilio calling webhook once call is completely done. after completing call twilio give response in that webhook In Nodejs.For handling that call response we need "Call Sid".In webhook response we cannot get "callSid".So how we can identify that response is for this particular call...?
anyone help me to how can find call-sid in response?
Callback response is
{
"status": "successful",
"message": null,
"code": null,
"results": {
"ibm_watson_speechtotext": {
"request_sid": "**************",
"status": "successful",
"message": null,
"code": null,
"payload": [{
"content_type": "application/json",
"url": "*************/Data"
}],
"links": {
"add_on_result": "***********/Accounts/********/Recordings/**********/AddOnResults/******",
"payloads": "https://api.twilio.com/2010-04-01/Accounts/******/Recordings/*******/AddOnResults/******/Payloads",
"recording": "https://api.twilio.com/2010-04-01/Accounts/******/Recordings/*******"
}
}
}
}
Thank you for help
When you get the Add-on result it comes as part of the Twilio voice webhook, with all the normal request parameters as well as an extra AddOns parameter which contains the JSON that you have shared above.
Webhook requests from Twilio are in the format application/x-www-form-urlencoded and then within the AddOns parameter the data is a JSON string. In a (Ruby-like) pseudo code, you can access the call sid and the add on data like this:
post "/webhook" do
call_sid = params["CallSid"]
add_on_json = params["AddOns"]
add_on_data = JSON.parse(add_on_json)
# return TwiML
end

Can a webhook server have no response?

I have a dialogflow implementation using Google assistant, a nodeJS server to serve the webhook calls. Some of the user intents are empty phrases, and they do not need any response from webhook server. However, the webhook must be called with every user sentence.
Can the webhook response return an empty response and not crash while doing so?
The server returns the response in predefined JSON format known to google assistant. When I set this response to NULL OR when I set the payload part of this response to BLANK, the application crashes.
var simpleChatResponse = {
"payload": {
"google": {
"expectUserResponse": true,
"richResponse": {
"items": [
]
}
}
},
"outputContexts": [
]
};
Actual results: "myAgent1 is not responding" and the application crashes.
No, you can not send an empty response. As the docs state:
The first item in a rich response must be a simple response.

Cannot fetch internet message headers for some emails

I'm using the Microsoft Graph Client for Node.js to fetch email messages. I'm only interested in the headers. The code is working, but recently I got some errors in fetching internetMessageHeaders via the API.
For some messages, the API doesn't return any data for this field (and the code wasn't checking for this, hence the errors).
I tried to manually run the queries using POSTMAN. Using ?$select=internetMessageHeaders as a query param to only fetch the headers. If I remove the query param, I can fetch the message normally (body, etc.).
I haven't been able to find anywhere in the docs why this would happen.
Any ideas on what it's happening?
Edit: Here is the node.js code I am using
const MicrosoftGraphClient = require("#microsoft/microsoft-graph-client").Client
async function fetchEmailMessageHeaders(id, credentials) {
let client = MicrosoftGraphClient.init({
authProvider: callback => callback(null, credentials.access_token)
});
let req = client.api(id).select("internetMessageHeaders");
let message = await req.get();
return message.internetMessageHeaders;
}
Sample output (value of message var):
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('156751349d3cc68b')/messages(internetMessageHeaders)/$entity",
"#odata.etag": "W/\"CQAAABYAAABcr9US8aH2RIGaOGQZwDg3AAKyfdpk\"",
"id": "AQMkADAwATM0MDAAMS1hZWIxLThjZmYALTAwAi0wMAoARgAAA3wcQrEUgfhHoZ0BD2jmyXYHAFyv1RLxofZEgZo4ZBnAODcAAAIBDAAAAFyv1RLxofZEgZo4ZBnAODcAArJaPEcAAAA=",
"internetMessageHeaders": [
{
"name": "Received",
"value": "from BY2NAM01HT225.eop-nam01.prod.protection.outlook.com (2603:10a6:803:118::39) by VI1PR0301MB2221.eurprd03.prod.outlook.com with HTTPS via VE1PR03CA0050.EURPRD03.PROD.OUTLOOK.COM; Wed, 10 Apr 2019 11:41:55 +0000"
}
...
]
}
The problem is that for some emails, the message does not contain internetMessageHeaders.
I am using an inbox subscription to receive events for new email messages from Outlook. When an event is received I use the API to retrieve the message and extract headers from it.
API reference here
Edit2:
Here is an example JSON output of an event that fails to also retrieve headers:
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('53e07bf4-bb6a-4a82-a724-37dadfb1cf11')/messages(internetMessageHeaders)/$entity",
"#odata.etag": "W/\"CQAAABQAAADdeU+v2VzbRpZMSJGral7kAAXJkA==\"",
"id": "AAMkADhmMmVmM2NjLWFhNGMtNDBlYy04NzBkLTg5MmU2OWI0ODU4MgBGAAAAAAALg6E-e6CxRLEbZfrhFaDBBwAzAAMgKF1iTp242t34rFy5AAAAAAAOAAB5l_O62bUUTqTLk6KYrl4sAAGrJcjfAAA="
}
It seems internetMessageHeaders are not set when an email is replied to using outlook.office365.com. I'm also not able to see message headers when inspecting the message using Outlook on Mac.

Gmail api send with Postman

I am trying to send a mail using gmail API with POSTMAN, using POST Method
POST https://www.googleapis.com/upload/gmail/v1/users/example#gmail.com/messages/send
but I get an error below:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidArgument"
"message": "Recipient address required"
}
],
"code": 400,
"message": "Recipient address required"
}
}
header is already putted Content-type: message/rfc822
I know that this has to be encoded into base64(web_safe), so I translated
"From: sender.example#gmail.com\r\n" +
"To: receiver.example#gmail.com\r\n" +
"Subject: Subject Example\r\n" +
"This is content: hope you got it\r\n"
I also replaced them to be web_safe
replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
so I got an base64 like below.
so I put raw in body of POST METHOD
{
"raw": "RnJvbTogc2VuZGVyLmV4YW1wbGVAZ21haWwuY29tDQpUbzogcmVjZWl2ZXIuZXhhbXBsZUBnbWFpbC5jb20NClN1YmplY3Q6IFN1YmplY3QgRXhhbXBsZQ0KVGhpcyBpcyBjb250ZW50OiBob3BlIHlvdSBnb3QgaXQNCg"
}
I used 'try this api' on google developers' site, and I could send it.
https://developers.google.com/gmail/api/v1/reference/users/messages/send
But with POSTMAN, I cannot.
Any help please?
This mean, data format is incorrect. You should try below method which perfectly worked for me.
I use below format.
From: <FROM#gmail.com>
To: <TO#gmail.com>
Subject: Test Email
Test
For testing purpose, I used https://ostermiller.org/calc/encode.html to 64encode above text message. So I will get encoded string as below
IEZyb206IDxGUk9NQGdtYWlsLmNvbT4KICAgIFRvOiA8VE9AZ21haWwuY29tPgogICAgU3ViamVjdDogVGVzdCBFbWFpbAogICAgCiAgICBUZXN0
Now in postman,
Gmail Rest API URL you have to use https://www.googleapis.com/gmail/v1/users/<YOUR#gmail.com>/messages/send
Content type should be json because you send json format in message body.
Content-Type: application/json
In body
{
"raw": "IEZyb206IDxGUk9NQGdtYWlsLmNvbT4KICAgIFRvOiA8VE9AZ21haWwuY29tPgogICAgU3ViamVjdDogVGVzdCBFbWFpbAogICAgCiAgICBUZXN0"
}
So finally postman looks like as below.
Once you send a request to API, You will receive response looks like this
{
"id": "172016110a227c19",
"threadId": "172016110a227c19",
"labelIds": [
"UNREAD",
"SENT",
"INBOX"
]
}
I think that you should set the Content-type header to application/json. Also, don't forget to add the Authorization header.
If you want more details please refer below link:
How to send a message successfully using the new Gmail REST API?
If you want more details please refer below link: Gmail API send message without using Base64 encode

Slack Interactive Messages: No POST response from Slack

I'm constructing and sending the message attachment:
var zz = {
"text": "Would you like to play a game??",
"attachments": [
{
"text": "Choose a game to play",
"fallback": "You are unable to choose a game",
"callback_id": "wopr_game",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [
{
"name": "chess",
"text": "Chess",
"type": "button",
"value": "chess"
}
]
}
]
}
web.chat.postMessage(message.source.channel, '', zz);
The message with buttons displays fine on Slack, but when I tap a button there's no POST response from Slack to my local ngrok or express route:
app.post('/slackaction', function(req, res)
While other messages are posting fine to this route.
I'm seeing this error from Slack after I tap a button:
"Oh no, something went wrong. Please try that again"
Slack Interactive Messages request_url set as:
https://xxx.ngrok.io/slackaction
Thanks to comments from Taylor Singletary for pointing me in the right direction.
The test tokens or a bot token for Custom Integrations can post Interactive Messages but you need an App to handle them.
To fix it you need to add a bot to your app here: https://api.slack.com/apps
and then get the access token for that bot.
You can use the Slack button generator here: https://api.slack.com/docs/slack-button# to get the OAuth URL and paste it on a browser.
Then from your app handle the OAuth flow, store the access token and use that with chat.postMessage.
Then you should receive the POST request when clicking on the message buttons.

Resources