4
4
import telegram
5
5
import time
6
6
logger = logging .getLogger (__name__ )
7
- __all__ = ['CommandHandler' , 'CommandHandlerWithHelp' ]
7
+ __all__ = ['CommandHandler' , 'CommandHandlerWithHelp' , 'CommandHandlerWithFatherCommand' ,
8
+ 'CommandHandlerWithHelpAndFather' ]
9
+
10
+
8
11
class CommandHandler (object ):
9
12
""" This handles incomming commands and gives an easy way to create commands.
10
13
@@ -15,7 +18,8 @@ class CommandHandler(object):
15
18
"""
16
19
def __init__ (self , bot ):
17
20
self .bot = bot # a telegram bot
18
- self .isValidCommand = None # a function that returns a boolean and takes one agrument an update. if False is returned the the comaand is not executed.
21
+ self .isValidCommand = None # a function that returns a boolean and takes one agrument an update.
22
+ # If False is returned the the command is not executed.
19
23
20
24
def _get_command_func (self , command ):
21
25
if command [0 ] == '/' :
@@ -38,6 +42,7 @@ def run(self, make_thread=True, last_update_id=None, thread_timeout=2, sleep=0.2
38
42
The timeout on a thread. If a thread is alive after this period then try to join the thread in
39
43
the next loop.
40
44
"""
45
+
41
46
old_threads = []
42
47
while True :
43
48
time .sleep (sleep )
@@ -82,7 +87,7 @@ def run_once(self, make_thread=True, last_update_id=None):
82
87
if username == bot_name :
83
88
command_func = self ._get_command_func (command )
84
89
if command_func is not None :
85
- self .bot .sendChatAction (update .message .chat .id ,telegram .ChatAction .TYPING )
90
+ self .bot .sendChatAction (chat_id = update .message .chat .id , action = telegram .ChatAction .TYPING )
86
91
if self .isValidCommand is None or self .isValidCommand (update ):
87
92
if make_thread :
88
93
t = threading .Thread (target = command_func , args = (update ,))
@@ -107,8 +112,9 @@ def _command_not_valid(self, update):
107
112
"""
108
113
chat_id = update .message .chat .id
109
114
reply_to = update .message .message_id
110
- message = "Sorry, the command was not authorised or valid: {command}." .format (command = update .message .text .split (' ' )[0 ])
111
- self .bot .sendMessage (chat_id , message , reply_to_message_id = reply_to )
115
+ message = "Sorry, the command was not authorised or valid: {command}." .format (
116
+ command = update .message .text .split (' ' )[0 ])
117
+ self .bot .sendMessage (chat_id = chat_id , text = message , reply_to_message_id = reply_to )
112
118
113
119
def _command_not_found (self , update ):
114
120
"""Inform the telegram user that the command was not found.
@@ -119,7 +125,7 @@ def _command_not_found(self, update):
119
125
chat_id = update .message .chat .id
120
126
reply_to = update .message .message_id
121
127
message = "Sorry, I didn't understand the command: {command}." .format (command = update .message .text .split (' ' )[0 ])
122
- self .bot .sendMessage (chat_id , message , reply_to_message_id = reply_to )
128
+ self .bot .sendMessage (chat_id = chat_id , text = message , reply_to_message_id = reply_to )
123
129
124
130
125
131
class CommandHandlerWithHelp (CommandHandler ):
@@ -130,8 +136,23 @@ def __init__(self, bot):
130
136
self ._help_before_list = '' # text with information about the bot
131
137
self ._help_after_list = '' # a footer
132
138
self ._help_list_title = 'These are the commands:' # the title of the list
139
+ self ._help_extra_message = 'These commands are only usefull to the developer.'
133
140
self .is_reply = True
134
141
self .command_start = self .command_help
142
+ self .skip_in_help = []
143
+
144
+ def command_helpextra (self ,update ):
145
+ """ The commands in here are only usefull to the developer of the bot"""
146
+ command_functions = [attr [1 ] for attr in getmembers (self , predicate = ismethod ) if attr [0 ][:8 ] == 'command_' and
147
+ attr [0 ] in self .skip_in_help ]
148
+ chat_id = update .message .chat .id
149
+ help_message = self ._help_extra_message + '\n '
150
+ for command_function in command_functions :
151
+ if command_function .__doc__ is not None :
152
+ help_message += ' /' + command_function .__name__ [8 :] + ' - ' + command_function .__doc__ + '\n '
153
+ else :
154
+ help_message += ' /' + command_function .__name__ [8 :] + ' - ' + '\n '
155
+ self .bot .sendMessage (chat_id = chat_id , text = help_message )
135
156
136
157
def _generate_help (self ):
137
158
""" Generate a string which can be send as a help file.
@@ -141,17 +162,23 @@ def _generate_help(self):
141
162
command to the telegram user.
142
163
"""
143
164
144
- command_functions = [attr [1 ] for attr in getmembers (self , predicate = ismethod ) if attr [0 ][:8 ] == 'command_' ]
145
165
help_message = self ._help_title + '\n \n '
146
166
help_message += self ._help_before_list + '\n \n '
147
167
help_message += self ._help_list_title + '\n '
168
+ help_message += self ._generate_help_list ()
169
+ help_message += '\n '
170
+ help_message += self ._help_after_list
171
+ return help_message
172
+
173
+ def _generate_help_list (self ):
174
+ command_functions = [attr [1 ] for attr in getmembers (self , predicate = ismethod ) if attr [0 ][:8 ] == 'command_' and
175
+ attr [0 ] not in self .skip_in_help ]
176
+ help_message = ''
148
177
for command_function in command_functions :
149
178
if command_function .__doc__ is not None :
150
179
help_message += ' /' + command_function .__name__ [8 :] + ' - ' + command_function .__doc__ + '\n '
151
180
else :
152
181
help_message += ' /' + command_function .__name__ [8 :] + ' - ' + '\n '
153
- help_message += '\n '
154
- help_message += self ._help_after_list
155
182
return help_message
156
183
157
184
def _command_not_found (self , update ):
@@ -160,15 +187,45 @@ def _command_not_found(self, update):
160
187
reply_to = update .message .message_id
161
188
message = 'Sorry, I did not understand the command: {command}. Please see /help for all available commands'
162
189
if self .is_reply :
163
- self .bot .sendMessage (chat_id , message .format (command = update .message .text .split (' ' )[0 ]),
190
+ self .bot .sendMessage (chat_id = chat_id , text = message .format (command = update .message .text .split (' ' )[0 ]),
164
191
reply_to_message_id = reply_to )
165
192
else :
166
- self .bot .sendMessage (chat_id , message .format (command = update .message .text .split (' ' )[0 ]))
193
+ self .bot .sendMessage (chat_id = chat_id , text = message .format (command = update .message .text .split (' ' )[0 ]))
167
194
168
195
def command_help (self , update ):
169
196
""" The help file. """
170
197
chat_id = update .message .chat .id
171
198
reply_to = update .message .message_id
172
199
message = self ._generate_help ()
173
- self .bot .sendMessage (chat_id , message , reply_to_message_id = reply_to )
200
+ self .bot .sendMessage (chat_id = chat_id , text = message , reply_to_message_id = reply_to )
201
+
202
+
203
+ class CommandHandlerWithFatherCommand (CommandHandler ):
204
+ """ A class that creates some commands that are usefull when setting up the bot
205
+ """
206
+ def __init__ (self , bot ):
207
+ super (CommandHandlerWithFatherCommand , self ).__init__ (bot )
208
+ self .skip_in_help = ['command_father' ]
209
+
210
+ def command_father (self , update ):
211
+ """Gives you the commands you need to setup this bot. in telegram.me/BotFather"""
212
+ chat_id = update .message .chat .id
213
+ self .bot .sendMessage (chat_id = chat_id , text = 'Send the following messages to telegram.me/BotFather' )
214
+ self .bot .sendMessage (chat_id = chat_id , text = '/setcommands' )
215
+ self .bot .sendMessage (chat_id = chat_id , text = '@' + self .bot .getMe ()['username' ])
216
+ commands = ''
217
+ command_functions = [attr [1 ] for attr in getmembers (self , predicate = ismethod ) if attr [0 ][:8 ] == 'command_' and
218
+ attr [0 ] not in self .skip_in_help ]
219
+
220
+ for command_function in command_functions :
221
+ if command_function .__doc__ is not None :
222
+ commands += command_function .__name__ [8 :] + ' - ' + command_function .__doc__ + '\n '
223
+ else :
224
+ commands += command_function .__name__ [8 :] + ' - ' + '\n '
225
+ self .bot .sendMessage (chat_id = chat_id , text = commands )
226
+
174
227
228
+ class CommandHandlerWithHelpAndFather (CommandHandlerWithFatherCommand , CommandHandlerWithHelp ):
229
+ """A class that combines CommandHandlerWithHelp and CommandHandlerWithFatherCommand.
230
+ """
231
+ pass
0 commit comments