-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
332 lines (298 loc) · 11.4 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
'use strict'
const cote = require('cote')
const util = require('./util')
const u = require('elife-utils')
const comment = require('./tweet/comment')
const retweet = require('./tweet/retweet')
const like = require('./tweet/like')
const follow = require('./tweet/follow')
const unfollow = require('./tweet/unfollow')
/* microservice key (identity of the microservice) */
let msKey = 'eskill_tweet'
/* understand/
* This is the main entry point where we start.
*
* outcome/
* Start our microservice.
*/
function main() {
startMicroservice()
registerWithCommMgr()
loadCrediential()
}
const ssbClient = new cote.Requester({
name: 'Tweet Job Skill -> SSB client ',
key: 'everlife-ssb-svc',
})
const levelDBClient = new cote.Requester({
name: 'Tweet Job Skill -> Level DB Client',
key: 'everlife-db-svc',
})
let auth
const commonerr = 'Twitter Job will not work'
const errmsg = {
LEVELERR: `Error retriving your twitter credentials! If you have not set them, please do so by clicking skill tab and eskill-tweet otherwise ${commonerr}`,
DECRYPTERR: `Error decrypting your twitter credentials! ${commonerr}`,
PARSEERR: `Error loading your twitter credentials! ${commonerr}`,
}
function loadCrediential(){
levelDBClient.send({type:'get', key: 'eskill-tweet'},(err, data) => {
if(err) {
u.showErr(err)
sendReply(errmsg.LEVELERR, { USELASTCHAN: true })
} else {
ssbClient.send({type: 'decrypt-text', text: data },(err, data) => {
if(err) {
u.showErr(err)
sendReply(errmsg.DECRYPTERR, { USELASTCHAN: true })
} else {
try {
auth = JSON.parse(data)
} catch (e) {
u.showErr(e)
sendReply(errmsg.PARSEERR, { USELASTCHAN: true })
}
}
})
}
})
}
const commMgrClient = new cote.Requester({
name: 'Tweet Job Skill -> CommMgr',
key: 'everlife-communication-svc',
})
function sendReply(msg, req) {
req.type = 'reply'
req.msg = String(msg)
commMgrClient.send(req, (err) => {
if(err){
u.showErr('eskill-tweet:')
u.showErr(err)
}
})
}
function startMicroservice() {
/* understand/
* The microservice (partitioned by key to prevent
* conflicting with other services).
*/
const svc = new cote.Responder({
name: 'Tweet Job',
key: msKey,
})
/* outcome/
* Respond to user messages asking us to code/decode things
*/
svc.on('msg', (req, cb) => {
if(!req.msg) return cb()
if(req.msg.startsWith('/tweet')){
cb(null, true)
if(!auth || !auth.username || !auth.password){
sendReply(errmsg.LEVELERR, req)
}
else {
let tweet_msg = req.msg.substr('/tweet'.length).trim()
if(tweet_msg.length > 280) {
sendReply('Tweet message character limit exists. Try with a different message.',req)
}else {
util.tweet(auth.username, auth.password, tweet_msg)
.then((result) => {
if(result && result.success)
sendReply("Tweeted successfully.", req)
else
sendReply('Tweet failed.', req)
})
.catch((err) => {
u.showErr(err)
sendReply('Tweet failed..', req)
})
}
}
} else if(req.msg.startsWith('/retweet')){
cb(null, true)
if(!auth || !auth.username || !auth.password){
sendReply(errmsg.LEVELERR, req)
}else {
let tweet_statusId = req.msg.substr('/retweet'.length).trim()
retweet.reTweet(auth.username,auth.password,tweet_statusId)
.then((result)=>{
if(result && result.success)
sendReply("Retweeted successfully.",req)
else
sendReply('Retweet failed.',req)
})
.catch((err) =>{
u.showErr(err)
sendReply('Retweet failed..',req)
})
}
}else if(req.msg.startsWith('/like')){
cb(null, true)
if(!auth || !auth.username || !auth.password){
sendReply(errmsg.LEVELERR, req)
}else {
let like_statusId = req.msg.substr('/like'.length).trim()
like.likeButton(auth.username,auth.password,like_statusId)
.then((result)=>{
if(result && result.success)
sendReply("Liked successfully.",req)
else
sendReply('Like failed.',req)
})
.catch((err) =>{
u.showErr(err)
sendReply('Like failed..',req)
})
}
}else if(req.msg.startsWith('/comment')){
cb(null, true)
if(!auth || !auth.username || !auth.password){
sendReply(errmsg.LEVELERR, req)
}else {
let x= req.msg.substr('/comment'.length).trim()
let status_Id=x.substr(0,x.indexOf(' '))
let tweet_comment=x.substr(x.indexOf(' ')+1);
comment.commentTweet(auth.username,auth.password,status_Id,tweet_comment)
.then((result)=>{
if(result && result.success)
sendReply("Commented tweet successfully.",req)
else
sendReply('Comment failed.',req)
})
.catch((err) =>{
u.showErr(err)
sendReply('failed..',req)
})
}
}else if(req.msg.startsWith('/following')){
cb(null, true)
if(!auth || !auth.username || !auth.password){
sendReply(errmsg.LEVELERR, req)
}else {
let follow_Id = req.msg.substr('/following'.length).trim()
follow.follow(auth.username,auth.password,follow_Id)
.then((result)=>{
if(result && result.success)
sendReply("Followed successfully.",req)
else
sendReply('Follow failed.',req)
})
.catch((err) =>{
u.showErr(err)
sendReply('Follow failed..',req)
})
}
}else if(req.msg.startsWith('/unfollowing')){
cb(null, true)
if(!auth || !auth.username || !auth.password){
sendReply(errmsg.LEVELERR, req)
}else {
let unfollow_Id = req.msg.substr('/unfollowing'.length).trim()
unfollow.unfollow(auth.username,auth.password,unfollow_Id)
.then((result)=>{
if(result && result.success)
sendReply("Unfollowed successfully.",req)
else
sendReply('Unfollow failed.',req)
})
.catch((err) =>{
u.showErr(err)
sendReply('Unfollow failed..',req)
})
}
}
else{
cb()
}
})
svc.on('task', (req, cb) => {
try{
let data = JSON.parse(util.getData(req.task));
console.log(data)
if(!auth || !auth.username || !auth.password) cb('Twitter credentials missing.')
else if(data.type == 'tweet'){
util.tweet(auth.username, auth.password, data.message)
.then((result)=>{
cb(null, req.task, result)
})
.catch((err) => {
u.showErr(err)
cb('Something went wrong.')
})
}
else if(data.type == 'retweet'){
console.log(data.uri)
retweet.reTweet(auth.username, auth.password, data.uri)
.then((result)=>{
console.log(result)
cb(null, req.task, result)
})
.catch((err) => {
u.showErr(err)
cb('Something went wrong.')
})
}
else if(data.type == 'comment'){
comment.commentTweet(auth.username, auth.password,data.uri,data.comment)
.then((result)=>{
cb(null, req.task, result)
})
.catch((err) => {
u.showErr(err)
cb('Something went wrong.')
})
}
else if(data.type == 'like'){
like.likeButton(auth.username, auth.password, data.uri)
.then((result)=>{
cb(null, req.task, result)
})
.catch((err) => {
u.showErr(err)
cb('Something went wrong.')
})
}
else if(data.type == 'follow'){
follow.follow(auth.username, auth.password, data.uri)
.then((result)=>{
cb(null, req.task, result)
})
.catch((err) => {
u.showErr(err)
cb('Something went wrong.')
})
}
else if(data.type == 'unfollow'){
unfollow.unfollow(auth.username, auth.password, data.uri)
.then((result)=>{
cb(null, req.task, result)
})
.catch((err) => {
u.showErr(err)
cb('Something went wrong.')
})
}
}catch(e){
console.log(e)
cb('Something went wrong')
}
})
}
function registerWithCommMgr() {
commMgrClient.send({
type: 'register-msg-handler',
mskey: msKey,
mstype: 'msg',
mshelp: [
{cmd: '/tweet', txt: 'Send a Tweet' },
{cmd: '/retweet', txt: 'For retweet'},
{cmd: '/comment', txt: 'For commenting a tweet'},
{cmd: '/like', txt: 'For like a tweet'},
{cmd: '/following', txt: 'For following '},
{cmd: '/unfollowing', txt: 'For unfollowing '},
],
}, (err) => {
if(err) u.showErr(err)
})
}
main()