-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
321 lines (249 loc) · 8.68 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
import { Bot } from 'grammy'
import dotenv from 'dotenv'
import User from './model/user.js'
import mongoose from 'mongoose'
import Alert from './model/alert.js'
import {
fetchCoinsDetails,
coinsDetails,
availableCoins,
getPriceHistory,
priceHistory
} from './service/marketDataService.js'
import {
sendPriceAlerts
} from './service/alertsService.js'
import {
bot,
initializeBot
} from './config/bot.js'
import {
fetchTweets,
sendTwitterData
} from './service/twitterService.js'
dotenv.config()
const dbConnnectionOptions = {
maxPoolSize: 5,
minPoolSize: 1,
socketTimeoutMS: 100000
}
await mongoose.connect(process.env.MONGO_URI, dbConnnectionOptions)
.then(async (res) => {
console.log("connected to database")
})
.catch((error) => {
console.log("failed to start application, reason: " + error)
process.exit(1)
})
// setup scripts
await fetchCoinsDetails()
await initializeBot()
await getPriceHistory()
/**
* /start
*
* set up user, save a empty user documents in DB and print all available methods
*/
bot.command('start', async (ctx) => {
let chatId = ctx.msg.chat.id
let newUser = new User({
userId: chatId,
watchlist: []
})
User.findOne({ userId: chatId })
.then(res => {
if (!res) {
// add the user if does not exists
newUser.save()
}
})
.catch(err => console.log(err))
await ctx.reply(`Hi ${ctx.from.first_name || ctx.from.username}, Gday!`)
await ctx.reply('Welcome to coinmonitor. Here are some commands to start with')
await ctx.reply(`/help: shows all the available commands`)
await ctx.reply(`/watchlist: shows the coins in your watch list`)
await ctx.reply(`/add <coin symbol>: adds a coin with specified symbol to watch list, it must be a valid coin symbol. example: /add BTC`)
await ctx.reply(`/remove <coin symbol>: removes a coin with specified symbol from watch list, if it already exists, example /remove BTC`)
await ctx.reply(`To explore all the capabilities of the bot currently, just type /help.`)
})
bot.command('help', async (ctx) => {
let helpMessage = `The bot can do following things
Setup & starting up
==========================
1. /start Welcome command, sets up user and prints welcome message
Manage Watchlist
==========================
1. /watchlist : Print the details of coins in your watch list
2. /add : Adds a coin in your watchlist, example: /add BTC
3. /remove : Removes a coin from your watchlist, /remove BTC
See Supported coins
==========================
1. /all : Prints all the monitorable coins
Alerts
==========================
1. /alert volatility : Adds an alert for extreme volatility
2. /alert price <coin symbol> below <strike price> : Adds an alert for coin symbol when price goes below strike price,
example: /alert price BTC below 60000
3. /alert price <coin symbol> above <strike price> : Adds an alert for coin symbol when price goes above strike price
example: /alert price BTC above 5000
Tweets
==========================
1. /tweet on : Subscribe to daily twitter data from various sources
2. /tweet off : Unsubscribe to daily twitter data
`
await ctx.reply(helpMessage)
})
/**
* /watchlist
*
* list the details of all the coins in watchlist
*/
bot.command('watchlist', async (ctx) => {
await ctx.reply('Fetching your watchlist real quick...')
let chatId = ctx.msg.chat.id
let user = await User.findOne({ userId: chatId })
let watchlist = user?.watchlist || []
let coins = []
for (let symbol of watchlist) {
if (symbol in coinsDetails) coins.push(coinsDetails[symbol])
}
if (coins.length == 0) {
await ctx.reply('Watch list seems empty, add some coins to watchlist with /add <coin symbol> command')
return
}
for (let c of coins) {
let sym = c.symbol || ''
let message = `${sym} ${c.name}
Code: ${c.code}
Price: ${c.rate}
Volume: ${c.volume}
Market Cap: ${c.cap}
`
await ctx.reply(message)
}
})
/**
* /add
*
* adds a coin symbol to watchlist
*/
bot.command('add', async (ctx) => {
let chatId = ctx.msg.chat.id
let message = ctx.msg.text
let symbol = message.split(' ')
if (symbol.length < 2) {
await ctx.reply("No symbol is provided, please provide a valid symbol, use /all to get all available coins")
return
}
symbol = symbol[1].toUpperCase()
if (!(availableCoins.has(symbol))) {
await ctx.reply(`${symbol} is not valid crypto code, please use command /all to get all available coin codes`)
return
}
let user = await User.findOneAndUpdate({ userId: chatId }, { $addToSet: { watchlist: symbol }})
await ctx.reply(`Successfully added ${symbol} to your watch list`)
})
/**
* /remove
*
* removes a coin from watchlist
*/
bot.command('remove', async (ctx) => {
let chatId = ctx.msg.chat.id
let message = ctx.msg.text
let symbol = message.split(' ')
if (symbol.length < 2) {
await ctx.reply("No symbol is provided, please provide a valid symbol, use /all to get all available coins")
return
}
symbol = symbol[1].toUpperCase()
if (!(availableCoins.has(symbol))) {
await ctx.reply(`${symbol} is not valid crypto code or not supported yet, please use command /all to get all available coin codes`)
return
}
let user = await User.findOneAndUpdate({ userId: chatId }, { $pull: { watchlist: symbol }})
await ctx.reply(`Successfully removed ${symbol} from your watch list`)
})
/**
* /all
* lists all the available monitorable coins
*/
bot.command('all', async (ctx) => {
let all = ""
for (let c in coinsDetails) {
all += `${c} (${coinsDetails[c].name}) \n`
}
await ctx.reply(`Below are all coins available to monitor: \n ${all}`)
})
/**
* subscribe to a alert of type [volatility, ...]
*
* /alert <type> <args>
*/
bot.command('alert', async (ctx) => {
let supportedAlertTypes = ['volatility', 'price']
let chatId = ctx.msg.chat.id
let message = ctx.msg.text
let tokens = message.split(' ')
if (tokens.length < 2) {
await ctx.reply(`No alert type is provided, currently supported alert types are ${supportedAlertTypes}, for more info, use /help` )
return
}
let type = tokens[1]
if (type.toLowerCase() == 'volatility') {
await User.updateOne({ userId: chatId }, { volatilityAlert: true })
await ctx.reply('Added alert for extreme volatility signals for your watchlist')
}
else if (type.toLowerCase() == 'price') {
if (tokens.length < 5 || isNaN(tokens[4])) {
await ctx.reply('Coin and/or direction and/or strike price missing or malformed, a valid alert command would be something like this:')
await ctx.reply('/alert price BTC below 44')
await ctx.reply('or something like this...')
await ctx.reply('/alert price BTC above 24')
return
}
let coinId = tokens[2].toUpperCase()
let alertType = tokens[1]
let direction = tokens[3]
let value = Number(tokens[4])
if (!(availableCoins.has(coinId))) {
await ctx.reply(`Coin ${coinId} is either not valid or not supported yet`)
}
let newAlert = new Alert({
alertType: alertType,
value: value,
direction: direction,
userId: chatId,
coinId: coinId
})
await newAlert.save()
await ctx.reply(`Successfully added alert for coin ${coinId} for a strike price of ${direction} ${value}`)
}
else {
await ctx.reply(`Alert of type '${type} not supported yet, supproted types are ${supportedAlertTypes}`)
}
})
bot.command('tweet', async (ctx) => {
let message = ctx.msg.text
let chatId = ctx.msg.chat.id
let tokens = message.split(' ')
if (tokens.length < 2) {
await ctx.reply(`No action provided, available actions are [on, off], example, /tweet on`)
return
}
if (tokens[1] != 'on' && tokens[1] != 'off') {
await ctx.reply(`Action is not valie, available actions are [on, off], example, /tweet on`)
return
}
if (tokens[1] == 'on') {
await User.updateOne({ userId: chatId }, { tweet: true }, {multi: true})
await ctx.reply(`Successfuly subscribed to daily tweet updates`)
return
}
if (tokens[1] == 'off') {
await User.updateOne({ userId: chatId }, { tweet: false })
await ctx.reply('Successfully unsubscribed to daily tweets')
return
}
})
bot.start()