3939 logging .error ("Bot token not correct - please check." )
4040 exit (1 )
4141
42- updater = Updater (token = BOT_TOKEN )
42+ updater = Updater (token = BOT_TOKEN , use_context = True )
4343dispatcher = updater .dispatcher
4444
4545game_handler = GameHandler ().get_instance ()
@@ -56,7 +56,8 @@ def change_language(bot, update, lang_id):
5656 db .insert ("languageID" , lang_id , update .callback_query .from_user .id )
5757
5858
59- def callback_eval (bot , update ):
59+ def callback_eval (update , context ):
60+ bot = context .bot
6061 query_data = update .callback_query .data
6162
6263 # For changing the language:
@@ -65,18 +66,18 @@ def callback_eval(bot, update):
6566 change_language (bot = bot , update = update , lang_id = lang_id )
6667
6768 elif query_data == "com_ch_lang" :
68- language_cmd (bot , update )
69+ language_cmd (update , context )
6970
7071 elif query_data == "cancel_comment" :
71- cancel_cmd (bot , update )
72+ cancel_cmd (update , context )
7273
7374 elif query_data == "new_game" or query_data == "start_game" :
7475 bot .answerCallbackQuery (callback_query_id = update .callback_query .id )
75- start_cmd (bot , update )
76+ start_cmd (update , context )
7677
7778 elif query_data == "join_game" :
7879 bot .answerCallbackQuery (callback_query_id = update .callback_query .id )
79- join_cmd (bot , update )
80+ join_cmd (update , context )
8081
8182
8283def send_message (chat_id , text , message_id = None , parse_mode = None , reply_markup = None , game_id = None ):
@@ -95,7 +96,8 @@ def send_mp_message(chat_id, text, message_id=None, parse_mode=None, reply_marku
9596 print ("Game is None" )
9697
9798
98- def game_commands (bot , update ):
99+ def game_commands (update , context ):
100+ bot = context .bot
99101 if update .message is None :
100102 logger .warning ("game_commands error happened again! Update: {}" .format (update ))
101103
@@ -114,7 +116,7 @@ def game_commands(bot, update):
114116
115117 if user .get_state () == UserState .COMMENTING :
116118 # User wants to comment!
117- bot . sendMessage ( chat_id , text = translate ("userComment" , lang_id ))
119+ update . message . reply_text ( text = translate ("userComment" , lang_id ))
118120 for admin_id in db .get_admins ():
119121 admin_message = "New comment:\n \n {}\n \n {} | {} | {} | @{} | {}" .format (text , user_id , first_name , last_name ,
120122 username , lang_id )
@@ -129,7 +131,7 @@ def game_commands(bot, update):
129131
130132 if chat_id > 0 :
131133 # ask user for language if it's a private chat:
132- language_cmd (bot , update )
134+ language_cmd (update , context )
133135
134136 return
135137
@@ -141,8 +143,9 @@ def game_commands(bot, update):
141143 game .analyze_message (update )
142144
143145
144- def error ( bot , update , error ):
146+ def error_callback ( update , context ):
145147 """Log Errors caused by Updates."""
148+ error = context .error
146149 if update is None :
147150 return
148151
@@ -162,11 +165,11 @@ def stop_and_restart():
162165def admin_method (func ):
163166 """Decorator for marking methods as admin-only methods, so that strangers can't use them"""
164167
165- def admin_check (bot , update ):
168+ def admin_check (update , context ):
166169 db = DBwrapper .get_instance ()
167170 user = update .message .from_user
168171 if user .id in db .get_admins ():
169- return func (bot , update )
172+ return func (update , context )
170173 else :
171174 update .message .reply_text ('You have not the needed permissions to do that!' )
172175 logger .warning (
@@ -179,7 +182,7 @@ def admin_check(bot, update):
179182# -----------------
180183# User commands
181184# -----------------
182- def start_cmd (bot , update ):
185+ def start_cmd (update , context ):
183186 message = update .effective_message
184187 chat_id = message .chat_id
185188 eff_user = update .effective_user
@@ -198,7 +201,7 @@ def start_cmd(bot, update):
198201 db .add_user (user_id , "en" , first_name , last_name , username )
199202 if chat_id > 0 :
200203 # ask user for language:
201- language_cmd (bot , update )
204+ language_cmd (update , context )
202205 return
203206
204207 # check if user already has got a game (in the same chat):
@@ -219,7 +222,7 @@ def start_cmd(bot, update):
219222 message .reply_text ("Only the creator ({}) can start the game" .format (game .players [0 ].first_name ))
220223
221224
222- def stop_cmd (bot , update ):
225+ def stop_cmd (update , context ):
223226 user_id = update .message .from_user .id
224227 state_handler = StateHandler .get_instance ()
225228 user = state_handler .get_user (user_id )
@@ -230,7 +233,7 @@ def stop_cmd(bot, update):
230233 game_handler .gl_remove (chat_id )
231234
232235
233- def help_cmd (bot , update ):
236+ def help_cmd (update , context ):
234237 # Explains commands to user
235238 db = DBwrapper .get_instance ()
236239 lang_id = db .get_lang_id (update .message .from_user .id )
@@ -258,21 +261,20 @@ def help_cmd(bot, update):
258261 update .message .reply_text (text )
259262
260263
261- def join_cmd (bot , update ):
264+ def join_cmd (update , context ):
262265 message = update .effective_message
263- chat_id = message .chat_id
264266 user = update .effective_user
265- game = game_handler .get_game_by_chatid (chat_id )
267+ game = game_handler .get_game_by_chatid (message . chat_id )
266268
267269 if game is not None :
268270 game .add_player (user .id , user .first_name , message .message_id )
269271
270272
271- def stats_cmd (bot , update ):
273+ def stats_cmd (update , context ):
272274 update .message .reply_text (get_user_stats (update .message .from_user .id ))
273275
274276
275- def language_cmd (bot , update ):
277+ def language_cmd (update , context ):
276278 lang_de_button = InlineKeyboardButton (text = "Deutsch \U0001F1E9 \U0001F1EA " , callback_data = "ch_lang_de" )
277279 lang_en_button = InlineKeyboardButton (text = "English \U0001F1FA \U0001F1F8 " , callback_data = "ch_lang_en" )
278280 lang_nl_button = InlineKeyboardButton (text = "Nederlands \U0001F1F3 \U0001F1F1 " , callback_data = "ch_lang_nl" )
@@ -298,7 +300,9 @@ def language_cmd(bot, update):
298300 reply_markup = lang_keyboard , message_id = update .message .message_id )
299301
300302
301- def comment_cmd (bot , update , args ):
303+ def comment_cmd (update , context ):
304+ bot = context .bot
305+ args = context .args
302306 user_id = update .message .from_user .id
303307 chat_id = update .message .chat_id
304308 first_name = update .message .from_user .first_name
@@ -315,7 +319,7 @@ def comment_cmd(bot, update, args):
315319 text = " " .join (args )
316320 logger .debug ("New comment! {}!" .format (user_id ))
317321
318- bot . sendMessage ( chat_id = chat_id , text = translate ("userComment" , lang_id ))
322+ update . message . reply_text ( text = translate ("userComment" , lang_id ))
319323 for admin_id in db .get_admins ():
320324 bot .sendMessage (admin_id ,
321325 "New comment:\n \n {}\n \n {} | {} | {} | @{} | {}" .format (text , user_id , first_name ,
@@ -329,11 +333,11 @@ def comment_cmd(bot, update, args):
329333 keyboard = [[InlineKeyboardButton (text = translate ("cancel" , lang_id ), callback_data = "cancel_comment" )]]
330334 reply_markup = InlineKeyboardMarkup (keyboard )
331335
332- bot . sendMessage ( chat_id = chat_id , text = translate ("sendCommentNow" , lang_id ), reply_markup = reply_markup )
336+ update . message . reply_text ( text = translate ("sendCommentNow" , lang_id ), reply_markup = reply_markup )
333337 user .set_state (UserState .COMMENTING )
334338
335339
336- def cancel_cmd (bot , update ):
340+ def cancel_cmd (update , context ):
337341 user_id = update .effective_user .id
338342 message_id = update .effective_message .message_id
339343 callback_query_id = update .callback_query .id
@@ -347,24 +351,25 @@ def cancel_cmd(bot, update):
347351 lang_id = db .get_lang_id (user_id )
348352
349353 user .set_state (UserState .IDLE )
350- bot . editMessageText ( chat_id = chat_id , message_id = message_id , text = translate ("cancelledMessage" , lang_id ))
351- bot . answerCallbackQuery ( callback_query_id = callback_query_id , text = translate ("cancelledMessage" , lang_id ))
354+ cbq . edit_message_text ( text = translate ("cancelledMessage" , lang_id ))
355+ cbq . answer ( text = translate ("cancelledMessage" , lang_id ))
352356
353357
354- def hide_cmd (bot , update ):
358+ def hide_cmd (update , context ):
355359 """Hides the keyboard in the specified chat."""
356360 update .message .reply_text ("\U0001F44D " , reply_markup = ReplyKeyboardRemove ())
357361
358362
359- def mentions_cmd (bot , update ):
363+ def mentions_cmd (update , context ):
360364 # TODO mention users which helped (translations, etc.)
361365 pass
362366
363367
364- def multiplayer (bot , update ):
368+ def multiplayer (update , context ):
365369 chat_id = update .message .chat_id
366370 user_id = update .message .from_user .id
367371 message_id = update .message .message_id
372+ msg = update .message
368373 first_name = update .message .from_user .first_name
369374 # last_name = update.message.from_user.last_name
370375 # username = update.message.from_user.username
@@ -378,12 +383,12 @@ def multiplayer(bot, update):
378383 bj = BlackJackGame (chat_id , user_id , lang_id , first_name , game_handler , message_id , send_mp_message ,
379384 multiplayer = True , game_id = game_id )
380385 game_handler .add_game (bj )
381- bot . sendMessage ( chat_id , "Your game_id: {}" .format (bj .game_id ))
386+ msg . reply_text ( "Your game_id: {}" .format (bj .game_id ))
382387 else :
383388 logger .debug ("Game already existing" )
384389
385390
386- def join_secret (bot , update ):
391+ def join_secret (update , context ):
387392 user_id = update .message .from_user .id
388393 message_id = update .message .message_id
389394 first_name = update .message .from_user .first_name
@@ -396,62 +401,62 @@ def join_secret(bot, update):
396401 # TODO send message that user joined
397402
398403
399- def leave_chat (bot , update ):
404+ def leave_chat (update , context ):
400405 logger .info ("Leave channel" )
401- bot .leaveChat (update .effective_message .chat .id )
406+ context . bot .leaveChat (update .effective_message .chat .id )
402407
403408
404409# -----------------
405410# Admin commands
406411# -----------------
407412@admin_method
408- def answer (bot , update ):
409- sender_id = update .message . from_user . id
413+ def answer (update , context ):
414+ msg = update .message
410415 reply_to_message = update .message .reply_to_message
416+ sender_id = update .message .from_user .id
411417 text = str (update .message .text [8 :])
412418 db = DBwrapper .get_instance ()
413419
414420 if reply_to_message is None :
415- bot . sendMessage ( sender_id , text = "⚠ You need to reply to the user's comment!" )
421+ msg . reply_text ( text = "⚠ You need to reply to the user's comment!" )
416422 return
417423
418424 try :
419425 last_line = reply_to_message .text .split ("\n " )
420426 ll_list = last_line [- 1 ].split (" | " )
421- user_id = ll_list [0 ]
427+ receiver_id = ll_list [0 ]
422428 except ValueError :
423429 return
424430
425- if not re .match ("[0-9]+" , user_id ):
426- bot . sendMessage ( sender_id , "⚠ The user_id is not valid. Are you sure you replied to a user comment?" )
431+ if not re .match ("[0-9]+" , receiver_id ):
432+ msg . reply_text ( "⚠ The user_id is not valid. Are you sure you replied to a user comment?" )
427433 return
428434
429- answer_text = "{}\n \n {}" .format (translate ("answerFromDev" , db .get_lang_id (user_id )), text )
430- bot .sendMessage (chat_id = user_id , text = answer_text )
431- bot . sendMessage ( chat_id = sender_id , text = "Message sent!" )
435+ answer_text = "{}\n \n {}" .format (translate ("answerFromDev" , db .get_lang_id (receiver_id )), text )
436+ context . bot .sendMessage (chat_id = receiver_id , text = answer_text )
437+ msg . reply_text ( text = "Message sent!" )
432438
433439
434440@admin_method
435- def users (bot , update ):
436- sender_id = update .message .from_user .id
441+ def users (update , context ):
437442 db = DBwrapper .get_instance ()
438443 players = db .get_recent_players ()
439444
440445 text = "Last 24 hours: {}" .format (len (players ))
441446
442- bot . sendMessage ( chat_id = sender_id , text = text )
447+ update . message . reply_text ( text = text )
443448
444449
445450@admin_method
446- def restart (bot , update ):
451+ def restart (update , context ):
447452 update .message .reply_text ('Bot is restarting...' )
448453 Thread (target = stop_and_restart ).start ()
449454
450455
451- channel_handler = MessageHandler (own_filters . ChannelFilter , leave_chat )
452- start_handler = CommandHandler (translate_all ( "startCmd" ) , start_cmd )
453- stop_handler = CommandHandler (translate_all ( "stopCmd" ) , stop_cmd )
454- join_handler = CommandHandler (translate_all ( "join" ) , join_cmd )
456+ channel_handler = MessageHandler (Filters . update . channel_posts , leave_chat )
457+ start_handler = CommandHandler ("start" , start_cmd )
458+ stop_handler = CommandHandler ("stop" , stop_cmd )
459+ join_handler = CommandHandler ("join" , join_cmd )
455460help_handler = CommandHandler ('help' , help_cmd )
456461hide_handler = CommandHandler ('hide' , hide_cmd )
457462stats_handler = CommandHandler ('stats' , stats_cmd )
@@ -475,7 +480,7 @@ def restart(bot, update):
475480for handler in handlers :
476481 dispatcher .add_handler (handler )
477482
478- dispatcher .add_error_handler (error )
483+ dispatcher .add_error_handler (error_callback )
479484
480485if USE_WEBHOOK :
481486 updater .start_webhook (listen = "127.0.0.1" , port = WEBHOOK_PORT , url_path = BOT_TOKEN , cert = CERTPATH , webhook_url = WEBHOOK_URL )
0 commit comments