27
27
28
28
application = None
29
29
default_chat_id = None
30
- custom_command = None
31
- custom_command_callback = None
32
30
33
- async def bot_start (token , chat_id , command = None , command_callback = None ):
31
+ class MyCommandHandler (telegram .ext .CommandHandler ):
32
+ __slots__ = ('my_command' , 'my_callback' )
33
+
34
+ def __init__ (self , command , callback ):
35
+ super ().__init__ (command , self .on_command )
36
+ self .my_command = command
37
+ self .my_callback = callback
38
+
39
+ async def on_command (self , update , context : telegram .ext .ContextTypes .DEFAULT_TYPE ):
40
+ try :
41
+ if self .my_command is not None :
42
+ await self .my_callback (self .my_command , context .args )
43
+ except :
44
+ logging .exception ('Fehler beim Bearbeiten des Kommandos' )
45
+
46
+
47
+ async def bot_start (token , chat_id , commands = [ ], command_callback = None ):
34
48
global application
35
49
global default_chat_id
36
- global custom_command
37
- global custom_command_callback
38
50
39
51
default_chat_id = chat_id
40
-
52
+
41
53
application = telegram .ext .Application .builder ().token (token ).read_timeout (20 ).get_updates_read_timeout (30 ).build ()
42
54
43
55
application .add_error_handler (on_error )
44
56
45
- if command is not None :
46
- application .add_handler (telegram .ext .CommandHandler (command , on_custom_command ))
47
-
48
- custom_command = command
49
- custom_command_callback = command_callback
50
-
57
+ for command in commands :
58
+ application .add_handler (MyCommandHandler (command , command_callback ))
59
+
51
60
await application .initialize ()
52
61
await application .updater .start_polling ()
53
62
await application .start ()
54
63
55
64
56
- async def on_custom_command (update , context : telegram .ext .ContextTypes .DEFAULT_TYPE ):
57
- global custom_command_callback
58
-
59
- try :
60
- if custom_command_callback is not None :
61
- await custom_command_callback (context .args )
62
-
63
- except :
64
- logging .exception ('Fehler beim Bearbeiten des Kommandos' )
65
-
66
-
67
65
async def on_error (update , context : telegram .ext .ContextTypes .DEFAULT_TYPE ):
68
66
global application
69
- global custom_command
70
-
67
+
71
68
logging .exception ('Error in telegram bot' , exc_info = context .error )
72
-
69
+
73
70
# If the bot is idle for several hours it might happen that it looses
74
71
# connection after a NetworkError. We can fix that with a simple reconnect
75
72
if isinstance (context .error , telegram .error .NetworkError ):
@@ -88,20 +85,20 @@ async def bot_stop():
88
85
async def bot_send (text , chat_id = None ):
89
86
global application
90
87
global default_chat_id
91
-
88
+
92
89
if chat_id is None :
93
90
chat_id = default_chat_id
94
-
91
+
95
92
message = await application .bot .send_message (chat_id , text )
96
-
93
+
97
94
return message .message_id
98
95
99
96
async def bot_delete (message_id , chat_id = None ):
100
97
global application
101
98
global default_chat_id
102
-
99
+
103
100
if chat_id is None :
104
101
chat_id = default_chat_id
105
-
102
+
106
103
await application .bot .delete_message (chat_id , message_id )
107
-
104
+
0 commit comments