-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
208 lines (167 loc) · 6.07 KB
/
app.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import express from 'express';
import crypto from 'crypto';
import bodyParser from 'body-parser';
import zipcode from 'zipcode';
import request from "request";
import Twitter from 'twitter';
// Watson Work Services URL
const watsonWork = "https://api.watsonwork.ibm.com";
// Application Id, obtained from registering the application at https://developer.watsonwork.ibm.com
const appId = process.env.TWITTER_CLIENT_ID;
// Application secret. Obtained from registration of application.
const appSecret = process.env.TWITTER_CLIENT_SECRET;
// Webhook secret. Obtained from registration of a webhook.
const webhookSecret = process.env.TWITTER_WEBHOOK_SECRET;
// Twitter API keys obtained via: https://apps.twitter.com (see README for more info)
// a JSON object with all the authentication info for Twitter
const twitter_auth = {
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
}
// Keyword to "listen" for when receiving outbound webhook calls.
const webhookKeyword = "@twitter";
const failMessage =
`Hey, maybe it's me... maybe it's Twitter, but I sense the fail whale should be here... Try again later`;
const successMessage = (username, tweetText, tweetId) => {
return `*Tweet* from [@${username}](http://twitter.com/${username}): ${tweetText}. Click [here](https://twitter.com/${username}/status/${tweetId}) to view more. \r\n\r\n`;
};
const app = express();
const client = new Twitter(twitter_auth);
// Send 200 and empty body for requests that won't be processed.
const ignoreMessage = (res) => {
res.status(200).end();
}
// Process webhook verification requests
const verifyCallback = (req, res) => {
console.log("Verifying challenge");
const bodyToSend = {
response: req.body.challenge
};
// Create a HMAC-SHA256 hash of the recieved body, using the webhook secret
// as the key, to confirm webhook endpoint.
const hashToSend =
crypto.createHmac('sha256', webhookSecret)
.update(JSON.stringify(bodyToSend))
.digest('hex');
res.set('X-OUTBOUND-TOKEN', hashToSend);
res.send(bodyToSend).end();
};
// Validate events coming through and process only message-created or verification events.
const validateEvent = (req, res, next) => {
// Event to Event Handler mapping
const processEvent = {
'verification': verifyCallback,
'message-created': () => next()
};
// If event exists in processEvent, execute handler. If not, ignore message.
return (processEvent[req.body.type]) ?
processEvent[req.body.type](req, res) : ignoreMessage(res);
};
// Authenticate Application
const authenticateApp = (callback) => {
// Authentication API
const authenticationAPI = 'oauth/token';
const authenticationOptions = {
"method": "POST",
"url": `${watsonWork}/${authenticationAPI}`,
"auth": {
"user": appId,
"pass": appSecret
},
"form": {
"grant_type": "client_credentials"
}
};
request(authenticationOptions, (err, response, body) => {
// If can't authenticate just return
if (response.statusCode != 200) {
console.log("Error authentication application. Exiting.");
process.exit(1);
}
callback(JSON.parse(body).access_token);
});
};
// Send message to Watson Workspace
const sendMessage = (spaceId, message) => {
// Spaces API
const spacesAPI = `v1/spaces/${spaceId}/messages`;
// Photos API
const photosAPI = `photos`;
// Format for sending messages to Workspace
const messageData = {
type: "appMessage",
version: 1.0,
annotations: [
{
type: "generic",
version: 1.0,
color: "#1DA1F2",
title: "Results from Twitter",
text: message
}
]
};
// Authenticate application and send message.
authenticateApp( (jwt) => {
const sendMessageOptions = {
"method": "POST",
"url": `${watsonWork}/${spacesAPI}`,
"headers": {
"Authorization": `Bearer ${jwt}`
},
"json": messageData
};
request(sendMessageOptions, (err, response, body) => {
if(response.statusCode != 201) {
console.log("Error posting twitter information.");
console.log(response.statusCode);
console.log(err);
}
});
});
};
// Ensure we can parse JSON when listening to requests
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.send('IBM Watson Workspace app for Twitter is alive and happy!');
});
// This is callback URI that Watson Workspace will call when there's a new message created
app.post('/webhook', validateEvent, (req, res) => {
// Check if the first part of the message is '@twitter'.
// This lets us "listen" for the '@twitter' keyword.
if (req.body.content.indexOf(webhookKeyword) != 0) {
ignoreMessage(res);
return;
}
// Send status back to Watson Work to confirm receipt of message
res.status(200).end();
// Id of space where outbound event originated from.
const spaceId = req.body.spaceId;
// Parse twitter query from message body.
// Expected format: <keyword> <twitter query>
const twitterQuery = req.body.content.split(' ')[1];
console.log('Getting Twitter results: \'' + twitterQuery + '\'');
client.get('search/tweets', {q: twitterQuery}, function(err, tweets, response) {
// If error, send message to Watson Workspace with failure message
if (err) {
sendMessage(spaceId, failMessage, res);
return ;
}
var resultCount = tweets.statuses.length;
var messageToPost = "";
// return up to 3 tweets
var tweetCount = resultCount > 3 ? 3 : resultCount;
for (var i = 0; i < tweetCount; i++) {
messageToPost += successMessage(tweets.statuses[i].user.screen_name, tweets.statuses[i].text, tweets.statuses[i].id_str);
}
console.log("Posting recent twitter search results back to space");
sendMessage(spaceId, messageToPost);
return;
});
});
// Kickoff the main process to listen to incoming requests
app.listen(process.env.PORT || 3000, () => {
console.log('Twitter app is listening on the port');
});