-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
215 lines (191 loc) Β· 5.95 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
const Twitter = require('twitter')
const Telegraf = require('telegraf')
const debug = require('debug')
const CronJob = require('cron').CronJob
const jsonfile = require('jsonfile')
const log = debug('TTgram:bot')
const logError = debug('TTgram:error')
const actions = require('./lib/actions')
const twitter = require('./lib/twitter')
const bot = new Telegraf(process.env.telegram_token)
var config = jsonfile.readFileSync('config.json')
var users = {}
const loadUsers = () => {
users = {}
config.map((user) => {
users[user.id] = {
log: log,
logError: logError,
bot: bot,
chat_id: user.id,
styles: user.styles,
client: new Twitter({
consumer_key: user.consumerKey,
consumer_secret: user.consumerSecret,
access_token_key: user.accessTokenKey,
access_token_secret: user.accessTokenSecret
})
}
})
}
loadUsers()
bot.use((ctx, next) => {
var id = 0000000
if (ctx.update) {
if (ctx.update.message && ctx.update.message.from) {
id = ctx.update.message.from.id
} else if (ctx.update.callback_query && ctx.update.callback_query.from) {
id = ctx.update.callback_query.from.id
}
}
if (!users[id]) {
if (id.toString() == process.env.admin_id.toString()) {
return next(ctx)
}
return ctx.replyWithMarkdown(`
π Sorry, you do not have access to this bot.
π TTGram is Open Source: [github.com/TiagoDanin/TTgram](https://github.com/TiagoDanin/TTgram)
`)
}
ctx.log = users[id].log
ctx.logError = users[id].logError
ctx.bot = users[id].bot
ctx.styles = users[id].styles
ctx.client = users[id].client
ctx.chat_id = users[id].chat_id
return next(ctx)
})
bot.telegram.sendMessage(process.env.admin_id, '*TTgram starting...*', {
parse_mode: 'Markdown'
})
log('TTgram starting...')
bot.command('ping', (ctx) => {
return ctx.replyWithMarkdown('*Pong*!')
})
bot.command('help', (ctx) => {
return ctx.replyWithMarkdown(`
*Help*!
/new twitter [text] - Create an new twitter
/search [text] - Search tweets
/about - About bot
*Admin*!
/add - Add new user
/rem [telegram id] - Remove user
`)
})
bot.command(['start', 'about'], (ctx) => {
return ctx.replyWithMarkdown(`
*TTgram*
π€ Developer: Tiago Danin (@TiagoEDGE)
π TTGram is Open Source: [github.com/TiagoDanin/TTgram](https://github.com/TiagoDanin/TTgram)
π /help
`)
})
bot.command('add', (ctx) => {
if (ctx.message.from.id.toString() == process.env.admin_id.toString()) {
return ctx.replyWithMarkdown(`
*Reply this message!*
Add new user
---------------------------
Following the format: \`\`\`
Telegram user id
twitter consumer key
twitter consumer secret
twitter access token key
twitter access token secret
\`\`\`
---------------------------
Example: \`\`\`
123456789
absdajshd43288dsau
Djhf789HD98s9a8_fds7f8d7fs
hfdu8&HD_DHUDHUJIi6786sah
dsjahdja432343JHD7das_dffsdf_Dsda7
\`\`\`
`)
}
return ctx.replyWithMarkdown('*You are not admin of bot*!')
})
bot.hears(/^\/rem\s(.*)/i, (ctx) => {
if (ctx.message.from.id.toString() == process.env.admin_id.toString()) {
config = config.reduce((total, user) => {
if (user.id != ctx.match[1]) {
total.push(user)
}
return total
}, [])
loadUsers()
jsonfile.writeFileSync('config.json', config, { replacer: true })
return ctx.replyWithMarkdown('*User removed*!')
}
return ctx.replyWithMarkdown('*You are not admin of bot*!')
})
bot.hears(/^\/[new_\s]*twitter[s]* (.*)/i, async(ctx) => {
var post = await actions.create(ctx)
if (post.error) {
var error = post.error
return ctx.replyWithHTML(`*ERROR*!\n*Code*: ${error[0].code}\n*Message*: ${error[0].message}`)
}
return twitter.sendTwitter(ctx, post)
})
bot.hears(/^\/get[s]*|\/[new_\s]*twitter[s]*$/i, (ctx) => {
return twitter.getTimeLine(ctx)
})
bot.hears(/^\/search\s(.*)/i, (ctx) => {
return twitter.getSearch(ctx)
})
bot.on('message', (ctx) => {
if (ctx.message.reply_to_message && ctx.message.reply_to_message.text) {
var replyMsg = ctx.message.reply_to_message.text
if (replyMsg.match('Reply this message!') && ctx.message.from.id.toString() == process.env.admin_id.toString()) {
var params = (ctx.message.text || '').split('\n')
if (params.length != 5) {
return ctx.replyWithMarkdown('*Invalid user...*, use /add again.')
}
config.push({
id: params[0],
consumerKey: params[1],
consumerSecret: params[2],
accessTokenKey: params[3],
accessTokenSecret: params[4],
styles: {
user: 'tg',
text: 'default',
url: 'default',
keyboard: 'count',
noPreviewLink: true
}
})
loadUsers()
jsonfile.writeFileSync('config.json', config, { replacer: true })
return ctx.replyWithMarkdown('*User added*')
}
}
})
bot.on('callback_query', (ctx) => {
var data = ctx.callbackQuery.data.split(':')
ctx.id = data[1]
if (data[0] == 'love') {
actions.like(ctx)
ctx.answerCbQuery('Favorited β€')
} else if (data[0] == 'unlove') {
actions.unlike(ctx)
ctx.answerCbQuery('Unfavored π')
} else if (data[0] == 'rt') {
actions.rt(ctx)
ctx.answerCbQuery('Retweeted π')
} else if (data[0] == 'unrt') {
actions.unrt(ctx)
ctx.answerCbQuery('Undone β')
}
return twitter.getTwitter(ctx)
})
bot.catch((err) => {
logError(`Oooops ${err}`)
})
bot.startPolling()
new CronJob(process.env.cron_job, function() {
for (var id in users) {
twitter.getTimeLine(users[id])
}
}, null, true, 'America/Los_Angeles')