forked from kaibruneji/MyIrcBot
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tgbich.py
68 lines (53 loc) Β· 2.49 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
# 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 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.
<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", "ΠΊΡΡΡ"})
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()