This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
167 lines (136 loc) · 4.1 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
const express = require('express')
const bodyParser = require('body-parser')
const passport = require('passport')
const uuid = require('uuid/v4')
const security = require('./helpers/security')
const auth = require('./helpers/auth')
const cacheRoute = require('./helpers/cache-route')
const socket = require('./helpers/socket')
const app = express()
const TIE = require('@artificialsolutions/tie-api-client');
var request = require('request')
require('dotenv').config();
OAuth= require('oauth').OAuth
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(passport.initialize());
require('dotenv').config();
// twitter authentication
var twitter_oauth = {
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
token: process.env.TWITTER_ACCESS_TOKEN,
token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
}
var appconfig = {
teneoURL: process.env.TENEO_ENGINE_URL,
port: process.env.PORT
}
const teneoApi = TIE.init(appconfig.teneoURL);
// Initialize session handler to store mapping between a Twitter UserID and an Engine session ID.
const sessionHandler = SessionHandler();
app.set('port', (appconfig.port || 5000))
// start server
const server = app.listen(app.get('port'), function() {
console.log('Node app is running on port: ', app.get('port'))
initTwitterOauth()
})
function getDirectMessageRequestOptions(recipientID, messageText){
var dmParameters = {
"event": {
"type": "message_create",
"message_create": {
"target": {
"recipient_id": recipientID
},
"message_data": {
"text": messageText
}
}
}
}
var requestOptions = {
url: 'https://api.twitter.com/1.1/direct_messages/events/new.json',
oauth: twitter_oauth,
json: true,
headers: {
'content-type': 'application/json'
},
body: dmParameters
}
return requestOptions
}
function sendDM(recipientID, messageText){
// POST request to send Direct Message
request.post(getDirectMessageRequestOptions(recipientID, messageText), function (error, response, body) {
if(error){
console.log(error)
}
})
}
OAuth= require('oauth').OAuth
var oa
// We init OAuth with our consumer key & secret just like with passport
function initTwitterOauth() {
//Generate random 'nonce' string for this request
var nonce = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for(var i = 0; i < 32; i++) {
nonce += possible.charAt(Math.floor(Math.random() * possible.length));
}
oa = new OAuth(
"https://twitter.com/oauth/request_token"
, "https://twitter.com/oauth/access_token"
, twitter_oauth.consumer_key
, twitter_oauth.consumer_secret
, "1.0A"
, nonce
, "HMAC-SHA1"
);
}
// Use OAuth authentication with token and secret to tweet
// https://dev.twitter.com/docs/api/1/post/statuses/update
function makeTweet(replyToTweetID, newTweetText, cb) {
oa.post(
"https://api.twitter.com/1.1/statuses/update.json"
, twitter_oauth.token
, twitter_oauth.token_secret
, { "status": newTweetText, "in_reply_to_status_id": replyToTweetID }
, cb
);
}
// initialize socket.io
socket.init(server)
// form parser middleware
var parseForm = bodyParser.urlencoded({ extended: false })
/**
* Activity view
**/
app.get('/activity', auth.basic, require('./routes/activity'))
/**
* Handles Twitter sign-in OAuth1.0a callbacks
**/
app.get('/callbacks/:action', passport.authenticate('twitter', { failureRedirect: '/' }),
require('./routes/sub-callbacks'))
/***
* SESSION HANDLER
***/
function SessionHandler() {
// Map a Twitter UserID to the Teneo Engine Session ID.
// This code keeps the map in memory, which is ok for demo purposes
// For production usage it is advised to make use of more resilient storage mechanisms like redis
const sessionMap = new Map();
return {
getSession: (userId) => {
if (sessionMap.size > 0) {
return sessionMap.get(userId);
}
else {
return "";
}
},
setSession: (userId, sessionId) => {
sessionMap.set(userId, sessionId)
}
}
}