Skip to content

Instantly share code, notes, and snippets.

@victorpaulo
Last active August 28, 2019 20:10
Show Gist options
  • Save victorpaulo/e7070a2d76256b03cdc38be81a7165cb to your computer and use it in GitHub Desktop.
Save victorpaulo/e7070a2d76256b03cdc38be81a7165cb to your computer and use it in GitHub Desktop.
IBM Watson Assistant & Twilio Sandbox
require('dotenv').config();
var AssistantV1 = require('ibm-watson/assistant/v1');
const clientTwilio = require('twilio')(process.env.accountSid, process.env.authToken);
const express = require('express');
const bodyParser = require('body-parser');
// Constants
const port = process.env.PORT || 8080;
// App
const app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// This is the entry point for the Twilio callback
app.post('/', function (req, res) {
//Watson Assistant
var assistant = new AssistantV1({
iam_apikey: process.env.WATSON_API,
url: 'https://gateway.watsonplatform.net/assistant/api/',
version: '2018-02-16'
});
var inputMsg = { text: req.body.Body }
var msg = {
input: inputMsg,
workspace_id: process.env.WORKSPACE_ID
}
assistant.message(msg)
.then(result => {
console.log("watson result ", JSON.stringify(result));
//sending the Watson Assistant response message to twilio to be sent to whatsapp
clientTwilio.messages
.create({
body: result.output.text[0],
from: 'whatsapp:+141.....6', //get this number from your twilio whatsapp sandbox console
to: req.body.From,
})
.then(message => console.log(message.sid))
.done();
}).catch(err => {
console.log(err);
});
});
//Server listening on port 8080
var server = app.listen(port, function () {
console.log('Server running at http://127.0.0.1:' + port + '/');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment