forked from KDZMEDIABOT/KDZMEDIABOT
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tgbich.py
80 lines (63 loc) · 2.96 KB
/
tgbich.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
# tgbich.py
from abstractbich import BichBot
from helpers import get_pretty_json_string, shell, LOG_TRACE
import os
import asyncio
from aiogram import Bot, Dispatcher, types
class TgBich(BichBot):
def __init__(self, settings_key, connection_settings: dict, config):
super(TgBich, self).__init__(settings_key, connection_settings, config)
self.BOT_TOKEN = self.connection_settings('BOT_TOKEN')
print(f"{self}: entering asyncio.run(self.main())");
asyncio.run(self.main())
print(f"{self}: completed asyncio.run(self.main())");
async def on_message(self, message: types.Message):
text = message.text
ticker_str = str(text).strip().upper()
reply_str = self.compose_ticker_price_reply(ticker_str)
await message.answer(
reply_str,
parse_mode=types.ParseMode.HTML,
)
async def cmd_start_handler(self, event: types.Message):
await event.answer(
f"""Hello, {event.from_user.get_mention(as_html=True)} 👋!
<b>Help:</b>
/m /markets - see the markets report (курсы валют, индикаторы и прочее);
/h /help - see the help and welcome message.
If you send a stock or token ticker to the bot, it will query its price at CoinMarketCap.com and report it to you.
<b>Bot author:</b> @ConstAlphaRusDev (Гипн aka гном)""",
parse_mode=types.ParseMode.HTML,
)
async def cmd_markets_handler(self, event: types.Message):
markets_report_str = self.compose_markets_report()
print(f"{self}: markets_report_str '{markets_report_str}'");
await event.answer(
markets_report_str,
parse_mode=types.ParseMode.HTML,
)
async def main(self):
print(f"{self}: new Bot");
self.bot = Bot(token=self.BOT_TOKEN)
print(f"{self}: done new Bot");
try:
print(f"{self}: new Dispatcher");
self.disp = Dispatcher(bot=self.bot)
print(f"{self}: done new Dispatcher");
self.disp.register_message_handler(self.cmd_start_handler, commands={"start", "s", "h", "help", "?"})
self.disp.register_message_handler(self.cmd_markets_handler, commands={"markets", "m", "курс"})
self.disp.register_message_handler(self.on_message)
print(f"{self}: entering start_polling()");
await self.disp.start_polling()
print(f"{self}: completed start_polling()");
finally:
print(f"{self}: finally: entering bot.close()");
await self.bot.close()
print(f"{self}: finally: completed bot.close()");
def run_tgbich(settings_key, connection_settings: dict, config):
connection_props = connection_settings
print(f'run_tgbich settings_key="{settings_key}"')
print(f'{settings_key}.parent pid: {os.getppid()}')
print(f'{settings_key}.pid: {os.getpid()}')
bot = TgBich(settings_key, connection_settings, config)
bot.login_and_loop()