-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
356 lines (307 loc) · 14 KB
/
bot.py
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from telegram import Updater, ReplyKeyboardMarkup, ReplyKeyboardHide
import logging
import pytrtbot
import pickledb
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('settings.ini')
token = str(config.get('main', 'token'))
REG = {
'apikey': None,
'apisecret': None,
'register': True
}
FUNDS = ['BTCEUR', 'BTCUSD', 'LTCEUR', 'LTCBTC', 'BTCXRP',
'EURXRP', 'USDXRP', 'LTCUSD', 'NMCBTC', 'PPCEUR',
'EURDOG', 'PPCBTC', 'BTCDOG']
CURRENCIES = ['BTC', 'EUR', 'USD', 'LTC', 'XRP', 'NMC', 'PPC', 'DOGE']
# Enable logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
def home(bot, update):
pytrtbot.writedb(update.message.to_dict())
db = pickledb.load('db/users.db', False)
userId = str(update.message.from_user.id)
try:
registered = not db.get(userId)['register']
except:
registered = False
if registered:
reply_markup = ReplyKeyboardMarkup([['/price', '/tickers'],
['/balances', '/discounts'],
['/trades', '/transactions'],
['/orders', '/bitcoin_data'],
['/help']])
else:
reply_markup = ReplyKeyboardMarkup([['/price', '/tickers'],
['/bitcoin_data'],
['/register', '/help']])
bot.sendMessage(update.message.chat_id,
text=pytrtbot.Home(),
reply_markup=reply_markup)
def help(bot, update):
pytrtbot.writedb(update.message.to_dict())
db = pickledb.load('db/users.db', False)
userId = str(update.message.from_user.id)
try:
registered = not db.get(userId)['register']
except:
registered = False
if registered:
reply_markup = ReplyKeyboardMarkup([['/delete_keys'],
['/home']])
else:
reply_markup = ReplyKeyboardMarkup([['/home']])
bot.sendMessage(update.message.chat_id,
text=pytrtbot.Help(),
reply_markup=reply_markup)
def price(bot, update):
pytrtbot.writedb(update.message.to_dict())
reply_markup = ReplyKeyboardMarkup([['/home']])
bot.sendMessage(update.message.chat_id,
text=pytrtbot.Last(),
reply_markup=reply_markup)
def ticker(bot, update):
pytrtbot.writedb(update.message.to_dict())
reply_markup = ReplyKeyboardMarkup([['/tickers'], ['/home']])
fund_id = str(update.message.text).replace('/tick_', '')
bot.sendMessage(update.message.chat_id,
text=pytrtbot.Ticker(fund_id),
reply_markup=reply_markup)
def tickers(bot, update):
pytrtbot.writedb(update.message.to_dict())
reply = 'Select a fund to get some info on it!'
reply_markup = ReplyKeyboardMarkup([['/tick_BTCEUR', '/tick_BTCUSD'],
['/tick_LTCEUR', '/tick_LTCBTC'],
['/tick_BTCXRP', '/tick_EURXRP'],
['/tick_USDXRP', '/tick_LTCUSD'],
['/tick_NMCBTC', '/tick_PPCEUR'],
['/tick_EURDOG', '/tick_PPCBTC'],
['/tick_BTCDOG', '/home']])
bot.sendMessage(update.message.chat_id,
text=reply,
reply_markup=reply_markup)
def bitcoinData(bot, update):
pytrtbot.writedb(update.message.to_dict())
reply_markup = ReplyKeyboardMarkup([['/home']])
text = pytrtbot.bitcoinData()
bot.sendMessage(update.message.chat_id,
text=text, reply_markup=reply_markup)
def register(bot, update):
pytrtbot.writedb(update.message.to_dict())
db = pickledb.load('db/users.db', False)
db.set(update.message.from_user.id, REG)
db.dump()
reply_markup = ReplyKeyboardHide()
bot.sendMessage(update.message.chat_id,
text='Send your public API Key and API ' +
'secret, with a space between them.',
reply_markup=reply_markup)
def deleteKeys(bot, update):
pytrtbot.writedb(update.message.to_dict())
db = pickledb.load('db/users.db', False)
db.rem(str(update.message.from_user.id))
db.dump()
reply_markup = ReplyKeyboardMarkup([['/price', '/tickers'],
['/bitcoin_data'],
['/register', '/help']])
bot.sendMessage(update.message.chat_id,
text='Your keys are deleted. You can use the bot anyway!',
reply_markup=reply_markup)
def signup(bot, update):
pytrtbot.writedb(update.message.to_dict())
userId = str(update.message.from_user.id)
db = pickledb.load('db/users.db', False)
try:
if db.get(userId)['register']:
key = update.message.text.split()[0]
secret = update.message.text.split()[1]
insertApiKey = {
'apikey': key,
'apisecret': secret,
'register': False
}
db.set(userId, insertApiKey)
db.dump()
reply_markup = ReplyKeyboardMarkup([['/balances'], ['/home']])
bot.sendMessage(update.message.chat_id,
text='User registered! Use /balances to try it',
reply_markup=reply_markup)
except:
reply_markup = ReplyKeyboardMarkup([['/register'],
['/home'],
['/help']])
bot.sendMessage(update.message.chat_id,
text='You can register! Type /register ' +
'and follow the instructions',
reply_markup=reply_markup)
def balances(bot, update):
pytrtbot.writedb(update.message.to_dict())
userId = str(update.message.from_user.id)
db = pickledb.load('db/users.db', False)
try:
apikey = db.get(userId)['apikey']
apisecret = db.get(userId)['apisecret']
markup, reply = pytrtbot.MyBalances(apikey, apisecret)
reply_markup = ReplyKeyboardMarkup([[markup]])
bot.sendMessage(update.message.chat_id,
text=reply,
reply_markup=reply_markup)
except:
reply_markup = ReplyKeyboardMarkup([['/register'], ['/home']])
bot.sendMessage(update.message.chat_id,
text='You must register! Type /register ' +
'and follow the instructions',
reply_markup=reply_markup)
def transactions(bot, update):
pytrtbot.writedb(update.message.to_dict())
userId = str(update.message.from_user.id)
db = pickledb.load('db/users.db', False)
try:
apikey = db.get(userId)['apikey']
apisecret = db.get(userId)['apisecret']
markup, reply = pytrtbot.MyTransactions(apikey, apisecret)
reply_markup = ReplyKeyboardMarkup([[markup]])
bot.sendMessage(update.message.chat_id,
text=reply,
reply_markup=reply_markup)
except:
reply_markup = ReplyKeyboardMarkup([['/register'], ['/home']])
bot.sendMessage(update.message.chat_id,
text='You must register! Type /register ' +
'and follow the instructions',
reply_markup=reply_markup)
def trades(bot, update):
pytrtbot.writedb(update.message.to_dict())
reply = 'Select a fund to get your trades!'
reply_markup = ReplyKeyboardMarkup([['/tr_BTCEUR', '/tr_BTCUSD'],
['/tr_LTCEUR', '/tr_LTCBTC'],
['/tr_BTCXRP', '/tr_EURXRP'],
['/tr_USDXRP', '/tr_LTCUSD'],
['/tr_NMCBTC', '/tr_PPCEUR'],
['/tr_EURDOG', '/tr_PPCBTC'],
['/tr_BTCDOG', '/home']])
bot.sendMessage(update.message.chat_id,
text=reply,
reply_markup=reply_markup)
def fundTrades(bot, update):
pytrtbot.writedb(update.message.to_dict())
userId = str(update.message.from_user.id)
db = pickledb.load('db/users.db', False)
try:
apikey = db.get(userId)['apikey']
apisecret = db.get(userId)['apisecret']
fund_id = str(update.message.text).replace('/tr_', '')
markup, reply = pytrtbot.MyTrades(apikey, apisecret, fund_id)
reply_markup = ReplyKeyboardMarkup([[markup], ['/home']])
bot.sendMessage(update.message.chat_id,
text=reply,
reply_markup=reply_markup)
except:
reply_markup = ReplyKeyboardMarkup([['/register'], ['/home']])
bot.sendMessage(update.message.chat_id,
text='You must register! Type /register ' +
'and follow the instructions',
reply_markup=reply_markup)
def discounts(bot, update):
pytrtbot.writedb(update.message.to_dict())
reply = 'Select a currency to get info on your discounts!'
reply_markup = ReplyKeyboardMarkup([['/disc_BTC', '/disc_EUR'],
['/disc_USD', '/disc_LTC'],
['/disc_XRP', '/disc_NMC'],
['/disc_PPC', '/disc_DOGE'],
['/home']])
bot.sendMessage(update.message.chat_id,
text=reply,
reply_markup=reply_markup)
def currDiscount(bot, update):
pytrtbot.writedb(update.message.to_dict())
userId = str(update.message.from_user.id)
db = pickledb.load('db/users.db', False)
try:
apikey = db.get(userId)['apikey']
apisecret = db.get(userId)['apisecret']
fund_id = str(update.message.text).replace('/disc_', '')
markup, reply = pytrtbot.MyDiscount(apikey, apisecret, fund_id)
reply_markup = ReplyKeyboardMarkup([[markup], ['/home']])
bot.sendMessage(update.message.chat_id,
text=reply,
reply_markup=reply_markup)
except:
reply_markup = ReplyKeyboardMarkup([['/register'], ['/home']])
bot.sendMessage(update.message.chat_id,
text='You must register! Type /register ' +
'and follow the instructions',
reply_markup=reply_markup)
def orders(bot, update):
pytrtbot.writedb(update.message.to_dict())
reply = 'Select a fund to get your orders!'
reply_markup = ReplyKeyboardMarkup([['/ord_BTCEUR', '/ord_BTCUSD'],
['/ord_LTCEUR', '/ord_LTCBTC'],
['/ord_BTCXRP', '/ord_EURXRP'],
['/ord_USDXRP', '/ord_LTCUSD'],
['/ord_NMCBTC', '/ord_PPCEUR'],
['/ord_EURDOG', '/ord_PPCBTC'],
['/ord_BTCDOG', '/home']])
bot.sendMessage(update.message.chat_id,
text=reply,
reply_markup=reply_markup)
def fundOrders(bot, update):
pytrtbot.writedb(update.message.to_dict())
userId = str(update.message.from_user.id)
db = pickledb.load('db/users.db', False)
try:
apikey = db.get(userId)['apikey']
apisecret = db.get(userId)['apisecret']
fund_id = str(update.message.text).replace('/ord_', '')
markup, reply = pytrtbot.MyOrders(apikey, apisecret, fund_id)
reply_markup = ReplyKeyboardMarkup([[markup], ['/home']])
bot.sendMessage(update.message.chat_id,
text=reply,
reply_markup=reply_markup)
except:
reply_markup = ReplyKeyboardMarkup([['/register'], ['/home']])
bot.sendMessage(update.message.chat_id,
text='You must register! Type /register ' +
'and follow the instructions',
reply_markup=reply_markup)
def error(bot, update, error):
pytrtbot.writedb(update.message.to_dict())
logger.warn('Update "%s" caused error "%s"' % (update, error))
def main():
# Create the EventHandler and pass it your bot's token.
updater = Updater(token)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.addTelegramCommandHandler("help", help)
dp.addTelegramCommandHandler("start", home)
dp.addTelegramCommandHandler("home", home)
dp.addTelegramCommandHandler("price", price)
dp.addTelegramCommandHandler("register", register)
dp.addTelegramCommandHandler("balances", balances)
dp.addTelegramCommandHandler("transactions", transactions)
dp.addTelegramCommandHandler("trades", trades)
dp.addTelegramCommandHandler("tickers", tickers)
dp.addTelegramCommandHandler("discounts", discounts)
dp.addTelegramCommandHandler("delete_keys", deleteKeys)
dp.addTelegramCommandHandler("bitcoin_data", bitcoinData)
dp.addTelegramCommandHandler("orders", orders)
for fund in FUNDS:
dp.addTelegramCommandHandler(('tr_' + fund), fundTrades)
dp.addTelegramCommandHandler(('tick_' + fund), ticker)
dp.addTelegramCommandHandler(('ord_'+fund), fundOrders)
for currency in CURRENCIES:
dp.addTelegramCommandHandler(('disc_' + currency), currDiscount)
dp.addTelegramMessageHandler(signup)
dp.addErrorHandler(error)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()