-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathindex.js
209 lines (193 loc) · 7.35 KB
/
index.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
209
//@ts-check
const { BotFrameworkAdapter } = require("botbuilder");
const restify = require("restify");
const { config } = require("dotenv");
const { TwitterAdapter, TwitterSubscriptionManager, TwitterWebhookManager } = require("@botbuildercommunity/adapter-twitter");
config();
const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log(`${server.name} listening to ${server.url}`);
});
const adapter = new BotFrameworkAdapter({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
const twitterAdapter = new TwitterAdapter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN,
access_token_secret: process.env.TWITTER_TOKEN_SECRET,
screen_name: process.env.TWITTER_APPLICATION_USERNAME
});
server.post("/api/messages", (req, res) => {
adapter.processActivity(req, res, async (context) => {
console.log(context.activity.text);
});
});
server.post("/api/twitter/messages", (req, res) => {
twitterAdapter.processActivity(req, res, async (context) => {
if(context.activity.type === "message") {
await context.sendActivity("Posting a tweet from the adapter!");
}
});
});
/*
* This endpoint is hit by Twitter with the crc_token.
* Twitter's Activity API expects a formatted response in return.
* The TwitterAdapter has utility methods to handle this.
* The `processWebhook()` method checks for the crc_token and creates the appropriate response.
* This response data should be sent via the Restify or Express response.
*/
server.get('/api/twitter/messages', (req, res) => {
try {
const webHookResponse = TwitterWebhookManager.processWebhook(req, process.env.TWITTER_CONSUMER_SECRET);
res.send(webHookResponse);
}
catch(e) {
res.status(500);
res.send({ error: e });
}
});
/*
* This endpoint is for registering the webhook URL where Twitter will send the messages.
* This is only for an example. If this is a production application, you should secure this.
*/
server.get('/api/twitter/webhook', async (req, res) => {
try {
const webhookID = await TwitterWebhookManager.registerWebhook(twitterAdapter.client, process.env.TWITTER_ACTIVITY_ENV, process.env.TWITTER_WEBHOOK_URL);
res.send({ webhookID: webhookID });
}
catch(e) {
res.status(500);
res.send({ error: e });
}
});
/*
* This endpoint is for listing the webhooks associated with your Twitter Activity API
* This is only for an example. If this is a production application, you should secure this.
*/
server.get('/api/twitter/webhook/list', async (req, res) => {
try {
const webhooks = await TwitterWebhookManager.listWebhooks(process.env.TWITTER_CONSUMER_KEY, process.env.TWITTER_CONSUMER_SECRET, process.env.TWITTER_ACTIVITY_ENV);
res.send({ webhooks: webhooks });
}
catch(e) {
res.status(500);
res.send({ error: e });
}
});
/*
* This endpoint is for updating a deactivated webhook associated with your Twitter Activity API
* This is only for an example. If this is a production application, you should secure this.
*/
server.get('/api/twitter/webhook/update', async (req, res) => {
const webhooks = await TwitterWebhookManager.listWebhooks(process.env.TWITTER_CONSUMER_KEY, process.env.TWITTER_CONSUMER_SECRET, process.env.TWITTER_ACTIVITY_ENV);
if(webhooks.length > 0) {
const webhookID = webhooks[0].id;
try {
const success = await TwitterWebhookManager.updateWebhook(
process.env.TWITTER_CONSUMER_KEY,
process.env.TWITTER_CONSUMER_SECRET,
process.env.TWITTER_ACCESS_TOKEN,
process.env.TWITTER_TOKEN_SECRET,
process.env.TWITTER_ACTIVITY_ENV,
webhookID);
res.send({ success: success });
}
catch(e) {
res.status(500);
res.send({ error: e });
}
}
else {
res.send({ message: 'No webhooks registered.' });
}
});
/*
* This endpoint is removing a webhook associated with your Twitter Activity API
* This is only for an example. If this is a production application, you should secure this.
*/
server.get('/api/twitter/webhook/remove', async (req, res) => {
const webhooks = await TwitterWebhookManager.listWebhooks(process.env.TWITTER_CONSUMER_KEY, process.env.TWITTER_CONSUMER_SECRET, process.env.TWITTER_ACTIVITY_ENV);
if(webhooks.length > 0) {
const webhookID = webhooks[0].id;
try {
const success = await TwitterWebhookManager.removeWebhook(
process.env.TWITTER_CONSUMER_KEY,
process.env.TWITTER_CONSUMER_SECRET,
process.env.TWITTER_ACCESS_TOKEN,
process.env.TWITTER_TOKEN_SECRET,
process.env.TWITTER_ACTIVITY_ENV,
webhookID);
res.send({ success: success });
}
catch(e) {
res.status(500);
res.send({ error: e });
}
}
else {
res.send({ message: 'No webhooks registered.' });
}
});
/*
* This endpoint is for kicking off the subscription process.
* It will first check that the current credentials in your enviroment are registered to use
* the Activity API. If not, it will subscribe that account.
* If this was a production application, you'd probably want better security on this endpoint.
*/
server.get('/api/twitter/subscription', async (req, res) => {
try {
const result = await TwitterSubscriptionManager.manageSubscription(
process.env.TWITTER_CONSUMER_KEY,
process.env.TWITTER_CONSUMER_SECRET,
process.env.TWITTER_ACCESS_TOKEN,
process.env.TWITTER_TOKEN_SECRET,
process.env.TWITTER_ACTIVITY_ENV);
res.send({ success: result });
}
catch(e) {
res.status(500);
res.send({ error: e });
}
});
/*
* This endpoint is for deleting a subscription associated with your Twitter Activity API
* This is only for an example. If this is a production application, you should secure this.
*/
server.get('/api/twitter/subscription/remove', async (req, res) => {
try {
const success = await TwitterSubscriptionManager.removeSubscription(
process.env.TWITTER_CONSUMER_KEY,
process.env.TWITTER_CONSUMER_SECRET,
process.env.TWITTER_ACTIVITY_ENV,
process.env.TWITTER_APPLICATION_USERNAME
);
res.send({ success: success });
}
catch(e) {
res.status(500);
res.send({ error: e });
}
});
/*
* This endpoint is for listing the subscriptions associated with your Twitter Activity API
* This is only for an example. If this is a production application, you should secure this.
*/
server.get('/api/twitter/subscription/list', async (req, res) => {
try {
const subs = await TwitterSubscriptionManager.listSubscriptions(
process.env.TWITTER_CONSUMER_KEY,
process.env.TWITTER_CONSUMER_SECRET,
process.env.TWITTER_ACTIVITY_ENV
);
res.send({ subs: subs });
}
catch(e) {
res.status(500);
res.send({ error: e });
}
});
server.get('/', (req, res) => {
res.send({ message: 'This service is up.' });
});