Twilio Whatsapp: Send Location not working - twilio

I'm using the FUNCTION Widget in Twilio Studio.
I need to send location inside a message in whatsapp.
I've tried this peice of code inside a function in Twilio:
// This is your new function. To start, set the name and path on the left.
exports.handler = function(context, event, callback) {
// The pre-initialized Twilio Client is available from the `context` object
const twilioClient = context.getTwilioClient();
// Determine message details from the incoming event, with fallback values
const from = event.From || 'whatsapp:+14155238886';
const to = event.To || 'whatsapp:+556181584246';
const body = event.Body || 'Ahoy, World!';
const persistentAction = 'geo:-1.232453, 36.878987';
twilioClient.messages
.create({to, body, from, persistentAction})
.then((result) => {
console.log('Created message using callback');
console.log(result);
console.log(result.sid);
return callback();
})
.catch((error) => {
console.error(error);
return callback(error);
});
};

Related

Receiving and handling media messages in Twilio-dialogflow WhatsApp integration

I have deployed a server on GCP to receive message traffic from twilio via webhook and integrated it with Google's dialogflow. You can see the original project here "https://github.com/GoogleCloudPlatform/dialogflow-integrations#readme".
The function works fine for receiving and responding via intent detection but it can't handle any media inputs from a user as dialogflow can't interpret it. I've been trying to code a simple IF statement that converts any input media into URL's prior to processing by dialogflow. The full code server.js file is given below:
const express = require('express');
const request = require('request');
const app = express();
const dialogflowSessionClient = require('../botlib/dialogflow_session_client.js');
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const projectId = 'PROJECT-ID';
const phoneNumber = "+1##########";
const accountSid = '*********************';
const authToken = '*******************';
const client = require('twilio')(accountSid, authToken);
const MessagingResponse = require('twilio').twiml.MessagingResponse;
const sessionClient = new dialogflowSessionClient(projectId);
const listener = app.listen(process.env.PORT, function() {
console.log('listner marker');
console.log('Your Twilio01 integration server is listening on port '+ listener.address().port);
});
app.post('/', async function(req, res) {
const body = req.body;
const text = body.Body;
const id = body.From;
console.log('body marker');
const dialogflowResponse = (await sessionClient.detectIntent(text, id, body)).fulfillmentText;
const twiml = new MessagingResponse();
const message = twiml.message(dialogflowResponse);
res.send(twiml.toString());});
process.on('SIGTERM', () => {
listener.close(() => {
console.log('Closing http server.');
process.exit(0);
});
});
I have tried to add my IF statement like below but it fails to execute when placed into the main file.
if (MessagingResponse.NumMedia != "0") {
console.log(MessagingResponse.MediaUrl0);
MessagingResponse = MessagingResponse.MediaUrl0;
console.log(response.toString());
}
you cannot change the const variable value on the next statement so
change your
const text;
to
var text;
and add
if(body.MediaUrl0){
text = body.MediaUrl0;
}
before
const dialogflowResponse = (await sessionClient.detectIntent(
text, id, body)).fulfillmentText;
Twilio developer evangelist here.
You're trying to read the NumMedia and other properties from the MessagingResponse class. You should be trying to read it from the body of the incoming request: req.body.NumMedia.
Edit
Checked your code from the repo. The const still doesn't seem right. Try this:
app.post("/", async function (req, res) {
const body = req.body;
let text;
if (body.NumMedia != "0") {
text = body.MediaUrl0;
} else {
text = body.Body;
}
const id = body.From;
const dialogflowResponse = (await sessionClient.detectIntent(text, id, body))
.fulfillmentText;
const twiml = new MessagingResponse();
twiml.message(dialogflowResponse);
res.send(twiml.toString());
});
In this case we define text with let instead of const and outside of the conditional. That way we can reassign to text and use it later in the function too.

Twilio Studio & Functions - Call Whispers

I'm trying to implement a simple call whisper that lets our agent know which phone number/product has been dialed. When a call comes in, it goes through a studio flow where the caller chooses a language. The call is then routed to a taskrouter queue via the "Enqueue" widget. From what I've read in the documentation, I need to pass the callback function a instruction: 'call' parameter in order to be able to specify a URL to be "played" to the agent.
I'm currently limited to implementing everything inside of twilio itself via Studio and Functions.
Why isn't this working, and what specifically do I need to do?
Thanks!
Assignment Callback Function
exports.handler = function(context, event, callback) {
const client = context.getTwilioClient();
let eventInfo = JSON.parse(event.TaskAttributes);
// const response = new Twilio.twiml.VoiceResponse();
console.log(Object.keys(event));
console.log(JSON.parse(event.TaskAttributes));
console.log(Object.keys(JSON.parse(event.TaskAttributes)));
// console.log(JSON.parse(event.WorkerAttributes));
const worker = JSON.parse(event.WorkerAttributes);
// console.log("ReservationSid: " + event.ReservationSid);
// console.log(typeof eventInfo);
// // ATTEMPT 1
// // This works to dequeue a call to a worker, but there is no whisper functionality
// callback(null, {
// 'instruction':'dequeue',
// 'post_work_activity_sid' : 'WORKSPACEACTIVITYSID',
// 'from' : eventInfo.from
// });
// // ATTEMPT 2
// // This works to play the whisper to the agent, but the caller is never connected to the agent.
let callbackURL = 'TWILIOFUNCTIONURL';
callback(null, {
'instruction':'call',
'post_work_activity_sid' : 'WORKSPACEACTIVITYSID',
'from' : eventInfo.from,
'url' : callbackURL
});
// // ATTEMPT 3 - Building a twiml version of attempt #2
// // This doesn't work.
//
// let callbackURL = 'TWILIOFUNCTIONURL';
// let twiml = new Twilio.twiml.VoiceResponse();
// const dial = twiml.dial();
// dial.queue({
// 'instruction':'call',
// // 'post_work_activity_sid' : 'WORKSPACEACTIVITYSID',
// 'from' : eventInfo.from,
// 'accept' : true,
// 'url' : callbackURL,
// // 'reservationSid' : event.ReservationSid
// }, event.selected_language);
// console.log(dial.toString());
// console.log(twiml.toString());
// callback(null, twiml);
// // ATTEMPT 4 - Build a twiml version of attempt #1
// // This doesn't work.
//
// let twiml = new Twilio.twiml.VoiceResponse();
// twiml.dial({
// 'instruction':'dequeue',
// 'post_work_activity_sid' : 'WORKSPACEACTIVITYSID',
// 'from' : eventInfo.from
// // 'to' : 'WORKSPACEQUEUESID'
// });
// console.log(twiml.toString());
// callback(null, twiml);
};
Callback URL containing queue announcement stuff.
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse();
// let client = context.getTwilioClient();
// console.log("BEGIN Queue Announcer - context");
// console.log(Object.keys(context));
// console.log(context);
// console.log("BEGIN Queue Announcer - event");
// console.log(Object.keys(event));
// console.log(event);
// console.log("BEGIN Queue Announcer - client");
// console.log(Object.keys(client));
// console.log(client);
// console.log("END Queue Announcer");
// Random attempt to get the call info
// console.log("BEGIN Queue Announcer - CallSid");
// console.log(event.CallSid);
// client.calls(event.CallSid)
// .fetch()
// .then(call => console.log("Call: " + call));
// console.log("END Queue Announcer - CallSid");
let client = context.getTwilioClient();
twiml.say("QUEUE NAME GOES HERE");
twiml.dial()
.queue({
// 'reservationSid' : event.ReservationSid
}, 'english');
callback(null, twiml);
};
Here's what I did that worked. This is contained within a function that is effectively the "assignment callback URL".
Steps:
Use the 'echo' twimlet to generate the twiml. Modify the supplied URL to include the 'event.RegistrationSid' where appropriate.
Accept the registration via a POST request.
Do the callback, including the 'instruction : call' parameter.
exports.handler = function(context, event, callback) {
const client = context.getTwilioClient();
let eventInfo = JSON.parse(event.TaskAttributes);
// Generate the callback url
let callbackURL = `http://twimlets.com/echo?Twiml=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3CResponse%3E%0A%3CSay%3ECALLWHISPERGOESHERE%3C%2FSay%3E%0A%3CDial%3E%0A%3CQueue%20reservationSid%3D%22${event.ReservationSid}%22%3E%3C%2FQueue%3E%0A%3C%2FDial%3E%0A%3C%2FResponse%3E&`;
// Generate POST request to accept the reservation
const rp = require("request-promise");
const postURI = `https://taskrouter.twilio.com/v1/Workspaces/${event.WorkspaceSid}/Tasks/${event.TaskSid}/Reservations/${event.ReservationSid}`;
const postForm = {
ReservationStatus: "accepted"
};
const postAuth = {
user: context.ACCOUNT_SID,
pass: context.AUTH_TOKEN
};
const options = {
method: "POST",
uri: postURI,
auth: postAuth,
form: postForm
};
rp(options)
.then(body => {
console.log(body);
callback(null, null);
})
.catch(err => {
console.log(err);
callback(null, err);
});
// Call the agent
callback(null, {
instruction: "call",
post_work_activity_sid: "POSTWORKACTIVITYSID",
from: eventInfo.from,
url: callbackURL
});
};

Twilio Studio Not Listing Services

I am setting up a Sync Application using Twilio's Sync Library. For some reason, none of the REST API methods seem to work. That is, I cannot get any of the sync methods to console.log() anything via the runtime functions.
I can, however, console.log() plain text.
Here is my code:
exports.handler = function(context, event, callback) {
// 0. Init
// const phoneNumber = event.phoneNumber;
const issueLimit = 3; // CHANGE IF NEEDED
const listName = 'issues';
const twilioClient = context.getTwilioClient();
// 1. List all lists
twilioClient.sync.services(context.SYNC_SERVICE_SID)
.syncLists
.list({limit: 20})
.then(syncLists => syncLists.forEach(s => console.log(s.sid)));
// 2. return true if quota reached
console.log("Got to here");
// 3. return false
callback(null, undefined);
};
The only code that appears to execute is the 'console.log("Got to here");'. I'm also not receiving any error messages.
Any guidance is sincerely appreciated.
When you see .then(), that's a promise, and you can read more about this here https://www.twilio.com/blog/2016/10/guide-to-javascript-promises.html
In other words, the JavaScript engine goes to your steps 2. and then 3. without waiting for 1. to finish. And since you're returning at step 3 with callback(null, undefined); you won't see the logs.
So, you'll have to move callback() inside the .then(), something like this:
exports.handler = function (context, event, callback) {
// 0. Init
// const phoneNumber = event.phoneNumber;
const issueLimit = 3; // CHANGE IF NEEDED
const listName = 'issues';
const twilioClient = context.getTwilioClient();
// 1. List all lists
twilioClient.sync.services(context.SYNC_SERVICE_SID)
.syncLists
.list({ limit: 20 })
.then(
function (syncLists) {
console.log("Got to here");
syncLists.forEach(s => console.log(s.sid));
callback(null, undefined);
}
);
};

Send Recorded Twilio Audio To Lex

Currently I am able to record user input, pass the recording URL to the needed function, and download the audio file locally. What I am trying to do with the audio file is either get a buffer of it to send to Lex or convert it to the format Lex needs.
Per AWS Documentation the following values are accepted for the input stream param value:
var params = {
botAlias: 'STRING_VALUE', /* required */
botName: 'STRING_VALUE', /* required */
contentType: 'STRING_VALUE', /* required */
inputStream: new Buffer('...') || 'STRING_VALUE' || streamObject, /*required */
userId: 'STRING_VALUE', /* required */
accept: 'STRING_VALUE',
requestAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */,
sessionAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */
};
lexruntime.postContent(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
Per the twilio documentation it looks like the audio file is pretty flexible...
A request to the RecordingUrl will return a recording in binary WAV audio format by default. To request the recording in MP3 format, append ".mp3" to the RecordingUrl.
What do I need to do to get the twilio recorded audio in the right format for Lex? Is it just a matter of building the correct Lex param set or do I need to do some audio conversion before hand? I am writing this application in node js if that helps and I can add more code if it will help.
I was able to figure this out by downloading the file from Twilio as a PCM and changing my parameters a bit. Also, due to the way that Twilio handles the record verb, I needed to transfer the call to a hold state while waiting for the recordingStatusCallback to POST out. I also send a text to the caller with the final status from Lex.
The code I used to download the file:
app.post('/processRecording', (request, response) => {
var https = require('https');
var fs = require('fs');
let callSID = request.body.CallSid;
let url = request.body.RecordingUrl;
var saveFile = new Promise(function(resolve, reject) {
let fileName = callSID+ ".pcm";
var file = fs.createWriteStream(fileName);
var request = https.get(url, function(response) {
response.pipe(file);
resolve();
});
});
});
const accountSid = 'YOUR ACCOUNT SID';
const authToken = 'YOUR AUTH TOKEN';
const client = require('twilio')(accountSid, authToken);
//Once the file is downloaded, I then fetch the call from the hold state using this code:
saveFile.then(function(){
client.calls(callSID)
.update({method: 'POST', url: '/updateCall'})
.then(call => console.log(call.to))
.done();
});
And my updateCall endpoint looks like this:
app.post('/updateCall', (request, response) => {
let lexruntime = new AWS.LexRuntime();
let recordedFileName = request.body.CallSid + '.pcm';
let toNumber = request.body.To;
let fromNumber = request.body.From;
let twiml = new Twilio.twiml.VoiceResponse();
let lexFileStream = fs.createReadStream(recordedFileName);
let sid = request.body.CallSid;
var params = {
botAlias: 'prod', /* required */
botName: 'OrderFlowers', /* required */
contentType: 'audio/lpcm; sample-rate=8000; sample-size-bits=16; channel-count=1; is-big-endian=false',
accept: 'text/plain; charset=utf-8',
userId: sid /* required */
};
params.inputStream = lexFileStream;
lexruntime.postContent(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
if (data.dialogState == "ElicitSlot" || data.dialogState == "ConfirmIntent" || data.dialogState == "ElicitIntent" ){
twiml.say(data.message);
twiml.redirect({
method: 'POST'
}, '/recordVoice');
response.type('text/xml');
response.send(twiml.toString());
}
else if (data.dialogState == "Fulfilled" ){
twiml.say(data.message);
response.type('text/xml');
response.send(twiml.toString());
client.messages.create({
to: toNumber,
from: fromNumber,
body: data.message
}).then(msg => {
}).catch(err => console.log(err));
}
else{
twiml.say(data.message);
response.type('text/xml');
response.send(twiml.toString());
}
});
});
The recordVoice endpoint is actually a Twilio Serverless Function But I think this is what it would look like as an express endpoint:
app.post('/recordVoice', (request, response) => {
let twiml = new Twilio.twiml.VoiceResponse();
twiml.record({
action: '/deadAir',
recordingStatusCallback: '/processRecording',
trim: true,
maxLength: 10,
finishOnKey: '*'
});
twiml.say('I did not receive a recording');
response.type('text/xml');
response.send(twiml.toString());
});
The /deadAir endpoint is also a Twilio Serverless Function but this is what it would look like:
app.post('/deadAir', (request, response) => {
let twiml = new Twilio.twiml.VoiceResponse();
twiml.pause({
length: 60
});
response.type('text/xml');
response.send(twiml.toString());
});

Is it possible to create a tcp client with electron

Is it possible to create a tcp client with electron?
Or can we access the chrome socket api with does that?
https://developer.chrome.com/apps/sockets_tcp
You can use the Node net API in Electron to implement a TCP client.
Try this sample code (don't forget to change IP address) with a little socket server as SocketTest java application for example (HERE).
At the connection, you should see a "World!" string on server side. Try to send this message from server:
{
"nom":"Xplorer",
"prenom":"Yann"
}
And you should see Hello Yann! in your electron console.
'use strict';
const electron = require('electron');
const path = require('path');
const url = require('url');
const net = require('net');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
let mainWindow;
var socketClient
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600,backgroundColor:'#FFFFFF', frame:false})
// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname+'/html/', 'main.html'),
protocol: 'file:',
slashes: true
}))
// Open the DevTools.
//mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
/* Instance socket on create window */
console.log('Try to connect');
socketClient = net.connect({host:'192.16.122.3', port:9042}, () => {
// 'connect' listener
console.log('connected to server!');
socketClient.write('world!\r\n');
});
socketClient.on('data', (data) => {
console.log(data.toString());
var person = JSON.parse(data);
console.log('Hello '+person.prenom+"!");
});
socketClient.on('end', () => {
console.log('disconnected from server');
});
//mainWindow.openDevTools();
}
app.on('before-quit',function(){
socketClient.end();
})
see you.
You can use the Node net API in Electron to implement a TCP client.
It's easy to just test if the chrome API is present. If not I use the node API. So that I have the same code base for my Chrome App and my Electron App.
The 2 APIs are slightly different so I post here how to do it.
let client = null; // node socket
let socketId; // chrome API socket id
function toBuffer(ab) {
return new Buffer(new Uint8Array(ab));
}
function toArrayBuffer(buf) {
return new Uint8Array(buf).buffer;
}
function initConnToServer (ip, port) {
return new Promise((resolve, reject) => {
if(typeof chrome !== 'undefined') {
chrome.sockets.tcp.create({}, r => {
socketId = r.socketId;
chrome.sockets.tcp.connect(r.socketId, ip, port, code => resolve(code));
});
} else {
client = new net.Socket(); // return a Node socket
client.connect(port, ip);
client.on('connect', () => resolve());
}
});
};
function sendToServer_simple (data) {
return new Promise((resolve, reject) => {
if(typeof chrome !== 'undefined') {
chrome.sockets.tcp.send(socketId, data, r => {});
chrome.sockets.tcp.onReceive.addListener(receiveInfo => resolve(receiveInfo.data));
} else {
client.write(toBuffer(data));
client.on('data', data => resolve(toArrayBuffer(data)));
}
});
};

Resources