-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
475 lines (386 loc) · 20.3 KB
/
main.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
from unittest.mock import call
import json
import requests
import telebot
from telebot import types
import pb # для Дашиной функции импортим файл pb
import news_parser # для парсинга новостей
import json # импортим для парсинга сайта
from app.utils import serialize_ex, get_exchange
from app.config import ctx
from app.db import User, add_user, create_tables
bot = telebot.TeleBot(ctx.tg_bot_token) # АПИ бота, для его подключения
@bot.message_handler(commands=["start"])
def start_command(message):
if message.text == "/start":
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(
telebot.types.InlineKeyboardButton("Menu", callback_data="main_menu")
)
bot.send_message(
message.chat.id,
"Greetings! I can show you exchange rates.\n"
"Press Menu to continue.", reply_markup=keyboard
)
@bot.message_handler(commands=["menu"])
def menu_command(message):
if message.text == "/menu":
kb = telebot.types.InlineKeyboardMarkup(row_width=1)
kb.add(telebot.types.InlineKeyboardButton("Exchange", callback_data="exchange_back"),
telebot.types.InlineKeyboardButton("Ticker", callback_data="ticker_back"),
telebot.types.InlineKeyboardButton("Register", callback_data="reg"),
telebot.types.InlineKeyboardButton("Help", callback_data="help"),
)
bot.send_message(
message.chat.id,
"That's list of all possible features\n"
+ "To get the exchange rates press exchange.\n"
+ "To get Current price and EnterpriseValuetype press ticker.\n", reply_markup=kb
)
@bot.message_handler(commands=["reg"])
def start(message):
if message.text == "/reg":
bot.send_message(message.from_user.id, "What is your name?")
bot.register_next_step_handler(message, get_name)
else:
bot.send_message(message.from_user.id, "Type /reg")
@bot.message_handler(commands=["exchange"])
def exchange_command(message):
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(telebot.types.InlineKeyboardButton("USD", callback_data="get-USD"),
telebot.types.InlineKeyboardButton("EUR", callback_data="get-EUR"),
telebot.types.InlineKeyboardButton("RUB", callback_data="get-RUR"))
keyboard.row(
telebot.types.InlineKeyboardButton("Back", callback_data="main_menu"),
)
bot.send_message(
message.chat.id, "Click on the currency of choice:", reply_markup=keyboard
)
@bot.message_handler(commands=["ticker"])
def ticker(message):
if message.text == "/ticker":
keyboard = types.InlineKeyboardMarkup()
key_information = types.InlineKeyboardButton(text="Information", callback_data="Information_ticker")
keyboard.add(key_information)
key_news = types.InlineKeyboardButton(text="News", callback_data="News_ticker")
keyboard.add(key_news)
keyboard.add(types.InlineKeyboardButton(text="Back", callback_data="main_menu"))
bot.send_message(message.from_user.id, text="What would u like to see", reply_markup=keyboard)
else:
bot.send_message(message.from_user.id, "Type /ticker")
@bot.message_handler(commands=["help"])
def help_command(message):
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.add(
telebot.types.InlineKeyboardButton(
"Message the developer", url="telegram.me/co_ban"
)
)
keyboard.add(telebot.types.InlineKeyboardButton("Menu", callback_data="main_menu"))
bot.send_message(
message.chat.id,
"1) To receive a list of available currencies press Exchange in a menu.\n"
+ "1.1) Click on the currency you are interested in.\n"
+ "1.2) You will receive a message containing information regarding the source and the target currencies, "
+ "buying rates and selling rates.\n"
+ "2) The bot supports inline. Type @DSBAAN_bot in any chat and the first letters of a currency.\n"
+ "3) To register in the bot press Register in a menu.\n"
+ "4) To get Current price and EnterpriseValuetype press Ticker in a menu",
reply_markup=keyboard,
)
@bot.callback_query_handler(func=lambda call: True)
def iq_callback(query):
data = query.data
if data == "main_menu":
# Deleting Inline Buttons
bot.edit_message_reply_markup(chat_id=query.message.chat.id, message_id=query.message.message_id)
# Clearing step handlers
bot.clear_step_handler_by_chat_id(query.message.chat.id)
kb = telebot.types.InlineKeyboardMarkup(row_width=1)
kb.add(telebot.types.InlineKeyboardButton("Exchange", callback_data="exchange_back"),
telebot.types.InlineKeyboardButton("Ticker", callback_data="ticker_back"),
telebot.types.InlineKeyboardButton("Register", callback_data="reg"),
telebot.types.InlineKeyboardButton("Help", callback_data="help"),
)
bot.send_message(
query.message.chat.id,
"That's list of all possible features\n"
+ "To get the exchange rates press exchange.\n"
+ "To get Current price and EnterpriseValuetype press ticker.\n", reply_markup=kb
)
elif data == "exchange_back":
# Deleting Inline Buttons
bot.edit_message_reply_markup(chat_id=query.message.chat.id, message_id=query.message.message_id)
# Sending exchange msg with keyboard (just like /exchange command does)
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(telebot.types.InlineKeyboardButton("USD", callback_data="get-USD"),
telebot.types.InlineKeyboardButton("EUR", callback_data="get-EUR"),
telebot.types.InlineKeyboardButton("RUB", callback_data="get-RUR"))
keyboard.row(
telebot.types.InlineKeyboardButton("Back", callback_data="main_menu"),
)
bot.send_message(
query.message.chat.id, "Click on the currency of choice:", reply_markup=keyboard
)
elif data == "ticker_back":
# Deleting Inline Buttons
bot.edit_message_reply_markup(chat_id=query.message.chat.id, message_id=query.message.message_id)
# Clearing step handlers
bot.clear_step_handler_by_chat_id(query.message.chat.id)
keyboard = types.InlineKeyboardMarkup()
key_information = types.InlineKeyboardButton(text="Information", callback_data="Information_ticker")
keyboard.add(key_information)
key_news = types.InlineKeyboardButton(text="News", callback_data="News_ticker")
keyboard.add(key_news)
keyboard.add(types.InlineKeyboardButton(text="Back", callback_data="main_menu"))
bot.send_message(query.message.chat.id, text="What would u like to see", reply_markup=keyboard)
elif data == "reg":
# Deleting Inline Buttons
bot.edit_message_reply_markup(chat_id=query.message.chat.id, message_id=query.message.message_id)
bot.send_message(query.message.chat.id, "What is your name?")
bot.register_next_step_handler(query.message, get_name)
elif data == "help":
# Deleting Inline Buttons
bot.edit_message_reply_markup(chat_id=query.message.chat.id, message_id=query.message.message_id)
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.add(
telebot.types.InlineKeyboardButton(
"Message the developer", url="telegram.me/co_ban"
)
)
keyboard.add(telebot.types.InlineKeyboardButton("Menu", callback_data="main_menu"))
bot.send_message(
query.message.chat.id,
"1) To receive a list of available currencies press Exchange in a menu.\n"
+ "1.1) Click on the currency you are interested in.\n"
+ "1.2) You will receive a message containing information regarding the source and the target currencies, "
+ "buying rates and selling rates.\n"
+ "2) The bot supports inline. Type @DSBAAN_bot in any chat and the first letters of a currency.\n"
+ "3) To register in the bot press Register in a menu.\n"
+ "4) To get Current price and EnterpriseValuetype press Ticker in a menu",
reply_markup=keyboard,
)
elif data.startswith('get-'):
# Deleting Inline buttons
bot.edit_message_reply_markup(chat_id=query.message.chat.id, message_id=query.message.message_id)
get_ex_callback(query)
elif query.data == "Information_ticker":
# Deleting Inline buttons
bot.edit_message_reply_markup(chat_id=query.message.chat.id, message_id=query.message.message_id)
# Creating the keyboard
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(
telebot.types.InlineKeyboardButton("Back", callback_data="ticker_back"),
telebot.types.InlineKeyboardButton("Menu", callback_data="main_menu")
)
bot.register_next_step_handler(query.message, getinfo)
bot.send_message(query.message.chat.id, "Please send a ticker. For example: AAPL",
reply_markup=keyboard)
elif query.data == "News_ticker":
# Deleting Inline buttons
bot.edit_message_reply_markup(chat_id=query.message.chat.id, message_id=query.message.message_id)
# Creating the keyboard
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(
telebot.types.InlineKeyboardButton("Back", callback_data="ticker_back"),
telebot.types.InlineKeyboardButton("Menu", callback_data="main_menu")
)
bot.register_next_step_handler(query.message, sendnews)
bot.send_message(query.message.chat.id, "Please send a ticker at I'll find news for it. For example: AAPL",
reply_markup=keyboard)
elif query.data.startswith('send_ticker_results_'):
# Deleting Inline buttons
bot.edit_message_reply_markup(chat_id=query.message.chat.id, message_id=query.message.message_id)
url = "https://apidojo-yahoo-finance-v1.p.rapidapi.com/stock/v2/get-summary"
querystring = {"symbol": query.data.strip('send_ticker_results_'), "region": "US"}
headers = {
'x-rapidapi-key': "7d2a5f774amshbee3475d315e704p1bd9e6jsnc7de2c4c43d6",
'x-rapidapi-host': "apidojo-yahoo-finance-v1.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring)
try:
qwe = json.loads(response.text)
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(
telebot.types.InlineKeyboardButton("Back", callback_data="ticker_back"),
telebot.types.InlineKeyboardButton("Menu", callback_data="main_menu")
)
bot.send_message(query.message.chat.id,
f" {query.data.strip('send_ticker_results_')}\nCurrent price - {qwe['financialData']['currentPrice']['raw']}\nEnterpriseVAlue - {qwe['defaultKeyStatistics']['enterpriseValue']['raw']}",
reply_markup=keyboard)
except json.decoder.JSONDecodeError:
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(
telebot.types.InlineKeyboardButton("Back", callback_data="ticker_back"),
telebot.types.InlineKeyboardButton("Menu", callback_data="main_menu")
)
bot.send_message(query.message.chat.id, "No such ticker has been found. Please try again.",
reply_markup=keyboard)
bot.register_next_step_handler(query.message, getinfo)
return
else:
if query.data == "reg_confirmed":
# Deleting Inline buttons
bot.edit_message_reply_markup(chat_id=query.message.chat.id, message_id=query.message.message_id)
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(
telebot.types.InlineKeyboardButton("Menu", callback_data="main_menu")
)
bot.send_message(query.message.chat.id, "That's great!", reply_markup=keyboard)
elif query.data == "reg_declined":
# Deleting Inline buttons
bot.edit_message_reply_markup(chat_id=query.message.chat.id, message_id=query.message.message_id)
bot.send_message(query.message.chat.id, "Ok, one more time! What is your name?")
bot.register_next_step_handler(query.message, get_name)
@bot.message_handler(content_types=["text"])
def get_text_messages(message):
if message.text == "Hi":
kb = types.InlineKeyboardMarkup()
kb.add(types.InlineKeyboardButton(text="Register", callback_data="reg"))
bot.send_message(message.from_user.id, "Hi, you can press Register to register", reply_markup=kb)
else:
kb = types.InlineKeyboardMarkup()
kb.add(types.InlineKeyboardButton(text="Menu", callback_data="main_menu"))
kb.add(types.InlineKeyboardButton(text="Help", callback_data="help"))
bot.send_message(message.from_user.id, "I can't understand you. Press Menu to get a list of possible "
"features or Help if you need.", reply_markup=kb)
# Register functions query
def get_name(message):
global name
name = message.text
bot.send_message(message.from_user.id, "What is your surname?")
bot.register_next_step_handler(message, get_surname)
def get_surname(message):
global surname
surname = message.text
bot.send_message(message.from_user.id, "How old are you?")
bot.register_next_step_handler(message, get_age)
def get_age(message):
global age
age = message.text
if age.isdigit():
keyboard = types.InlineKeyboardMarkup()
key_yes = types.InlineKeyboardButton(text="Yes!", callback_data="reg_confirmed")
keyboard.add(key_yes)
key_no = types.InlineKeyboardButton(text="No", callback_data="reg_declined")
keyboard.add(key_no)
question = (
"You are " + str(age) + " years, your name is " + name + " " + surname + "?"
)
bot.send_message(message.from_user.id, text=question, reply_markup=keyboard)
else:
bot.send_message(message.from_user.id, "Type with numbers, please")
bot.register_next_step_handler(message, get_age)
# Proceeding EX callbacks
def get_ex_callback(query):
bot.answer_callback_query(query.id)
send_exchange_result(query.message, query.data[4:])
def send_exchange_result(message, ex_code):
bot.send_chat_action(message.chat.id, "typing")
ex = pb.get_exchange(ex_code)
bot.send_message(
message.chat.id,
serialize_ex(ex),
reply_markup=get_update_keyboard(ex),
parse_mode="HTML",
)
def get_update_keyboard(ex):
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(
telebot.types.InlineKeyboardButton(
"",
callback_data=json.dumps(
{"t": "u", "e": {"b": ex["buy"], "s": ex["sale"], "c": ex["ccy"]}}
).replace(" ", ""),
),
telebot.types.InlineKeyboardButton("", switch_inline_query=ex["ccy"]),
)
# Adding back and menu buttons
keyboard.row(
telebot.types.InlineKeyboardButton("Back", callback_data="exchange_back"),
telebot.types.InlineKeyboardButton("Menu", callback_data="main_menu")
)
return keyboard
# Ticker functions
def getinfo(ticker):
url = "https://apidojo-yahoo-finance-v1.p.rapidapi.com/stock/v2/get-summary"
querystring = {"symbol": ticker.text, "region": "US"}
headers = {
'x-rapidapi-key': "7d2a5f774amshbee3475d315e704p1bd9e6jsnc7de2c4c43d6",
'x-rapidapi-host': "apidojo-yahoo-finance-v1.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring)
try:
qwe = json.loads(response.text)
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(
telebot.types.InlineKeyboardButton("Back", callback_data="ticker_back"),
telebot.types.InlineKeyboardButton("Menu", callback_data="main_menu")
)
bot.send_message(ticker.from_user.id,
f" {ticker.text}\nCurrent price - {qwe['financialData']['currentPrice']['raw']}\nEnterpriseVAlue - {qwe['defaultKeyStatistics']['enterpriseValue']['raw']}",
reply_markup=keyboard)
except json.decoder.JSONDecodeError:
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(
telebot.types.InlineKeyboardButton("Back", callback_data="ticker_back"),
telebot.types.InlineKeyboardButton("Menu", callback_data="main_menu")
)
bot.send_message(ticker.from_user.id, "No such ticker has been found. Please try again.",
reply_markup=keyboard)
bot.register_next_step_handler(ticker, getinfo)
return
def sendnews(ticker):
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(
telebot.types.InlineKeyboardButton("Back", callback_data="ticker_back"),
telebot.types.InlineKeyboardButton("Menu", callback_data="main_menu"),
telebot.types.InlineKeyboardButton("Ticker", callback_data=f"send_ticker_results_{ticker.text}")
)
keyboard.add(telebot.types.InlineKeyboardButton("Find another one", callback_data="News_ticker"))
for msg in news_parser.find_by_token(ticker.text):
bot.send_message(ticker.from_user.id, text=msg, parse_mode="HTML")
bot.send_message(ticker.from_user.id, "That's the news I got for you. Click 'Ticker' to get ticker info for same"
" request.",
reply_markup=keyboard)
# Serializing functions
def serialize_ex(ex_json, diff=None):
result = (
"<b>"
+ ex_json["base_ccy"]
+ " -> "
+ ex_json["ccy"]
+ ":</b>\n\n"
+ "Buy: "
+ ex_json["buy"]
)
if diff:
result += (
" "
+ serialize_exchange_diff(diff["buy_diff"])
+ "\n"
+ "Sell: "
+ ex_json["sale"]
+ " "
+ serialize_exchange_diff(diff["sale_diff"])
+ "\n"
)
else:
result += "\nSell: " + ex_json["sale"] + "\n"
return result
def serialize_exchange_diff(diff):
result = ""
if diff > 0:
result = (
"("
+ str(diff)
+ ' <img draggable="false" data-mce-resize="false" data-mce-placeholder="1" data-wp-emoji="1" class="emoji" alt="<img draggable="false" data-mce-resize="false" data-mce-placeholder="1" data-wp-emoji="1" class="emoji" alt="<img draggable="false" data-mce-resize="false" data-mce-placeholder="1" data-wp-emoji="1" class="emoji" alt="<img draggable="false" data-mce-resize="false" data-mce-placeholder="1" data-wp-emoji="1" class="emoji" alt="<img draggable="false" data-mce-resize="false" data-mce-placeholder="1" data-wp-emoji="1" class="emoji" alt="↗️" src="https://s.w.org/images/core/emoji/2.3/svg/2197.svg">" src="https://s.w.org/images/core/emoji/2.3/svg/2197.svg">" src="https://s.w.org/images/core/emoji/2.3/svg/2197.svg">" src="https://s.w.org/images/core/emoji/72x72/2197.png">" src="https://s.w.org/images/core/emoji/72x72/2197.png">)'
)
elif diff < 0:
result = (
"("
+ str(diff)[1:]
+ ' <img draggable="false" data-mce-resize="false" data-mce-placeholder="1" data-wp-emoji="1" class="emoji" alt="<img draggable="false" data-mce-resize="false" data-mce-placeholder="1" data-wp-emoji="1" class="emoji" alt="<img draggable="false" data-mce-resize="false" data-mce-placeholder="1" data-wp-emoji="1" class="emoji" alt="<img draggable="false" data-mce-resize="false" data-mce-placeholder="1" data-wp-emoji="1" class="emoji" alt="<img draggable="false" data-mce-resize="false" data-mce-placeholder="1" data-wp-emoji="1" class="emoji" alt="↘️" src="https://s.w.org/images/core/emoji/2.3/svg/2198.svg">" src="https://s.w.org/images/core/emoji/2.3/svg/2198.svg">" src="https://s.w.org/images/core/emoji/2.3/svg/2198.svg">" src="https://s.w.org/images/core/emoji/72x72/2198.png">" src="https://s.w.org/images/core/emoji/72x72/2198.png">)'
)
return result
bot.polling(none_stop=True, interval=0)