-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-sms.js
55 lines (45 loc) · 1.7 KB
/
send-sms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const http = require('http');
const express = require('express');
const MessagingResponse = require('twilio').twiml.MessagingResponse;
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({extended:false}));
// const accountSid = 'XXXXXXX';
// const authToken = 'XXXXXXXXX';
const client = require('twilio')(accountSid, authToken);
const deep_cell = '+16477873102';
const whatsapp_num = '+14155238886';
// Function used to send message to deep_cell through whatsapp
function sendToWhatsapp(message){
client.messages.create({
to: "whatsapp:" + deep_cell,
from: "whatsapp:" + whatsapp_num,
body: message,
//mediaUrl: "https://i.cbc.ca/1.3463873.1456419440!/fileImage/httpImage/image.jpg_gen/derivatives/16x9_780/trufa-meme.jpg"
}).then(message => {
callback(null, message.sid);
}).catch(err => callback(err));
}
// Endpoint hit for all incoming messages
app.post('/sms', (req, res)=>{
const message = req.body.Body;
const numImg = req.body.NumMedia;
console.log('Incoming message: ' + message);
console.log('Number of images: ' + numImg);
// If image was attached, insert into mongo
if(numImg !== '0'){
// URL of image
const mediaUrl = req.body.MediaUrl0;
console.log('MediaURL: ' + mediaUrl);
}
// TODO: DO SOMETHING WITH RESPONSE
// Response
const twiml = new MessagingResponse();
twiml.message('Twilio has received your message');
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
});
http.createServer(app).listen(1337, ()=> {
console.log('Express server listening on port 1337');
});
//sendToWhatsapp('HELLO ANDY JIANG');