getToken() { return new Promise((resolve, reject) => { var appName = this.botConfig.apiaiAccessToken; var deviceId = "server"; // Create a unique ID for the client on their current device var endpointId = "TwilioChat:" + this.botConfig.botIdentity + ":browser"; // Create a "grant" which enables a client to use IPM as a given user, // on a given device var ipmGrant = new IpMessagingGrant({ serviceSid: this.botConfig.serviceSid, endpointId: endpointId }); // Create an access token which we will sign and return to the client, // containing the grant we just created var token = new AccessToken( this.botConfig.accountSid, this.botConfig.signingKeySid, this.botConfig.signingKeySecret ); token.addGrant(ipmGrant); token.identity = this.botConfig.botIdentity; resolve(token.toJwt()); }); }
function sendRiderSMS(rideReq, rider, driver, meetingPoint) { var notificationMessage = 'Hey ' + rider.firstName + ', you will meet ' + driver.firstName + ' at ' + meetingPoint + ', ' + rideReq.area + ' on ' + rideReq.date + ', ' + rideReq.time + '. Please be on time! - Erkab'; client.messages .create({ to: '+2' + rider.mobileNum, from: '+16024564295', body: notificationMessage, }) .then((message) => console.log(message.sid)); }
// Authentication Recording app.post('/authenticate', function(req, res) { var twiml = new VoiceResponse(); utilities.speak(twiml, 'Please say the following phrase to authenticate '); utilities.speak(twiml, config.chosenVoicePrintPhrase, config.contentLanguage); twiml.record({ action: '/process_authentication', maxLength: '5', trim: 'do-not-trim', }); res.type('text/xml'); res.send(twiml.toString()); });
const postMessage = async (req, res) => { const { message, phone } = req.body; if (!message || !phone) return res.json('Missing message or phone'); // Send an SMS with the message const result = await sendMessage(message, phone); // log the SMS id console.log(result.sid); // Saving the SMS in database await SMS.create({ Body: req.body.message, From: req.body.phone }); res.redirect('/'); }
// Enrollment Recording app.post('/enroll', function(req, res) { const enrollCount = req.query.enrollCount || 0; const twiml = new VoiceResponse(); utilities.speak(twiml, 'Please say the following phrase to enroll '); utilities.speak(twiml, config.chosenVoicePrintPhrase, config.contentLanguage); twiml.record({ action: '/process_enrollment?enrollCount=' + enrollCount, maxLength: 5, trim: 'do-not-trim' }); res.type('text/xml'); res.send(twiml.toString()); });
app.post('/token/:identity', (request, response) => { const identity = request.params.identity; const accessToken = new AccessToken(config.twilio.accountSid, config.twilio.apiKey, config.twilio.apiSecret); const chatGrant = new ChatGrant({ serviceSid: config.twilio.chatServiceSid, endpointId: `${identity}:browser` }); accessToken.addGrant(chatGrant); accessToken.identity = identity; response.set('Content-Type', 'application/json'); response.send(JSON.stringify({ token: accessToken.toJwt(), identity: identity })); })
admin.auth().getUser(phone) .then(userRecord => { const code = Math.floor(Math.random() * 8999 + 1000); twilio.messages.create({ body: 'Your code is ' + code, to: phone, from: '13312534741' }, (err) => { if (err) { return res.status(422).send(err); } admin.database().ref('users/' + phone) .update({ code: code, codeValid: true }, () => { res.send({ success: true }); }); }); return true; }) .catch((err) => { res.status(422).send({ error: err }); });
/** * API to lister to call forward request to agent * * @param {Object} req - request object * @param {Object} res - response object **/ app.post('/connect-agent', (req, res) => { const twiml = new twilio.twiml.VoiceResponse(); twiml.dial({ action: '/handle-dial-call-status', method: 'POST', timeLimit: 60, callerId: req.body.To }, '<TEST_PHONE_NUMBER_TO_RECEIVE_INCOMING_CALLS>'); res.type('text/xml'); res.send(twiml.toString()); });
app.post('/api/messages', (req, res) => { res.header('Content-Type', 'application/json'); client.messages .create({ from: process.env.TWILIO_PHONE_NUMBER, to: req.body.to, body: req.body.body }) .then(() => { res.send(JSON.stringify({ success: true })); }) .catch(err => { console.log(err); res.send(JSON.stringify({ success: false })); }); });
// This route is called by Twilio to receive TwiML instructions for // voice calls. The Twilio API for voice calling requires that a URL be // specified to obtain the TwiML code it needs to generate the voice message. // In order to put our message content into a voice call we give Twilio // a URL pointing to our own server, which contains the message text encoded // in it. When the call is answered, Twilio sends a request to this // URL, and our server responds with TwiML containing the decoded message text // and the configured voice for the text-to-speech conversion. router.post('/message/:message', function(req, res, next) { const message = decodeURIComponent(req.params.message) console.log('Making TwiML for voice message') const twiml = new VoiceResponse() twiml.say({ voice: config.voice }, message) res.type('text/xml') res.send(twiml.toString()) })
/** * API to lister to call forward request * * @param {Object} req - request object * @param {Object} res - response object **/ app.post('/call-forward', (req, res) => { io.sockets.emit('incoming_call', { callInfo: req.body }); const twiml = new twilio.twiml.VoiceResponse(); twiml.dial({ action: '/handle-dial-call-status', method: 'POST', timeLimit: 60 }, '<TEST_PHONE_NUMBER_TO_RECEIVE_INCOMING_CALLS>'); res.type('text/xml'); res.send(twiml.toString()); });
function sendDriverSMS(rideReq, driver, rider, meetingPoint) { var notificationMessage = 'Hey ' + driver.firstName + ', you will meet ' + rider.firstName + ' at ' + meetingPoint + ', ' + rideReq.area + ' on ' + rideReq.date + ', ' + rideReq.time + '. Please be on time! - Erkab'; client.messages .create({ to: '+2' + driver.mobileNum, from: '+16024564295', body: notificationMessage, }) .then((message) => console.log(message.sid)); }