2015-06-26 09:55:13 +03:00
# -*- coding: utf-8 -*-
2015-07-01 05:36:58 +03:00
from __future__ import print_function
2015-06-26 09:55:13 +03:00
2018-08-17 12:54:26 +03:00
import logging
import re
import sys
import threading
import time
2018-05-20 23:40:25 +03:00
2018-08-17 12:54:26 +03:00
import six
2015-09-30 18:18:26 +03:00
2015-09-08 20:47:55 +03:00
logger = logging . getLogger ( ' TeleBot ' )
2015-10-01 23:03:54 +03:00
formatter = logging . Formatter (
2016-02-27 06:17:35 +03:00
' %(asctime)s ( %(filename)s : %(lineno)d %(threadName)s ) %(levelname)s - %(name)s : " %(message)s " '
2015-10-01 23:03:54 +03:00
)
2015-09-05 13:12:52 +03:00
2015-09-08 20:56:05 +03:00
console_output_handler = logging . StreamHandler ( sys . stderr )
console_output_handler . setFormatter ( formatter )
logger . addHandler ( console_output_handler )
2015-09-08 20:47:55 +03:00
logger . setLevel ( logging . ERROR )
2015-08-31 12:46:18 +03:00
2018-04-28 13:50:59 +03:00
from telebot import apihelper , types , util
2020-04-12 20:41:32 +03:00
from telebot . handler_backends import MemoryHandlerBackend , FileHandlerBackend
2015-06-30 06:49:35 +03:00
2015-06-26 09:55:13 +03:00
"""
Module : telebot
"""
2015-09-30 18:18:26 +03:00
2018-05-25 20:57:22 +03:00
class Handler :
2018-05-27 01:23:20 +03:00
"""
Class for ( next step | reply ) handlers
"""
2018-08-17 13:01:03 +03:00
2018-05-27 16:54:56 +03:00
def __init__ ( self , callback , * args , * * kwargs ) :
2018-05-27 01:23:20 +03:00
self . callback = callback
2018-05-25 20:57:22 +03:00
self . args = args
self . kwargs = kwargs
def __getitem__ ( self , item ) :
return getattr ( self , item )
2015-06-26 09:55:13 +03:00
class TeleBot :
""" This is TeleBot Class
Methods :
getMe
sendMessage
forwardMessage
2017-05-07 17:37:03 +03:00
deleteMessage
2015-06-26 09:55:13 +03:00
sendPhoto
sendAudio
sendDocument
sendSticker
sendVideo
2019-02-15 21:46:18 +03:00
sendAnimation
2017-05-19 17:19:15 +03:00
sendVideoNote
2015-06-26 09:55:13 +03:00
sendLocation
sendChatAction
2020-04-15 08:10:05 +03:00
sendDice
2015-06-26 09:55:13 +03:00
getUserProfilePhotos
getUpdates
2017-06-30 19:47:09 +03:00
getFile
2019-06-06 21:47:08 +03:00
sendPoll
2017-06-30 19:47:09 +03:00
kickChatMember
unbanChatMember
restrictChatMember
promoteChatMember
exportChatInviteLink
setChatPhoto
deleteChatPhoto
setChatTitle
setChatDescription
pinChatMessage
unpinChatMessage
leaveChat
getChat
getChatAdministrators
getChatMembersCount
getChatMember
answerCallbackQuery
2020-05-08 21:06:39 +03:00
setMyCommands
2017-06-30 19:47:09 +03:00
answerInlineQuery
"""
2015-06-26 09:55:13 +03:00
2020-04-12 20:41:32 +03:00
def __init__ (
2020-07-04 21:07:42 +03:00
self , token , parse_mode = None , threaded = True , skip_pending = False , num_threads = 2 ,
2020-04-12 20:41:32 +03:00
next_step_backend = None , reply_backend = None
) :
2015-07-04 12:00:42 +03:00
"""
: param token : bot API token
2020-07-04 21:07:42 +03:00
: param parse_mode : default parse_mode
2015-07-20 04:56:17 +03:00
: return : Telebot object .
2015-07-04 12:00:42 +03:00
"""
2018-04-12 13:45:32 +03:00
2015-06-26 09:55:13 +03:00
self . token = token
2020-07-04 21:07:42 +03:00
self . parse_mode = parse_mode
2015-06-26 13:02:30 +03:00
self . update_listener = [ ]
2015-12-01 17:05:52 +03:00
self . skip_pending = skip_pending
2015-10-01 23:03:54 +03:00
2015-07-18 12:27:16 +03:00
self . __stop_polling = threading . Event ( )
2015-06-30 06:49:35 +03:00
self . last_update_id = 0
2015-10-01 23:03:54 +03:00
self . exc_info = None
2015-06-30 02:44:14 +03:00
2020-04-12 20:41:32 +03:00
self . next_step_backend = next_step_backend
if not self . next_step_backend :
self . next_step_backend = MemoryHandlerBackend ( )
2015-07-24 14:44:01 +03:00
2020-04-12 20:41:32 +03:00
self . reply_backend = reply_backend
if not self . reply_backend :
self . reply_backend = MemoryHandlerBackend ( )
2018-05-20 23:40:25 +03:00
2015-07-02 23:32:18 +03:00
self . message_handlers = [ ]
2016-06-07 14:29:12 +03:00
self . edited_message_handlers = [ ]
2016-12-03 08:28:22 +03:00
self . channel_post_handlers = [ ]
self . edited_channel_post_handlers = [ ]
2016-01-04 18:10:32 +03:00
self . inline_handlers = [ ]
2016-01-05 08:18:32 +03:00
self . chosen_inline_handlers = [ ]
2016-04-16 09:18:19 +03:00
self . callback_query_handlers = [ ]
2017-05-25 06:22:40 +03:00
self . shipping_query_handlers = [ ]
self . pre_checkout_query_handlers = [ ]
2020-03-09 13:25:54 +03:00
self . poll_handlers = [ ]
2020-05-12 03:09:34 +03:00
self . poll_answer_handlers = [ ]
2015-10-02 01:00:54 +03:00
2020-04-08 21:13:19 +03:00
self . typed_middleware_handlers = {
' message ' : [ ] ,
' edited_message ' : [ ] ,
' channel_post ' : [ ] ,
' edited_channel_post ' : [ ] ,
' inline_query ' : [ ] ,
' chosen_inline_result ' : [ ] ,
' callback_query ' : [ ] ,
' shipping_query ' : [ ] ,
' pre_checkout_query ' : [ ] ,
' poll ' : [ ] ,
}
self . default_middleware_handlers = [ ]
2015-10-02 01:00:54 +03:00
self . threaded = threaded
if self . threaded :
2017-04-06 22:12:17 +03:00
self . worker_pool = util . ThreadPool ( num_threads = num_threads )
2015-07-02 04:38:31 +03:00
2018-05-27 01:30:14 +03:00
def enable_save_next_step_handlers ( self , delay = 120 , filename = " ./.handler-saves/step.save " ) :
2018-05-27 01:37:06 +03:00
"""
2020-04-12 20:41:32 +03:00
Enable saving next step handlers ( by default saving disabled )
This function explicitly assigns FileHandlerBackend ( instead of Saver ) just to keep backward
compatibility whose purpose was to enable file saving capability for handlers . And the same
implementation is now available with FileHandlerBackend
Most probably this function should be deprecated in future major releases
2018-05-27 01:37:06 +03:00
: param delay : Delay between changes in handlers and saving
: param filename : Filename of save file
"""
2020-04-12 20:41:32 +03:00
self . next_step_backend = FileHandlerBackend ( self . next_step_backend . handlers , filename , delay )
2018-05-20 23:40:25 +03:00
2018-05-27 01:30:14 +03:00
def enable_save_reply_handlers ( self , delay = 120 , filename = " ./.handler-saves/reply.save " ) :
2018-05-27 01:37:06 +03:00
"""
Enable saving reply handlers ( by default saving disable )
2020-04-12 20:41:32 +03:00
This function explicitly assigns FileHandlerBackend ( instead of Saver ) just to keep backward
compatibility whose purpose was to enable file saving capability for handlers . And the same
implementation is now available with FileHandlerBackend
Most probably this function should be deprecated in future major releases
2018-05-27 01:37:06 +03:00
: param delay : Delay between changes in handlers and saving
: param filename : Filename of save file
"""
2020-04-12 20:41:32 +03:00
self . reply_backend = FileHandlerBackend ( self . reply_backend . handlers , filename , delay )
2018-05-20 23:40:25 +03:00
2018-05-25 08:56:31 +03:00
def disable_save_next_step_handlers ( self ) :
2018-05-27 01:37:06 +03:00
"""
Disable saving next step handlers ( by default saving disable )
2020-04-12 20:41:32 +03:00
This function is left to keep backward compatibility whose purpose was to disable file saving capability
for handlers . For the same purpose , MemoryHandlerBackend is reassigned as a new next_step_backend backend
instead of FileHandlerBackend .
Most probably this function should be deprecated in future major releases
2018-05-27 01:37:06 +03:00
"""
2020-04-12 20:41:32 +03:00
self . next_step_backend = MemoryHandlerBackend ( self . next_step_backend . handlers )
2018-05-20 23:40:25 +03:00
2018-05-25 08:56:31 +03:00
def disable_save_reply_handlers ( self ) :
2018-05-27 01:37:06 +03:00
"""
Disable saving next step handlers ( by default saving disable )
2020-04-12 20:41:32 +03:00
This function is left to keep backward compatibility whose purpose was to disable file saving capability
for handlers . For the same purpose , MemoryHandlerBackend is reassigned as a new reply_backend backend
instead of FileHandlerBackend .
Most probably this function should be deprecated in future major releases
2018-05-27 01:37:06 +03:00
"""
2020-04-12 20:41:32 +03:00
self . reply_backend = MemoryHandlerBackend ( self . reply_backend . handlers )
2018-05-20 23:40:25 +03:00
2018-05-27 01:30:14 +03:00
def load_next_step_handlers ( self , filename = " ./.handler-saves/step.save " , del_file_after_loading = True ) :
2018-05-27 01:37:06 +03:00
"""
Load next step handlers from save file
2020-04-12 20:41:32 +03:00
This function is left to keep backward compatibility whose purpose was to load handlers from file with the
help of FileHandlerBackend and is only recommended to use if next_step_backend was assigned as
FileHandlerBackend before entering this function
Most probably this function should be deprecated in future major releases
2018-05-27 01:37:06 +03:00
: param filename : Filename of the file where handlers was saved
: param del_file_after_loading : Is passed True , after loading save file will be deleted
"""
2020-04-12 20:41:32 +03:00
self . next_step_backend . load_handlers ( filename , del_file_after_loading )
2018-05-20 23:40:25 +03:00
2018-05-27 01:30:14 +03:00
def load_reply_handlers ( self , filename = " ./.handler-saves/reply.save " , del_file_after_loading = True ) :
2018-05-27 01:37:06 +03:00
"""
Load reply handlers from save file
2020-04-12 20:41:32 +03:00
This function is left to keep backward compatibility whose purpose was to load handlers from file with the
help of FileHandlerBackend and is only recommended to use if reply_backend was assigned as
FileHandlerBackend before entering this function
Most probably this function should be deprecated in future major releases
2018-05-27 01:37:06 +03:00
: param filename : Filename of the file where handlers was saved
: param del_file_after_loading : Is passed True , after loading save file will be deleted
"""
2020-04-12 20:41:32 +03:00
self . reply_backend . load_handlers ( filename , del_file_after_loading )
2018-05-20 23:40:25 +03:00
2016-12-06 06:42:15 +03:00
def set_webhook ( self , url = None , certificate = None , max_connections = None , allowed_updates = None ) :
return apihelper . set_webhook ( self . token , url , certificate , max_connections , allowed_updates )
2015-09-18 21:12:53 +03:00
2016-12-06 06:52:16 +03:00
def delete_webhook ( self ) :
"""
Use this method to remove webhook integration if you decide to switch back to getUpdates .
: return : bool
"""
return apihelper . delete_webhook ( self . token )
2016-10-20 10:52:38 +03:00
def get_webhook_info ( self ) :
result = apihelper . get_webhook_info ( self . token )
return types . WebhookInfo . de_json ( result )
2015-09-18 21:12:53 +03:00
def remove_webhook ( self ) :
2015-10-02 01:00:54 +03:00
return self . set_webhook ( ) # No params resets webhook
2015-09-18 21:12:53 +03:00
2016-12-06 06:44:30 +03:00
def get_updates ( self , offset = None , limit = None , timeout = 20 , allowed_updates = None ) :
2015-09-05 13:10:11 +03:00
"""
Use this method to receive incoming updates using long polling ( wiki ) . An Array of Update objects is returned .
2016-12-06 06:44:30 +03:00
: param allowed_updates : Array of string . List the types of updates you want your bot to receive .
2015-09-05 13:10:11 +03:00
: param offset : Integer . Identifier of the first update to be returned .
: param limit : Integer . Limits the number of updates to be retrieved .
: param timeout : Integer . Timeout in seconds for long polling .
: return : array of Updates
"""
2016-12-06 06:44:30 +03:00
json_updates = apihelper . get_updates ( self . token , offset , limit , timeout , allowed_updates )
2015-09-05 13:10:11 +03:00
ret = [ ]
for ju in json_updates :
ret . append ( types . Update . de_json ( ju ) )
return ret
2015-12-01 17:05:52 +03:00
def __skip_updates ( self ) :
"""
Get and discard all pending updates before first poll of the bot
: return : total updates skipped
"""
total = 0
updates = self . get_updates ( offset = self . last_update_id , timeout = 1 )
while updates :
total + = len ( updates )
for update in updates :
if update . update_id > self . last_update_id :
self . last_update_id = update . update_id
2016-01-05 08:18:32 +03:00
updates = self . get_updates ( offset = self . last_update_id + 1 , timeout = 1 )
2015-12-01 17:05:52 +03:00
return total
2015-10-01 23:03:54 +03:00
def __retrieve_updates ( self , timeout = 20 ) :
2015-07-02 14:43:49 +03:00
"""
Retrieves any updates from the Telegram API .
Registered listeners and applicable message handlers will be notified when a new message arrives .
: raises ApiException when a call has failed .
"""
2015-12-01 17:05:52 +03:00
if self . skip_pending :
logger . debug ( ' Skipped {0} pending messages ' . format ( self . __skip_updates ( ) ) )
self . skip_pending = False
2015-09-30 18:18:26 +03:00
updates = self . get_updates ( offset = ( self . last_update_id + 1 ) , timeout = timeout )
2016-02-07 18:45:54 +03:00
self . process_new_updates ( updates )
def process_new_updates ( self , updates ) :
2015-07-01 20:12:12 +03:00
new_messages = [ ]
2020-04-24 18:19:30 +03:00
new_edited_messages = [ ]
2016-12-03 08:28:22 +03:00
new_channel_posts = [ ]
new_edited_channel_posts = [ ]
2016-01-04 18:10:32 +03:00
new_inline_querys = [ ]
2016-01-05 08:18:32 +03:00
new_chosen_inline_results = [ ]
2016-04-16 09:18:19 +03:00
new_callback_querys = [ ]
2017-05-25 06:45:44 +03:00
new_shipping_querys = [ ]
new_pre_checkout_querys = [ ]
2020-03-09 13:25:54 +03:00
new_polls = [ ]
2020-05-12 03:09:34 +03:00
new_poll_answers = [ ]
2017-05-25 06:22:40 +03:00
2015-06-26 13:02:30 +03:00
for update in updates :
2020-04-11 11:02:50 +03:00
if apihelper . ENABLE_MIDDLEWARE :
self . process_middlewares ( update )
2020-04-08 21:13:19 +03:00
2015-09-05 16:54:54 +03:00
if update . update_id > self . last_update_id :
self . last_update_id = update . update_id
2016-01-04 18:10:32 +03:00
if update . message :
new_messages . append ( update . message )
2016-06-07 14:29:12 +03:00
if update . edited_message :
2020-04-24 18:19:30 +03:00
new_edited_messages . append ( update . edited_message )
2016-12-03 08:28:22 +03:00
if update . channel_post :
new_channel_posts . append ( update . channel_post )
if update . edited_channel_post :
new_edited_channel_posts . append ( update . edited_channel_post )
2016-01-04 18:10:32 +03:00
if update . inline_query :
new_inline_querys . append ( update . inline_query )
2016-01-05 08:18:32 +03:00
if update . chosen_inline_result :
new_chosen_inline_results . append ( update . chosen_inline_result )
2016-04-16 09:18:19 +03:00
if update . callback_query :
new_callback_querys . append ( update . callback_query )
2017-05-25 06:22:40 +03:00
if update . shipping_query :
2017-05-25 06:45:44 +03:00
new_shipping_querys . append ( update . shipping_query )
2017-05-25 06:22:40 +03:00
if update . pre_checkout_query :
2017-05-25 06:45:44 +03:00
new_pre_checkout_querys . append ( update . pre_checkout_query )
2020-03-09 13:25:54 +03:00
if update . poll :
new_polls . append ( update . poll )
2020-05-12 03:09:34 +03:00
if update . poll_answer :
new_poll_answers . append ( update . poll_answer )
2017-05-25 06:22:40 +03:00
2016-01-04 18:10:32 +03:00
logger . debug ( ' Received {0} new updates ' . format ( len ( updates ) ) )
2015-07-01 20:12:12 +03:00
if len ( new_messages ) > 0 :
2015-07-14 08:28:39 +03:00
self . process_new_messages ( new_messages )
2020-04-24 18:19:30 +03:00
if len ( new_edited_messages ) > 0 :
self . process_new_edited_messages ( new_edited_messages )
2016-12-03 08:28:22 +03:00
if len ( new_channel_posts ) > 0 :
self . process_new_channel_posts ( new_channel_posts )
if len ( new_edited_channel_posts ) > 0 :
self . process_new_edited_channel_posts ( new_edited_channel_posts )
2016-01-04 18:10:32 +03:00
if len ( new_inline_querys ) > 0 :
self . process_new_inline_query ( new_inline_querys )
2016-01-05 08:18:32 +03:00
if len ( new_chosen_inline_results ) > 0 :
self . process_new_chosen_inline_query ( new_chosen_inline_results )
2016-04-16 09:18:19 +03:00
if len ( new_callback_querys ) > 0 :
self . process_new_callback_query ( new_callback_querys )
2017-05-25 06:45:44 +03:00
if len ( new_shipping_querys ) > 0 :
self . process_new_shipping_query ( new_shipping_querys )
2020-03-09 13:25:37 +03:00
if len ( new_pre_checkout_querys ) > 0 :
self . process_new_pre_checkout_query ( new_pre_checkout_querys )
2020-03-09 13:25:54 +03:00
if len ( new_polls ) > 0 :
self . process_new_poll ( new_polls )
2020-05-12 03:09:34 +03:00
if len ( new_poll_answers ) > 0 :
self . process_new_poll_answer ( new_poll_answers )
2015-07-14 08:28:39 +03:00
def process_new_messages ( self , new_messages ) :
2018-04-12 13:45:32 +03:00
self . _notify_next_handlers ( new_messages )
self . _notify_reply_handlers ( new_messages )
2015-07-14 08:28:39 +03:00
self . __notify_update ( new_messages )
2016-01-05 08:18:32 +03:00
self . _notify_command_handlers ( self . message_handlers , new_messages )
2015-06-26 13:02:30 +03:00
2016-06-07 14:29:12 +03:00
def process_new_edited_messages ( self , edited_message ) :
self . _notify_command_handlers ( self . edited_message_handlers , edited_message )
2016-12-03 08:28:22 +03:00
def process_new_channel_posts ( self , channel_post ) :
self . _notify_command_handlers ( self . channel_post_handlers , channel_post )
def process_new_edited_channel_posts ( self , edited_channel_post ) :
self . _notify_command_handlers ( self . edited_channel_post_handlers , edited_channel_post )
2016-01-04 18:10:32 +03:00
def process_new_inline_query ( self , new_inline_querys ) :
2016-01-05 08:18:32 +03:00
self . _notify_command_handlers ( self . inline_handlers , new_inline_querys )
def process_new_chosen_inline_query ( self , new_chosen_inline_querys ) :
self . _notify_command_handlers ( self . chosen_inline_handlers , new_chosen_inline_querys )
2016-01-04 18:10:32 +03:00
2016-04-16 09:18:19 +03:00
def process_new_callback_query ( self , new_callback_querys ) :
self . _notify_command_handlers ( self . callback_query_handlers , new_callback_querys )
2017-05-25 06:45:44 +03:00
def process_new_shipping_query ( self , new_shipping_querys ) :
self . _notify_command_handlers ( self . shipping_query_handlers , new_shipping_querys )
def process_new_pre_checkout_query ( self , pre_checkout_querys ) :
self . _notify_command_handlers ( self . pre_checkout_query_handlers , pre_checkout_querys )
2020-03-09 13:25:54 +03:00
def process_new_poll ( self , polls ) :
self . _notify_command_handlers ( self . poll_handlers , polls )
2020-05-12 03:09:34 +03:00
def process_new_poll_answer ( self , poll_answers ) :
self . _notify_command_handlers ( self . poll_answer_handlers , poll_answers )
2020-04-08 21:13:19 +03:00
def process_middlewares ( self , update ) :
for update_type , middlewares in self . typed_middleware_handlers . items ( ) :
2020-04-20 09:30:03 +03:00
if getattr ( update , update_type ) is not None :
2020-04-08 21:13:19 +03:00
for typed_middleware_handler in middlewares :
typed_middleware_handler ( self , getattr ( update , update_type ) )
if len ( self . default_middleware_handlers ) > 0 :
for default_middleware_handler in self . default_middleware_handlers :
default_middleware_handler ( self , update )
2015-06-26 13:02:30 +03:00
def __notify_update ( self , new_messages ) :
for listener in self . update_listener :
2016-06-13 16:47:15 +03:00
self . _exec_task ( listener , new_messages )
2015-06-26 13:02:30 +03:00
2018-11-17 14:19:09 +03:00
def infinity_polling ( self , timeout = 20 , * args , * * kwargs ) :
2018-04-04 10:47:37 +03:00
while not self . __stop_polling . is_set ( ) :
try :
2018-11-17 13:58:56 +03:00
self . polling ( timeout = timeout , * args , * * kwargs )
2020-04-24 18:19:30 +03:00
except Exception :
2018-11-17 13:58:56 +03:00
time . sleep ( timeout )
2018-04-04 10:47:37 +03:00
pass
logger . info ( " Break infinity polling " )
2015-10-03 13:48:56 +03:00
def polling ( self , none_stop = False , interval = 0 , timeout = 20 ) :
2015-06-26 13:02:30 +03:00
"""
2015-10-01 23:03:54 +03:00
This function creates a new Thread that calls an internal __retrieve_updates function .
2015-07-02 23:32:18 +03:00
This allows the bot to retrieve Updates automagically and notify listeners and message handlers accordingly .
2015-10-01 23:03:54 +03:00
Warning : Do not call this function more than once !
2015-07-02 23:32:18 +03:00
2015-06-26 13:02:30 +03:00
Always get updates .
2017-08-06 09:25:25 +03:00
: param interval :
2015-10-01 23:03:54 +03:00
: param none_stop : Do not stop polling when an ApiException occurs .
2015-09-30 18:18:26 +03:00
: param timeout : Timeout in seconds for long polling .
2015-06-26 13:02:30 +03:00
: return :
"""
2015-10-02 01:00:54 +03:00
if self . threaded :
self . __threaded_polling ( none_stop , interval , timeout )
else :
self . __non_threaded_polling ( none_stop , interval , timeout )
2015-08-31 03:13:21 +03:00
2015-10-02 01:00:54 +03:00
def __threaded_polling ( self , none_stop = False , interval = 0 , timeout = 3 ) :
logger . info ( ' Started polling. ' )
self . __stop_polling . clear ( )
2020-01-08 20:06:40 +03:00
error_interval = 0.25
2015-10-01 23:03:54 +03:00
polling_thread = util . WorkerThread ( name = " PollingThread " )
or_event = util . OrEvent (
2016-02-27 06:17:35 +03:00
polling_thread . done_event ,
polling_thread . exception_event ,
self . worker_pool . exception_event
2015-10-01 23:03:54 +03:00
)
2015-07-18 12:27:16 +03:00
while not self . __stop_polling . wait ( interval ) :
2015-10-01 23:03:54 +03:00
or_event . clear ( )
2015-06-27 17:31:40 +03:00
try :
2015-10-01 23:03:54 +03:00
polling_thread . put ( self . __retrieve_updates , timeout )
2015-10-02 01:00:54 +03:00
2015-10-03 13:48:56 +03:00
or_event . wait ( ) # wait for polling thread finish, polling thread error or thread pool error
2015-10-01 23:03:54 +03:00
polling_thread . raise_exceptions ( )
self . worker_pool . raise_exceptions ( )
2020-01-08 20:06:40 +03:00
error_interval = 0.25
2015-08-31 03:13:21 +03:00
except apihelper . ApiException as e :
2015-10-01 23:03:54 +03:00
logger . error ( e )
2015-07-12 10:49:22 +03:00
if not none_stop :
2015-07-18 12:27:16 +03:00
self . __stop_polling . set ( )
2015-09-08 20:47:55 +03:00
logger . info ( " Exception occurred. Stopping. " )
2015-08-31 03:13:21 +03:00
else :
2015-10-01 23:03:54 +03:00
polling_thread . clear_exceptions ( )
self . worker_pool . clear_exceptions ( )
logger . info ( " Waiting for {0} seconds until retry " . format ( error_interval ) )
2015-08-31 03:13:21 +03:00
time . sleep ( error_interval )
error_interval * = 2
2015-10-02 01:00:54 +03:00
except KeyboardInterrupt :
logger . info ( " KeyboardInterrupt received. " )
self . __stop_polling . set ( )
break
2015-06-26 13:02:30 +03:00
2018-03-23 14:58:43 +03:00
polling_thread . stop ( )
2015-09-08 20:47:55 +03:00
logger . info ( ' Stopped polling. ' )
2015-06-26 13:02:30 +03:00
2015-10-02 01:00:54 +03:00
def __non_threaded_polling ( self , none_stop = False , interval = 0 , timeout = 3 ) :
logger . info ( ' Started polling. ' )
self . __stop_polling . clear ( )
2020-01-08 20:06:40 +03:00
error_interval = 0.25
2015-10-02 01:00:54 +03:00
while not self . __stop_polling . wait ( interval ) :
try :
self . __retrieve_updates ( timeout )
2020-01-08 20:06:40 +03:00
error_interval = 0.25
2015-10-02 01:00:54 +03:00
except apihelper . ApiException as e :
logger . error ( e )
if not none_stop :
self . __stop_polling . set ( )
logger . info ( " Exception occurred. Stopping. " )
else :
logger . info ( " Waiting for {0} seconds until retry " . format ( error_interval ) )
time . sleep ( error_interval )
error_interval * = 2
except KeyboardInterrupt :
logger . info ( " KeyboardInterrupt received. " )
self . __stop_polling . set ( )
break
logger . info ( ' Stopped polling. ' )
2016-06-13 16:47:15 +03:00
def _exec_task ( self , task , * args , * * kwargs ) :
2015-10-02 01:00:54 +03:00
if self . threaded :
self . worker_pool . put ( task , * args , * * kwargs )
else :
task ( * args , * * kwargs )
2015-06-26 13:02:30 +03:00
def stop_polling ( self ) :
2015-07-18 12:27:16 +03:00
self . __stop_polling . set ( )
2015-06-26 13:02:30 +03:00
2018-03-23 14:58:43 +03:00
def stop_bot ( self ) :
self . stop_polling ( )
if self . worker_pool :
self . worker_pool . close ( )
2015-06-26 13:02:30 +03:00
def set_update_listener ( self , listener ) :
self . update_listener . append ( listener )
2015-06-26 09:55:13 +03:00
def get_me ( self ) :
2015-06-26 10:46:02 +03:00
result = apihelper . get_me ( self . token )
2015-07-01 23:16:13 +03:00
return types . User . de_json ( result )
2015-06-30 17:40:44 +03:00
2015-09-18 21:31:29 +03:00
def get_file ( self , file_id ) :
return types . File . de_json ( apihelper . get_file ( self . token , file_id ) )
2018-03-25 13:22:35 +03:00
def get_file_url ( self , file_id ) :
return apihelper . get_file_url ( self . token , file_id )
2015-09-18 21:53:10 +03:00
def download_file ( self , file_path ) :
return apihelper . download_file ( self . token , file_path )
2015-06-30 17:40:44 +03:00
def get_user_profile_photos ( self , user_id , offset = None , limit = None ) :
"""
Retrieves the user profile photos of the person with ' user_id '
See https : / / core . telegram . org / bots / api #getuserprofilephotos
: param user_id :
: param offset :
: param limit :
2015-07-02 23:32:18 +03:00
: return : API reply .
2015-06-30 17:40:44 +03:00
"""
result = apihelper . get_user_profile_photos ( self . token , user_id , offset , limit )
2015-07-01 23:16:13 +03:00
return types . UserProfilePhotos . de_json ( result )
2015-06-26 09:55:13 +03:00
2016-06-07 14:00:44 +03:00
def get_chat ( self , chat_id ) :
2016-06-07 14:44:30 +03:00
"""
Use this method to get up to date information about the chat ( current name of the user for one - on - one
conversations , current username of a user , group or channel , etc . ) . Returns a Chat object on success .
: param chat_id :
: return :
"""
2016-06-07 14:00:44 +03:00
result = apihelper . get_chat ( self . token , chat_id )
return types . Chat . de_json ( result )
def leave_chat ( self , chat_id ) :
2016-06-07 14:44:30 +03:00
"""
Use this method for your bot to leave a group , supergroup or channel . Returns True on success .
: param chat_id :
: return :
"""
2016-06-07 14:00:44 +03:00
result = apihelper . leave_chat ( self . token , chat_id )
return result
def get_chat_administrators ( self , chat_id ) :
2016-06-07 14:44:30 +03:00
"""
2020-05-11 16:38:09 +03:00
Use this method to get a list of administrators in a chat .
On success , returns an Array of ChatMember objects that contains
information about all chat administrators except other bots .
: param chat_id : Unique identifier for the target chat or username
of the target supergroup or channel ( in the format @channelusername )
2016-06-07 14:44:30 +03:00
: return :
"""
2016-06-07 14:00:44 +03:00
result = apihelper . get_chat_administrators ( self . token , chat_id )
2016-06-07 14:08:52 +03:00
ret = [ ]
for r in result :
ret . append ( types . ChatMember . de_json ( r ) )
return ret
2016-06-07 14:00:44 +03:00
def get_chat_members_count ( self , chat_id ) :
2016-06-07 14:44:30 +03:00
"""
Use this method to get the number of members in a chat . Returns Int on success .
: param chat_id :
: return :
"""
2016-06-07 14:00:44 +03:00
result = apihelper . get_chat_members_count ( self . token , chat_id )
return result
2017-10-22 19:50:51 +03:00
def set_chat_sticker_set ( self , chat_id , sticker_set_name ) :
"""
Use this method to set a new group sticker set for a supergroup . The bot must be an administrator
in the chat for this to work and must have the appropriate admin rights .
Use the field can_set_sticker_set optionally returned in getChat requests to check
if the bot can use this method . Returns True on success .
: param chat_id : Unique identifier for the target chat or username of the target supergroup
( in the format @supergroupusername )
: param sticker_set_name : Name of the sticker set to be set as the group sticker set
: return :
"""
result = apihelper . set_chat_sticker_set ( self . token , chat_id , sticker_set_name )
return result
def delete_chat_sticker_set ( self , chat_id ) :
"""
Use this method to delete a group sticker set from a supergroup . The bot must be an administrator in the chat
for this to work and must have the appropriate admin rights . Use the field can_set_sticker_set
optionally returned in getChat requests to check if the bot can use this method . Returns True on success .
: param chat_id : Unique identifier for the target chat or username of the target supergroup
( in the format @supergroupusername )
: return :
"""
result = apihelper . delete_chat_sticker_set ( self . token , chat_id )
return result
2016-06-07 14:00:44 +03:00
def get_chat_member ( self , chat_id , user_id ) :
2016-06-07 14:44:30 +03:00
"""
Use this method to get information about a member of a chat . Returns a ChatMember object on success .
: param chat_id :
: param user_id :
: return :
"""
2016-06-07 14:29:12 +03:00
result = apihelper . get_chat_member ( self . token , chat_id , user_id )
2016-06-07 14:00:44 +03:00
return types . ChatMember . de_json ( result )
2015-09-08 22:51:45 +03:00
def send_message ( self , chat_id , text , disable_web_page_preview = None , reply_to_message_id = None , reply_markup = None ,
2018-07-24 00:33:13 +03:00
parse_mode = None , disable_notification = None , timeout = None ) :
2015-06-28 12:27:25 +03:00
"""
Use this method to send text messages .
2015-07-25 21:59:36 +03:00
Warning : Do not send more than about 5000 characters each message , otherwise you ' ll risk an HTTP 414 error.
If you must send more than 5000 characters , use the split_string function in apihelper . py .
2015-06-28 12:27:25 +03:00
: param chat_id :
: param text :
: param disable_web_page_preview :
: param reply_to_message_id :
: param reply_markup :
2015-09-08 22:51:45 +03:00
: param parse_mode :
2016-02-27 06:17:35 +03:00
: param disable_notification : Boolean , Optional . Sends the message silently .
2020-04-24 18:19:30 +03:00
: param timeout :
2015-07-02 23:32:18 +03:00
: return : API reply .
2015-06-28 12:27:25 +03:00
"""
2020-07-04 21:07:42 +03:00
parse_mode = self . parse_mode if not parse_mode else parse_mode
2015-07-03 02:42:47 +03:00
return types . Message . de_json (
2016-02-27 06:17:35 +03:00
apihelper . send_message ( self . token , chat_id , text , disable_web_page_preview , reply_to_message_id ,
2018-07-24 00:33:13 +03:00
reply_markup , parse_mode , disable_notification , timeout ) )
2015-06-26 17:16:11 +03:00
2020-05-16 17:34:56 +03:00
def forward_message ( self , chat_id , from_chat_id , message_id , disable_notification = None , timeout = None ) :
2015-06-26 17:35:52 +03:00
"""
2015-06-26 20:53:07 +03:00
Use this method to forward messages of any kind .
2016-02-27 06:17:35 +03:00
: param disable_notification :
2015-06-26 17:35:52 +03:00
: param chat_id : which chat to forward
: param from_chat_id : which chat message from
: param message_id : message id
2015-07-02 23:32:18 +03:00
: return : API reply .
2015-06-26 17:35:52 +03:00
"""
2016-02-27 06:17:35 +03:00
return types . Message . de_json (
2020-05-16 17:34:56 +03:00
apihelper . forward_message ( self . token , chat_id , from_chat_id , message_id , disable_notification , timeout ) )
2015-06-26 20:53:07 +03:00
2020-07-21 01:20:01 +03:00
def delete_message ( self , chat_id , message_id , timeout = None ) :
2017-05-07 17:37:03 +03:00
"""
2018-04-04 10:47:37 +03:00
Use this method to delete message . Returns True on success .
2017-05-07 17:37:03 +03:00
: param chat_id : in which chat to delete
: param message_id : which message to delete
: return : API reply .
"""
2020-07-21 01:20:01 +03:00
return apihelper . delete_message ( self . token , chat_id , message_id , timeout )
2017-05-07 17:37:03 +03:00
2020-05-16 17:34:56 +03:00
def send_dice (
self , chat_id ,
emoji = None , disable_notification = None , reply_to_message_id = None ,
reply_markup = None , timeout = None ) :
2020-04-15 08:10:05 +03:00
"""
Use this method to send dices .
: param chat_id :
2020-04-27 08:30:05 +03:00
: param emoji :
2020-04-15 08:10:05 +03:00
: param disable_notification :
: param reply_to_message_id :
: param reply_markup :
: return : Message
"""
return types . Message . de_json (
2020-05-16 17:34:56 +03:00
apihelper . send_dice (
self . token , chat_id , emoji , disable_notification , reply_to_message_id ,
reply_markup , timeout )
2020-04-15 08:10:05 +03:00
)
2016-02-27 06:17:35 +03:00
def send_photo ( self , chat_id , photo , caption = None , reply_to_message_id = None , reply_markup = None ,
2020-05-16 17:34:56 +03:00
parse_mode = None , disable_notification = None , timeout = None ) :
2015-06-26 21:14:45 +03:00
"""
Use this method to send photos .
2017-08-06 09:25:25 +03:00
: param disable_notification :
2015-06-26 21:14:45 +03:00
: param chat_id :
: param photo :
: param caption :
2018-02-14 23:27:55 +03:00
: param parse_mode
2015-06-26 21:14:45 +03:00
: param reply_to_message_id :
: param reply_markup :
2015-07-02 23:32:18 +03:00
: return : API reply .
2015-06-26 21:14:45 +03:00
"""
2020-07-04 21:07:42 +03:00
parse_mode = self . parse_mode if not parse_mode else parse_mode
2015-07-03 02:42:47 +03:00
return types . Message . de_json (
2016-02-27 06:17:35 +03:00
apihelper . send_photo ( self . token , chat_id , photo , caption , reply_to_message_id , reply_markup ,
2020-05-16 17:34:56 +03:00
parse_mode , disable_notification , timeout ) )
2015-06-26 20:53:07 +03:00
2016-10-29 16:22:46 +03:00
def send_audio ( self , chat_id , audio , caption = None , duration = None , performer = None , title = None ,
2018-02-16 18:29:29 +03:00
reply_to_message_id = None , reply_markup = None , parse_mode = None , disable_notification = None ,
2020-06-23 21:12:46 +03:00
timeout = None , thumb = None ) :
2015-06-26 21:14:45 +03:00
"""
2015-08-19 13:27:35 +03:00
Use this method to send audio files , if you want Telegram clients to display them in the music player . Your audio must be in the . mp3 format .
: param chat_id : Unique identifier for the message recipient
: param audio : Audio file to send .
2020-01-08 20:06:40 +03:00
: param caption :
2015-08-19 13:27:35 +03:00
: param duration : Duration of the audio in seconds
: param performer : Performer
: param title : Track name
2015-08-31 12:46:18 +03:00
: param reply_to_message_id : If the message is a reply , ID of the original message
2015-06-26 21:14:45 +03:00
: param reply_markup :
2020-01-08 20:06:40 +03:00
: param parse_mode
: param disable_notification :
: param timeout :
2020-06-23 21:14:52 +03:00
: param thumb :
2015-08-19 13:27:35 +03:00
: return : Message
2015-06-26 21:14:45 +03:00
"""
2015-07-03 02:42:47 +03:00
return types . Message . de_json (
2016-10-12 10:52:34 +03:00
apihelper . send_audio ( self . token , chat_id , audio , caption , duration , performer , title , reply_to_message_id ,
2020-06-23 21:12:46 +03:00
reply_markup , parse_mode , disable_notification , timeout , thumb ) )
2015-08-19 13:08:01 +03:00
2016-10-12 10:52:34 +03:00
def send_voice ( self , chat_id , voice , caption = None , duration = None , reply_to_message_id = None , reply_markup = None ,
2018-02-14 23:27:55 +03:00
parse_mode = None , disable_notification = None , timeout = None ) :
2015-08-19 13:27:35 +03:00
"""
Use this method to send audio files , if you want Telegram clients to display the file as a playable voice message .
: param chat_id : Unique identifier for the message recipient .
: param voice :
2020-01-08 20:06:40 +03:00
: param caption :
2015-08-19 13:27:35 +03:00
: param duration : Duration of sent audio in seconds
: param reply_to_message_id :
: param reply_markup :
2018-02-14 23:27:55 +03:00
: param parse_mode
2020-01-08 20:06:40 +03:00
: param disable_notification :
: param timeout :
2015-08-19 13:27:35 +03:00
: return : Message
"""
2020-07-04 21:07:42 +03:00
parse_mode = self . parse_mode if not parse_mode else parse_mode
2015-08-19 13:08:01 +03:00
return types . Message . de_json (
2016-10-12 10:52:34 +03:00
apihelper . send_voice ( self . token , chat_id , voice , caption , duration , reply_to_message_id , reply_markup ,
2018-02-14 23:27:55 +03:00
parse_mode , disable_notification , timeout ) )
2015-06-26 20:53:07 +03:00
2016-06-07 14:29:12 +03:00
def send_document ( self , chat_id , data , reply_to_message_id = None , caption = None , reply_markup = None ,
2018-02-14 23:27:55 +03:00
parse_mode = None , disable_notification = None , timeout = None ) :
2015-06-26 21:14:45 +03:00
"""
Use this method to send general files .
2015-07-24 14:56:47 +03:00
: param chat_id :
2015-06-26 21:14:45 +03:00
: param data :
: param reply_to_message_id :
2020-01-08 20:06:40 +03:00
: param caption :
2015-06-26 21:14:45 +03:00
: param reply_markup :
2018-02-16 18:29:29 +03:00
: param parse_mode :
: param disable_notification :
2020-01-08 20:06:40 +03:00
: param timeout :
2015-07-02 23:32:18 +03:00
: return : API reply .
2015-06-26 21:14:45 +03:00
"""
2020-07-04 21:07:42 +03:00
parse_mode = self . parse_mode if not parse_mode else parse_mode
2015-07-03 02:42:47 +03:00
return types . Message . de_json (
2016-02-27 06:17:35 +03:00
apihelper . send_data ( self . token , chat_id , data , ' document ' , reply_to_message_id , reply_markup ,
2018-02-14 23:27:55 +03:00
parse_mode , disable_notification , timeout , caption = caption ) )
2015-06-26 20:53:07 +03:00
2020-05-16 17:34:56 +03:00
def send_sticker (
self , chat_id , data , reply_to_message_id = None , reply_markup = None ,
disable_notification = None , timeout = None ) :
2015-06-26 21:14:45 +03:00
"""
Use this method to send . webp stickers .
: param chat_id :
: param data :
: param reply_to_message_id :
: param reply_markup :
2020-01-08 20:06:40 +03:00
: param disable_notification : to disable the notification
: param timeout : timeout
2015-07-02 23:32:18 +03:00
: return : API reply .
2015-06-26 21:14:45 +03:00
"""
2015-07-03 02:42:47 +03:00
return types . Message . de_json (
2020-05-16 17:34:56 +03:00
apihelper . send_data (
self . token , chat_id , data , ' sticker ' , reply_to_message_id , reply_markup ,
disable_notification , timeout ) )
2015-06-26 20:53:07 +03:00
2016-02-27 06:17:35 +03:00
def send_video ( self , chat_id , data , duration = None , caption = None , reply_to_message_id = None , reply_markup = None ,
2020-07-02 17:47:38 +03:00
parse_mode = None , supports_streaming = None , disable_notification = None , timeout = None , thumb = None , width = None , height = None ) :
2015-06-26 21:14:45 +03:00
"""
Use this method to send video files , Telegram clients support mp4 videos .
2015-08-01 05:12:15 +03:00
: param chat_id : Integer : Unique identifier for the message recipient — User or GroupChat id
: param data : InputFile or String : Video to send . You can either pass a file_id as String to resend a video that is already on the Telegram server
: param duration : Integer : Duration of sent video in seconds
: param caption : String : Video caption ( may also be used when resending videos by file_id ) .
2018-02-14 23:27:55 +03:00
: param parse_mode :
: param supports_streaming :
2015-06-26 21:14:45 +03:00
: param reply_to_message_id :
: param reply_markup :
2020-01-08 20:06:40 +03:00
: param disable_notification :
: param timeout :
2020-07-04 21:07:42 +03:00
: param thumb :
2015-08-01 05:12:15 +03:00
: return :
2015-06-26 21:14:45 +03:00
"""
2020-07-04 21:07:42 +03:00
parse_mode = self . parse_mode if not parse_mode else parse_mode
2015-07-03 02:42:47 +03:00
return types . Message . de_json (
2016-02-27 06:17:35 +03:00
apihelper . send_video ( self . token , chat_id , data , duration , caption , reply_to_message_id , reply_markup ,
2020-07-02 17:47:38 +03:00
parse_mode , supports_streaming , disable_notification , timeout , thumb , width , height ) )
2015-06-27 16:55:45 +03:00
2019-02-15 21:46:18 +03:00
def send_animation ( self , chat_id , animation , duration = None , caption = None , reply_to_message_id = None , reply_markup = None ,
parse_mode = None , disable_notification = None , timeout = None ) :
"""
Use this method to send animation files ( GIF or H .264 / MPEG - 4 AVC video without sound ) .
: param chat_id : Integer : Unique identifier for the message recipient — User or GroupChat id
2020-04-24 18:19:30 +03:00
: param animation : InputFile or String : Animation to send . You can either pass a file_id as String to resend an animation that is already on the Telegram server
2019-02-15 21:46:18 +03:00
: param duration : Integer : Duration of sent video in seconds
: param caption : String : Animation caption ( may also be used when resending animation by file_id ) .
: param parse_mode :
: param reply_to_message_id :
: param reply_markup :
2020-04-24 18:19:30 +03:00
: param disable_notification :
: param timeout :
2019-02-15 21:46:18 +03:00
: return :
"""
2020-07-04 21:07:42 +03:00
parse_mode = self . parse_mode if not parse_mode else parse_mode
2019-02-15 21:46:18 +03:00
return types . Message . de_json (
apihelper . send_animation ( self . token , chat_id , animation , duration , caption , reply_to_message_id , reply_markup ,
parse_mode , disable_notification , timeout ) )
2017-05-19 17:19:15 +03:00
def send_video_note ( self , chat_id , data , duration = None , length = None , reply_to_message_id = None , reply_markup = None ,
2017-05-25 05:56:58 +03:00
disable_notification = None , timeout = None ) :
2017-05-19 17:19:15 +03:00
"""
Use this method to send video files , Telegram clients support mp4 videos .
: param chat_id : Integer : Unique identifier for the message recipient — User or GroupChat id
: param data : InputFile or String : Video note to send . You can either pass a file_id as String to resend a video that is already on the Telegram server
: param duration : Integer : Duration of sent video in seconds
: param length : Integer : Video width and height , Can ' t be None and should be in range of (0, 640)
: param reply_to_message_id :
: param reply_markup :
2020-01-08 20:06:40 +03:00
: param disable_notification :
: param timeout :
2017-05-19 17:19:15 +03:00
: return :
"""
return types . Message . de_json (
apihelper . send_video_note ( self . token , chat_id , data , duration , length , reply_to_message_id , reply_markup ,
2017-05-25 05:56:58 +03:00
disable_notification , timeout ) )
2017-05-19 17:19:15 +03:00
2020-05-16 17:34:56 +03:00
def send_media_group (
self , chat_id , media ,
disable_notification = None , reply_to_message_id = None , timeout = None ) :
2017-11-29 08:45:25 +03:00
"""
send a group of photos or videos as an album . On success , an array of the sent Messages is returned .
: param chat_id :
: param media :
: param disable_notification :
: param reply_to_message_id :
: return :
"""
2020-05-16 17:34:56 +03:00
result = apihelper . send_media_group (
self . token , chat_id , media , disable_notification , reply_to_message_id , timeout )
2017-11-29 08:45:25 +03:00
ret = [ ]
for msg in result :
ret . append ( types . Message . de_json ( msg ) )
return ret
2020-05-16 17:34:56 +03:00
def send_location (
self , chat_id , latitude , longitude , live_period = None , reply_to_message_id = None ,
reply_markup = None , disable_notification = None , timeout = None ) :
2015-06-28 12:27:25 +03:00
"""
Use this method to send point on the map .
: param chat_id :
: param latitude :
: param longitude :
2017-10-22 19:50:51 +03:00
: param live_period
2015-06-28 12:27:25 +03:00
: param reply_to_message_id :
: param reply_markup :
2020-01-08 20:06:40 +03:00
: param disable_notification :
2015-07-02 23:32:18 +03:00
: return : API reply .
2015-06-28 12:27:25 +03:00
"""
2015-07-03 02:42:47 +03:00
return types . Message . de_json (
2020-05-16 17:34:56 +03:00
apihelper . send_location (
self . token , chat_id , latitude , longitude , live_period , reply_to_message_id ,
reply_markup , disable_notification , timeout ) )
2015-06-28 12:56:32 +03:00
2017-10-22 20:07:51 +03:00
def edit_message_live_location ( self , latitude , longitude , chat_id = None , message_id = None ,
2020-05-16 17:34:56 +03:00
inline_message_id = None , reply_markup = None , timeout = None ) :
2017-10-22 20:07:51 +03:00
"""
Use this method to edit live location
: param latitude :
: param longitude :
: param chat_id :
: param message_id :
: param inline_message_id :
: param reply_markup :
: return :
"""
return types . Message . de_json (
2020-05-16 17:34:56 +03:00
apihelper . edit_message_live_location (
self . token , latitude , longitude , chat_id , message_id ,
inline_message_id , reply_markup , timeout ) )
2017-10-22 20:07:51 +03:00
2020-05-16 17:34:56 +03:00
def stop_message_live_location (
self , chat_id = None , message_id = None ,
inline_message_id = None , reply_markup = None , timeout = None ) :
2017-10-22 20:07:51 +03:00
"""
Use this method to stop updating a live location message sent by the bot
or via the bot ( for inline bots ) before live_period expires
: param chat_id :
: param message_id :
: param inline_message_id :
: param reply_markup :
: return :
"""
return types . Message . de_json (
2020-05-16 17:34:56 +03:00
apihelper . stop_message_live_location (
self . token , chat_id , message_id , inline_message_id , reply_markup , timeout ) )
2017-10-22 20:07:51 +03:00
2020-05-16 17:34:56 +03:00
def send_venue (
self , chat_id , latitude , longitude , title , address , foursquare_id = None , disable_notification = None ,
reply_to_message_id = None , reply_markup = None , timeout = None ) :
2016-04-14 09:48:26 +03:00
"""
Use this method to send information about a venue .
: param chat_id : Integer or String : Unique identifier for the target chat or username of the target channel
: param latitude : Float : Latitude of the venue
: param longitude : Float : Longitude of the venue
: param title : String : Name of the venue
: param address : String : Address of the venue
: param foursquare_id : String : Foursquare identifier of the venue
: param disable_notification :
: param reply_to_message_id :
: param reply_markup :
: return :
"""
2016-04-14 08:55:28 +03:00
return types . Message . de_json (
2020-05-16 17:34:56 +03:00
apihelper . send_venue (
self . token , chat_id , latitude , longitude , title , address , foursquare_id ,
disable_notification , reply_to_message_id , reply_markup , timeout )
2016-04-14 08:55:28 +03:00
)
2020-05-16 17:34:56 +03:00
def send_contact (
self , chat_id , phone_number , first_name ,
last_name = None , disable_notification = None ,
reply_to_message_id = None , reply_markup = None , timeout = None ) :
2016-04-16 10:07:52 +03:00
return types . Message . de_json (
2020-05-16 17:34:56 +03:00
apihelper . send_contact (
self . token , chat_id , phone_number , first_name , last_name , disable_notification ,
reply_to_message_id , reply_markup , timeout )
2016-04-16 10:07:52 +03:00
)
2020-05-16 17:34:56 +03:00
def send_chat_action ( self , chat_id , action , timeout = None ) :
2015-06-28 12:56:32 +03:00
"""
Use this method when you need to tell the user that something is happening on the bot ' s side.
The status is set for 5 seconds or less ( when a message arrives from your bot , Telegram clients clear
its typing status ) .
: param chat_id :
2015-07-02 23:32:18 +03:00
: param action : One of the following strings : ' typing ' , ' upload_photo ' , ' record_video ' , ' upload_video ' ,
2017-05-21 14:27:31 +03:00
' record_audio ' , ' upload_audio ' , ' upload_document ' , ' find_location ' , ' record_video_note ' , ' upload_video_note ' .
2015-07-03 04:58:48 +03:00
: return : API reply . : type : boolean
2015-06-28 12:56:32 +03:00
"""
2020-05-16 17:34:56 +03:00
return apihelper . send_chat_action ( self . token , chat_id , action , timeout )
2015-07-02 04:38:31 +03:00
2017-07-01 06:11:25 +03:00
def kick_chat_member ( self , chat_id , user_id , until_date = None ) :
2016-04-14 09:48:26 +03:00
"""
Use this method to kick a user from a group or a supergroup .
: param chat_id : Int or string : Unique identifier for the target group or username of the target supergroup
: param user_id : Int : Unique identifier of the target user
2017-07-01 06:11:25 +03:00
: param until_date : Date when the user will be unbanned , unix time . If user is banned for more than 366 days or
less than 30 seconds from the current time they are considered to be banned forever
2018-07-25 12:44:18 +03:00
: return : boolean
2016-04-14 09:48:26 +03:00
"""
2017-07-01 06:11:25 +03:00
return apihelper . kick_chat_member ( self . token , chat_id , user_id , until_date )
2016-04-14 09:48:26 +03:00
def unban_chat_member ( self , chat_id , user_id ) :
2020-01-08 20:06:40 +03:00
"""
Removes member from the ban
: param chat_id :
: param user_id :
: return :
"""
2016-04-14 09:48:26 +03:00
return apihelper . unban_chat_member ( self . token , chat_id , user_id )
2020-05-16 17:34:56 +03:00
def restrict_chat_member (
self , chat_id , user_id , until_date = None ,
can_send_messages = None , can_send_media_messages = None ,
can_send_polls = None , can_send_other_messages = None ,
can_add_web_page_previews = None , can_change_info = None ,
can_invite_users = None , can_pin_messages = None ) :
2017-06-30 19:47:09 +03:00
"""
Use this method to restrict a user in a supergroup .
The bot must be an administrator in the supergroup for this to work and must have
the appropriate admin rights . Pass True for all boolean parameters to lift restrictions from a user .
Returns True on success .
: param chat_id : Int or String : Unique identifier for the target group or username of the target supergroup
or channel ( in the format @channelusername )
: param user_id : Int : Unique identifier of the target user
2017-06-30 20:16:51 +03:00
: param until_date : Date when restrictions will be lifted for the user , unix time .
If user is restricted for more than 366 days or less than 30 seconds from the current time ,
they are considered to be restricted forever
: param can_send_messages : Pass True , if the user can send text messages , contacts , locations and venues
: param can_send_media_messages Pass True , if the user can send audios , documents , photos , videos , video notes
and voice notes , implies can_send_messages
2020-05-16 17:34:56 +03:00
: param can_send_polls : Pass True , if the user is allowed to send polls , implies can_send_messages
2017-06-30 20:16:51 +03:00
: param can_send_other_messages : Pass True , if the user can send animations , games , stickers and
use inline bots , implies can_send_media_messages
: param can_add_web_page_previews : Pass True , if the user may add web page previews to their messages ,
implies can_send_media_messages
2020-05-16 17:34:56 +03:00
: param can_change_info : Pass True , if the user is allowed to change the chat title , photo and other settings . Ignored in public supergroups
: param can_invite_users : Pass True , if the user is allowed to invite new users to the chat ,
implies can_invite_users
: param can_pin_messages : Pass True , if the user is allowed to pin messages . Ignored in public supergroups
2017-06-30 19:47:09 +03:00
: return : types . Message
"""
2020-05-16 17:34:56 +03:00
return apihelper . restrict_chat_member (
self . token , chat_id , user_id , until_date ,
can_send_messages , can_send_media_messages ,
can_send_polls , can_send_other_messages ,
can_add_web_page_previews , can_change_info ,
can_invite_users , can_pin_messages )
2017-06-30 19:47:09 +03:00
2017-07-02 16:08:36 +03:00
def promote_chat_member ( self , chat_id , user_id , can_change_info = None , can_post_messages = None ,
can_edit_messages = None , can_delete_messages = None , can_invite_users = None ,
can_restrict_members = None , can_pin_messages = None , can_promote_members = None ) :
2017-06-30 19:47:09 +03:00
"""
Use this method to promote or demote a user in a supergroup or a channel . The bot must be an administrator
in the chat for this to work and must have the appropriate admin rights .
Pass False for all boolean parameters to demote a user . Returns True on success .
: param chat_id : Unique identifier for the target chat or username of the target channel (
in the format @channelusername )
: param user_id : Int : Unique identifier of the target user
: param can_change_info : Bool : Pass True , if the administrator can change chat title , photo and other settings
: param can_post_messages : Bool : Pass True , if the administrator can create channel posts , channels only
: param can_edit_messages : Bool : Pass True , if the administrator can edit messages of other users , channels only
: param can_delete_messages : Bool : Pass True , if the administrator can delete messages of other users
: param can_invite_users : Bool : Pass True , if the administrator can invite new users to the chat
: param can_restrict_members : Bool : Pass True , if the administrator can restrict , ban or unban chat members
: param can_pin_messages : Bool : Pass True , if the administrator can pin messages , supergroups only
: param can_promote_members : Bool : Pass True , if the administrator can add new administrators with a subset
of his own privileges or demote administrators that he has promoted , directly or indirectly
( promoted by administrators that were appointed by him )
: return :
"""
return apihelper . promote_chat_member ( self . token , chat_id , user_id , can_change_info , can_post_messages ,
can_edit_messages , can_delete_messages , can_invite_users ,
can_restrict_members , can_pin_messages , can_promote_members )
2020-05-09 23:23:08 +03:00
def set_chat_administrator_custom_title ( self , chat_id , user_id , custom_title ) :
"""
Use this method to set a custom title for an administrator
in a supergroup promoted by the bot .
Returns True on success .
: param chat_id : Unique identifier for the target chat or username of the target supergroup
( in the format @supergroupusername )
: param user_id : Unique identifier of the target user
: param custom_title : New custom title for the administrator ;
0 - 16 characters , emoji are not allowed
: return :
"""
return apihelper . set_chat_administrator_custom_title ( self . token , chat_id , user_id , custom_title )
2020-05-11 16:38:09 +03:00
def set_chat_permissions ( self , chat_id , permissions ) :
"""
Use this method to set default chat permissions for all members .
The bot must be an administrator in the group or a supergroup for this to work
and must have the can_restrict_members admin rights .
: param chat_id : Unique identifier for the target chat or username of the target supergroup
( in the format @supergroupusername )
: param permissions : New default chat permissions
: return :
"""
return apihelper . set_chat_permissions ( self . token , chat_id , permissions )
2017-06-30 19:47:09 +03:00
def export_chat_invite_link ( self , chat_id ) :
"""
Use this method to export an invite link to a supergroup or a channel . The bot must be an administrator
in the chat for this to work and must have the appropriate admin rights .
Returns exported invite link as String on success .
: param chat_id : Id : Unique identifier for the target chat or username of the target channel
( in the format @channelusername )
: return :
"""
return apihelper . export_chat_invite_link ( self . token , chat_id )
def set_chat_photo ( self , chat_id , photo ) :
"""
Use this method to set a new profile photo for the chat . Photos can ' t be changed for private chats.
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights .
Returns True on success .
Note : In regular groups ( non - supergroups ) , this method will only work if the ‘ All Members Are Admins ’
setting is off in the target group .
: param chat_id : Int or Str : Unique identifier for the target chat or username of the target channel
( in the format @channelusername )
: param photo : InputFile : New chat photo , uploaded using multipart / form - data
: return :
"""
return apihelper . set_chat_photo ( self . token , chat_id , photo )
def delete_chat_photo ( self , chat_id ) :
"""
Use this method to delete a chat photo . Photos can ' t be changed for private chats.
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights .
Returns True on success .
Note : In regular groups ( non - supergroups ) , this method will only work if the ‘ All Members Are Admins ’
setting is off in the target group .
: param chat_id : Int or Str : Unique identifier for the target chat or username of the target channel
( in the format @channelusername )
: return :
"""
return apihelper . delete_chat_photo ( self . token , chat_id )
2020-05-08 21:06:39 +03:00
def set_my_commands ( self , commands ) :
"""
Use this method to change the list of the bot ' s commands.
: param commands : Array of BotCommand . A JSON - serialized list of bot commands
to be set as the list of the bot ' s commands. At most 100 commands can be specified.
: return :
"""
return apihelper . set_my_commands ( self . token , commands )
2017-06-30 19:47:09 +03:00
def set_chat_title ( self , chat_id , title ) :
"""
Use this method to change the title of a chat . Titles can ' t be changed for private chats.
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights .
Returns True on success .
Note : In regular groups ( non - supergroups ) , this method will only work if the ‘ All Members Are Admins ’
setting is off in the target group .
: param chat_id : Int or Str : Unique identifier for the target chat or username of the target channel
( in the format @channelusername )
: param title : New chat title , 1 - 255 characters
: return :
"""
return apihelper . set_chat_title ( self . token , chat_id , title )
def set_chat_description ( self , chat_id , description ) :
"""
Use this method to change the description of a supergroup or a channel .
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights .
Returns True on success .
: param chat_id : Int or Str : Unique identifier for the target chat or username of the target channel
( in the format @channelusername )
: param description : Str : New chat description , 0 - 255 characters
: return :
"""
return apihelper . set_chat_description ( self . token , chat_id , description )
def pin_chat_message ( self , chat_id , message_id , disable_notification = False ) :
"""
Use this method to pin a message in a supergroup .
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights .
Returns True on success .
: param chat_id : Int or Str : Unique identifier for the target chat or username of the target channel
( in the format @channelusername )
: param message_id : Int : Identifier of a message to pin
: param disable_notification : Bool : Pass True , if it is not necessary to send a notification
to all group members about the new pinned message
: return :
"""
return apihelper . pin_chat_message ( self . token , chat_id , message_id , disable_notification )
def unpin_chat_message ( self , chat_id ) :
"""
Use this method to unpin a message in a supergroup chat .
The bot must be an administrator in the chat for this to work and must have the appropriate admin rights .
Returns True on success .
: param chat_id : Int or Str : Unique identifier for the target chat or username of the target channel
( in the format @channelusername )
: return :
"""
return apihelper . unpin_chat_message ( self . token , chat_id )
2016-04-14 10:06:46 +03:00
def edit_message_text ( self , text , chat_id = None , message_id = None , inline_message_id = None , parse_mode = None ,
2016-04-14 10:03:07 +03:00
disable_web_page_preview = None , reply_markup = None ) :
2020-01-08 20:17:25 +03:00
"""
2020-01-08 20:06:40 +03:00
Use this method to edit text and game messages .
: param text :
: param chat_id :
: param message_id :
: param inline_message_id :
: param parse_mode :
: param disable_web_page_preview :
: param reply_markup :
: return :
"""
2020-07-04 21:07:42 +03:00
parse_mode = self . parse_mode if not parse_mode else parse_mode
2016-04-17 05:24:37 +03:00
result = apihelper . edit_message_text ( self . token , text , chat_id , message_id , inline_message_id , parse_mode ,
2016-06-07 14:29:12 +03:00
disable_web_page_preview , reply_markup )
if type ( result ) == bool : # if edit inline message return is bool not Message.
2016-04-17 05:24:37 +03:00
return result
return types . Message . de_json ( result )
2016-04-14 10:03:07 +03:00
2018-08-09 19:10:01 +03:00
def edit_message_media ( self , media , chat_id = None , message_id = None , inline_message_id = None , reply_markup = None ) :
2020-01-08 20:17:25 +03:00
"""
2020-01-08 20:06:40 +03:00
Use this method to edit animation , audio , document , photo , or video messages . If a message is a part of a message album , then it can be edited only to a photo or a video . Otherwise , message type can be changed arbitrarily . When inline message is edited , new file can ' t be uploaded. Use previously uploaded file via its file_id or specify a URL.
: param media :
: param chat_id :
: param message_id :
: param inline_message_id :
: param reply_markup :
: return :
"""
2018-08-09 19:10:01 +03:00
result = apihelper . edit_message_media ( self . token , media , chat_id , message_id , inline_message_id , reply_markup )
if type ( result ) == bool : # if edit inline message return is bool not Message.
return result
return types . Message . de_json ( result )
2016-04-22 20:21:45 +03:00
def edit_message_reply_markup ( self , chat_id = None , message_id = None , inline_message_id = None , reply_markup = None ) :
2020-01-08 20:17:25 +03:00
"""
2020-01-08 20:06:40 +03:00
Use this method to edit only the reply markup of messages .
: param chat_id :
: param message_id :
: param inline_message_id :
: param reply_markup :
: return :
"""
2016-08-29 15:50:27 +03:00
result = apihelper . edit_message_reply_markup ( self . token , chat_id , message_id , inline_message_id , reply_markup )
if type ( result ) == bool :
return result
return types . Message . de_json ( result )
2016-04-14 10:17:53 +03:00
2020-05-16 17:34:56 +03:00
def send_game (
self , chat_id , game_short_name , disable_notification = None ,
reply_to_message_id = None , reply_markup = None , timeout = None ) :
2020-01-08 20:17:25 +03:00
"""
2020-01-08 20:06:40 +03:00
Used to send the game
: param chat_id :
: param game_short_name :
: param disable_notification :
: param reply_to_message_id :
: param reply_markup :
: return :
"""
2020-05-16 17:34:56 +03:00
result = apihelper . send_game (
self . token , chat_id , game_short_name , disable_notification ,
reply_to_message_id , reply_markup , timeout )
2016-10-08 15:36:48 +03:00
return types . Message . de_json ( result )
2017-05-25 05:56:58 +03:00
def set_game_score ( self , user_id , score , force = None , chat_id = None , message_id = None , inline_message_id = None ,
edit_message = None ) :
2020-01-08 20:17:25 +03:00
"""
2020-01-08 20:06:40 +03:00
Sets the value of points in the game to a specific user
: param user_id :
: param score :
: param force :
: param chat_id :
: param message_id :
: param inline_message_id :
: param edit_message :
: return :
"""
2016-12-03 08:31:55 +03:00
result = apihelper . set_game_score ( self . token , user_id , score , force , chat_id , message_id , inline_message_id ,
2016-10-29 16:22:46 +03:00
edit_message )
if type ( result ) == bool :
return result
return types . Message . de_json ( result )
def get_game_high_scores ( self , user_id , chat_id = None , message_id = None , inline_message_id = None ) :
2020-01-08 20:17:25 +03:00
"""
2020-01-08 20:06:40 +03:00
Gets top points and game play
: param user_id :
: param chat_id :
: param message_id :
: param inline_message_id :
: return :
"""
2016-10-29 16:22:46 +03:00
result = apihelper . get_game_high_scores ( self . token , user_id , chat_id , message_id , inline_message_id )
ret = [ ]
for r in result :
ret . append ( types . GameHighScore . de_json ( r ) )
return ret
2017-05-25 05:56:58 +03:00
def send_invoice ( self , chat_id , title , description , invoice_payload , provider_token , currency , prices ,
2017-05-25 06:14:08 +03:00
start_parameter , photo_url = None , photo_size = None , photo_width = None , photo_height = None ,
2017-05-25 05:56:58 +03:00
need_name = None , need_phone_number = None , need_email = None , need_shipping_address = None ,
2020-07-21 01:20:01 +03:00
send_phone_number_to_provider = None , send_email_to_provider = None , is_flexible = None ,
disable_notification = None , reply_to_message_id = None , reply_markup = None , provider_data = None , timeout = None ) :
2020-01-08 20:17:25 +03:00
"""
2020-01-08 20:06:40 +03:00
Sends invoice
2020-07-21 01:20:01 +03:00
: param chat_id : Unique identifier for the target private chat
: param title : Product name
: param description : Product description
: param invoice_payload : Bot - defined invoice payload , 1 - 128 bytes . This will not be displayed to the user , use for your internal processes .
: param provider_token : Payments provider token , obtained via @Botfather
: param currency : Three - letter ISO 4217 currency code , see https : / / core . telegram . org / bots / payments #supported-currencies
: param prices : Price breakdown , a list of components ( e . g . product price , tax , discount , delivery cost , delivery tax , bonus , etc . )
: param start_parameter : Unique deep - linking parameter that can be used to generate this invoice when used as a start parameter
: param photo_url : URL of the product photo for the invoice . Can be a photo of the goods or a marketing image for a service . People like it better when they see what they are paying for .
: param photo_size : Photo size
: param photo_width : Photo width
: param photo_height : Photo height
: param need_name : Pass True , if you require the user ' s full name to complete the order
: param need_phone_number : Pass True , if you require the user ' s phone number to complete the order
: param need_email : Pass True , if you require the user ' s email to complete the order
: param need_shipping_address : Pass True , if you require the user ' s shipping address to complete the order
: param is_flexible : Pass True , if the final price depends on the shipping method
: param send_phone_number_to_provider : Pass True , if user ' s phone number should be sent to provider
: param send_email_to_provider : Pass True , if user ' s email address should be sent to provider
: param disable_notification : Sends the message silently . Users will receive a notification with no sound .
: param reply_to_message_id : If the message is a reply , ID of the original message
: param reply_markup : A JSON - serialized object for an inline keyboard . If empty , one ' Pay total price ' button will be shown . If not empty , the first button must be a Pay button
: param provider_data : A JSON - serialized data about the invoice , which will be shared with the payment provider . A detailed description of required fields should be provided by the payment provider .
2020-01-08 20:06:40 +03:00
: return :
"""
2020-05-16 17:34:56 +03:00
result = apihelper . send_invoice (
self . token , chat_id , title , description , invoice_payload , provider_token ,
currency , prices , start_parameter , photo_url , photo_size , photo_width ,
photo_height , need_name , need_phone_number , need_email , need_shipping_address ,
2020-07-21 01:20:01 +03:00
send_phone_number_to_provider , send_email_to_provider , is_flexible , disable_notification ,
reply_to_message_id , reply_markup , provider_data , timeout )
2017-05-25 05:56:58 +03:00
return types . Message . de_json ( result )
2020-05-02 13:09:52 +03:00
def send_poll (
2020-05-16 17:34:56 +03:00
self , chat_id , question , options ,
2020-05-02 13:09:52 +03:00
is_anonymous = None , type = None , allows_multiple_answers = None , correct_option_id = None ,
explanation = None , explanation_parse_mode = None , open_period = None , close_date = None , is_closed = None ,
2020-05-16 17:34:56 +03:00
disable_notifications = False , reply_to_message_id = None , reply_markup = None , timeout = None ) :
2020-05-02 13:09:52 +03:00
"""
Send polls
2020-01-08 20:06:40 +03:00
: param chat_id :
2020-05-02 13:09:52 +03:00
: param question :
2020-05-02 13:27:39 +03:00
: param options : array of str with answers
2020-05-02 13:09:52 +03:00
: param is_anonymous :
: param type :
: param allows_multiple_answers :
: param correct_option_id :
: param explanation :
: param explanation_parse_mode :
: param open_period :
: param close_date :
: param is_closed :
2020-01-08 20:06:40 +03:00
: param disable_notifications :
2020-05-02 13:09:52 +03:00
: param reply_to_message_id :
2020-01-08 20:06:40 +03:00
: param reply_markup :
: return :
"""
2020-05-02 13:09:52 +03:00
if isinstance ( question , types . Poll ) :
raise Exception ( " The send_poll signature was changed, please see send_poll function details. " )
return types . Message . de_json (
apihelper . send_poll (
self . token , chat_id ,
question , options ,
is_anonymous , type , allows_multiple_answers , correct_option_id ,
explanation , explanation_parse_mode , open_period , close_date , is_closed ,
2020-05-16 17:34:56 +03:00
disable_notifications , reply_to_message_id , reply_markup , timeout ) )
2019-06-06 21:47:08 +03:00
def stop_poll ( self , chat_id , message_id ) :
2020-01-08 20:17:25 +03:00
"""
2020-01-08 20:06:40 +03:00
Stops poll
: param chat_id :
: param message_id :
: return :
"""
2019-06-06 21:47:08 +03:00
return types . Poll . de_json ( apihelper . stop_poll ( self . token , chat_id , message_id ) )
2017-05-25 05:56:58 +03:00
def answer_shipping_query ( self , shipping_query_id , ok , shipping_options = None , error_message = None ) :
2020-01-08 20:17:25 +03:00
"""
2020-01-08 20:06:40 +03:00
Asks for an answer to a shipping question
: param shipping_query_id :
: param ok :
: param shipping_options :
: param error_message :
: return :
"""
2017-06-02 11:07:35 +03:00
return apihelper . answer_shipping_query ( self . token , shipping_query_id , ok , shipping_options , error_message )
2017-05-25 05:56:58 +03:00
def answer_pre_checkout_query ( self , pre_checkout_query_id , ok , error_message = None ) :
2020-01-08 20:17:25 +03:00
"""
2020-01-08 20:06:40 +03:00
Response to a request for pre - inspection
2020-01-08 20:17:25 +03:00
: param pre_checkout_query_id :
: param ok :
: param error_message :
: return :
"""
2017-05-25 05:56:58 +03:00
return apihelper . answer_pre_checkout_query ( self . token , pre_checkout_query_id , ok , error_message )
2018-02-16 17:19:35 +03:00
def edit_message_caption ( self , caption , chat_id = None , message_id = None , inline_message_id = None ,
parse_mode = None , reply_markup = None ) :
2020-01-08 20:17:25 +03:00
"""
2020-01-08 20:06:40 +03:00
Use this method to edit captions of messages
: param caption :
: param chat_id :
: param message_id :
: param inline_message_id :
: param parse_mode :
: param reply_markup :
: return :
"""
2020-07-04 21:07:42 +03:00
parse_mode = self . parse_mode if not parse_mode else parse_mode
2016-10-08 15:36:48 +03:00
result = apihelper . edit_message_caption ( self . token , caption , chat_id , message_id , inline_message_id ,
2018-02-16 17:19:35 +03:00
parse_mode , reply_markup )
2016-08-29 15:50:27 +03:00
if type ( result ) == bool :
2016-08-29 15:21:56 +03:00
return result
2016-08-29 15:50:27 +03:00
return types . Message . de_json ( result )
2016-04-14 10:17:53 +03:00
2015-07-03 00:38:11 +03:00
def reply_to ( self , message , text , * * kwargs ) :
2015-07-24 14:44:01 +03:00
"""
Convenience function for ` send_message ( message . chat . id , text , reply_to_message_id = message . message_id , * * kwargs ) `
2020-01-08 20:17:25 +03:00
: param message :
2020-01-08 20:06:40 +03:00
: param text :
: param kwargs :
: return :
2015-07-24 14:44:01 +03:00
"""
2015-07-03 00:38:11 +03:00
return self . send_message ( message . chat . id , text , reply_to_message_id = message . message_id , * * kwargs )
2016-04-14 10:32:08 +03:00
def answer_inline_query ( self , inline_query_id , results , cache_time = None , is_personal = None , next_offset = None ,
switch_pm_text = None , switch_pm_parameter = None ) :
2016-01-06 09:31:21 +03:00
"""
Use this method to send answers to an inline query . On success , True is returned .
No more than 50 results per query are allowed .
: param inline_query_id : Unique identifier for the answered query
: param results : Array of results for the inline query
: param cache_time : The maximum amount of time in seconds that the result of the inline query may be cached on the server .
: param is_personal : Pass True , if results may be cached on the server side only for the user that sent the query .
: param next_offset : Pass the offset that a client should send in the next query with the same text to receive more results .
2016-04-14 10:32:08 +03:00
: param switch_pm_parameter : If passed , clients will display a button with specified text that switches the user
to a private chat with the bot and sends the bot a start message with the parameter switch_pm_parameter
: param switch_pm_text : Parameter for the start message sent to the bot when user presses the switch button
2016-01-06 09:31:21 +03:00
: return : True means success .
"""
2016-04-14 10:32:08 +03:00
return apihelper . answer_inline_query ( self . token , inline_query_id , results , cache_time , is_personal , next_offset ,
switch_pm_text , switch_pm_parameter )
2016-01-05 09:07:47 +03:00
2016-12-03 08:38:30 +03:00
def answer_callback_query ( self , callback_query_id , text = None , show_alert = None , url = None , cache_time = None ) :
2016-04-16 09:53:41 +03:00
"""
Use this method to send answers to callback queries sent from inline keyboards . The answer will be displayed to
the user as a notification at the top of the chat screen or as an alert .
: param callback_query_id :
: param text :
: param show_alert :
2020-01-08 20:06:40 +03:00
: param url :
: param cache_time :
2016-04-16 09:53:41 +03:00
: return :
"""
2016-12-03 08:38:30 +03:00
return apihelper . answer_callback_query ( self . token , callback_query_id , text , show_alert , url , cache_time )
2016-04-16 09:53:41 +03:00
2017-08-06 09:25:25 +03:00
def get_sticker_set ( self , name ) :
"""
Use this method to get a sticker set . On success , a StickerSet object is returned .
: param name :
: return :
"""
result = apihelper . get_sticker_set ( self . token , name )
return types . StickerSet . de_json ( result )
def upload_sticker_file ( self , user_id , png_sticker ) :
"""
Use this method to upload a . png file with a sticker for later use in createNewStickerSet and addStickerToSet
methods ( can be used multiple times ) . Returns the uploaded File on success .
: param user_id :
: param png_sticker :
: return :
"""
result = apihelper . upload_sticker_file ( self . token , user_id , png_sticker )
return types . File . de_json ( result )
def create_new_sticker_set ( self , user_id , name , title , png_sticker , emojis , contains_masks = None ,
mask_position = None ) :
"""
Use this method to create new sticker set owned by a user . The bot will be able to edit the created sticker set .
Returns True on success .
: param user_id :
: param name :
: param title :
: param png_sticker :
: param emojis :
: param contains_masks :
: param mask_position :
: return :
"""
return apihelper . create_new_sticker_set ( self . token , user_id , name , title , png_sticker , emojis , contains_masks ,
mask_position )
2017-08-19 21:36:08 +03:00
def add_sticker_to_set ( self , user_id , name , png_sticker , emojis , mask_position = None ) :
2017-08-06 09:25:25 +03:00
"""
Use this method to add a new sticker to a set created by the bot . Returns True on success .
: param user_id :
: param name :
: param png_sticker :
: param emojis :
: param mask_position :
: return :
"""
return apihelper . add_sticker_to_set ( self . token , user_id , name , png_sticker , emojis , mask_position )
def set_sticker_position_in_set ( self , sticker , position ) :
"""
Use this method to move a sticker in a set created by the bot to a specific position . Returns True on success .
: param sticker :
: param position :
: return :
"""
return apihelper . set_sticker_position_in_set ( self . token , sticker , position )
def delete_sticker_from_set ( self , sticker ) :
"""
Use this method to delete a sticker from a set created by the bot . Returns True on success .
: param sticker :
: return :
"""
return apihelper . delete_sticker_from_set ( self . token , sticker )
2018-04-12 13:45:32 +03:00
def register_for_reply ( self , message , callback , * args , * * kwargs ) :
2015-07-24 14:44:01 +03:00
"""
Registers a callback function to be notified when a reply to ` message ` arrives .
2018-05-27 17:01:07 +03:00
Warning : In case ` callback ` as lambda function , saving reply handlers will not work .
2015-07-24 14:44:01 +03:00
: param message : The message for which we are awaiting a reply .
: param callback : The callback function to be called when a reply arrives . Must accept one ` message `
parameter , which will contain the replied message .
"""
2018-04-12 13:45:32 +03:00
message_id = message . message_id
self . register_for_reply_by_message_id ( message_id , callback , * args , * * kwargs )
2015-07-24 14:44:01 +03:00
2018-04-12 13:45:32 +03:00
def register_for_reply_by_message_id ( self , message_id , callback , * args , * * kwargs ) :
"""
Registers a callback function to be notified when a reply to ` message ` arrives .
2015-07-24 14:44:01 +03:00
2018-05-27 17:01:07 +03:00
Warning : In case ` callback ` as lambda function , saving reply handlers will not work .
2015-07-24 14:44:01 +03:00
2018-05-27 17:02:04 +03:00
: param message_id : The id of the message for which we are awaiting a reply .
2018-04-12 13:45:32 +03:00
: param callback : The callback function to be called when a reply arrives . Must accept one ` message `
parameter , which will contain the replied message .
"""
2020-04-12 20:41:32 +03:00
self . reply_backend . register_handler ( message_id , Handler ( callback , * args , * * kwargs ) )
2015-07-24 14:44:01 +03:00
2018-04-12 13:45:32 +03:00
def _notify_reply_handlers ( self , new_messages ) :
2020-01-08 20:06:40 +03:00
"""
Notify handlers of the answers
: param new_messages :
: return :
"""
2018-04-12 13:45:32 +03:00
for message in new_messages :
if hasattr ( message , " reply_to_message " ) and message . reply_to_message is not None :
2020-04-12 20:41:32 +03:00
handlers = self . reply_backend . get_handlers ( message . reply_to_message . message_id )
for handler in handlers :
self . _exec_task ( handler [ " callback " ] , message , * handler [ " args " ] , * * handler [ " kwargs " ] )
2018-04-12 13:45:32 +03:00
def register_next_step_handler ( self , message , callback , * args , * * kwargs ) :
2015-07-30 06:02:08 +03:00
"""
Registers a callback function to be notified when new message arrives after ` message ` .
2018-05-27 17:01:07 +03:00
Warning : In case ` callback ` as lambda function , saving next step handlers will not work .
2018-04-12 13:45:32 +03:00
: param message : The message for which we want to handle new message in the same chat .
2015-07-30 06:02:08 +03:00
: param callback : The callback function which next new message arrives .
2018-04-12 13:45:32 +03:00
: param args : Args to pass in callback func
: param kwargs : Args to pass in callback func
2015-07-30 06:02:08 +03:00
"""
chat_id = message . chat . id
2018-04-12 13:45:32 +03:00
self . register_next_step_handler_by_chat_id ( chat_id , callback , * args , * * kwargs )
def register_next_step_handler_by_chat_id ( self , chat_id , callback , * args , * * kwargs ) :
"""
Registers a callback function to be notified when new message arrives after ` message ` .
2018-05-27 17:01:07 +03:00
Warning : In case ` callback ` as lambda function , saving next step handlers will not work .
2018-04-12 13:45:32 +03:00
: param chat_id : The chat for which we want to handle new message .
: param callback : The callback function which next new message arrives .
: param args : Args to pass in callback func
: param kwargs : Args to pass in callback func
"""
2020-04-12 20:41:32 +03:00
self . next_step_backend . register_handler ( chat_id , Handler ( callback , * args , * * kwargs ) )
2017-11-29 08:53:39 +03:00
2017-08-21 13:10:47 +03:00
def clear_step_handler ( self , message ) :
"""
Clears all callback functions registered by register_next_step_handler ( ) .
: param message : The message for which we want to handle new message after that in same chat .
"""
chat_id = message . chat . id
2018-04-12 13:45:32 +03:00
self . clear_step_handler_by_chat_id ( chat_id )
2015-07-30 06:02:08 +03:00
2018-04-12 13:45:32 +03:00
def clear_step_handler_by_chat_id ( self , chat_id ) :
"""
Clears all callback functions registered by register_next_step_handler ( ) .
: param chat_id : The chat for which we want to clear next step handlers
"""
2020-04-12 20:41:32 +03:00
self . next_step_backend . clear_handlers ( chat_id )
2018-05-20 23:40:25 +03:00
2018-04-12 13:45:32 +03:00
def clear_reply_handlers ( self , message ) :
"""
Clears all callback functions registered by register_for_reply ( ) and register_for_reply_by_message_id ( ) .
2018-05-19 00:42:06 +03:00
: param message : The message for which we want to clear reply handlers
2018-04-12 13:45:32 +03:00
"""
message_id = message . message_id
self . clear_reply_handlers_by_message_id ( message_id )
def clear_reply_handlers_by_message_id ( self , message_id ) :
"""
Clears all callback functions registered by register_for_reply ( ) and register_for_reply_by_message_id ( ) .
: param message_id : The message id for which we want to clear reply handlers
"""
2020-04-12 20:41:32 +03:00
self . reply_backend . clear_handlers ( message_id )
2018-05-20 23:40:25 +03:00
2018-04-12 13:45:32 +03:00
def _notify_next_handlers ( self , new_messages ) :
2020-01-08 20:06:40 +03:00
"""
Description : TBD
: param new_messages :
: return :
"""
2020-04-12 20:41:32 +03:00
for i , message in enumerate ( new_messages ) :
need_pop = False
handlers = self . next_step_backend . get_handlers ( message . chat . id )
for handler in handlers :
need_pop = True
self . _exec_task ( handler [ " callback " ] , message , * handler [ " args " ] , * * handler [ " kwargs " ] )
if need_pop :
new_messages . pop ( i ) # removing message that detects with next_step_handler
2018-04-12 13:45:32 +03:00
@staticmethod
def _build_handler_dict ( handler , * * filters ) :
2020-01-08 20:17:25 +03:00
"""
Builds a dictionary for a handler
: param handler :
2020-01-08 20:06:40 +03:00
: param filters :
: return :
"""
2016-06-13 14:15:15 +03:00
return {
' function ' : handler ,
2020-05-12 03:09:34 +03:00
' filters ' : filters
2016-06-13 14:15:15 +03:00
}
2020-04-08 21:13:19 +03:00
def middleware_handler ( self , update_types = None ) :
"""
Middleware handler decorator .
This decorator can be used to decorate functions that must be handled as middlewares before entering any other
message handlers
But , be careful and check type of the update inside the handler if more than one update_type is given
Example :
bot = TeleBot ( ' TOKEN ' )
# Print post message text before entering to any post_channel handlers
@bot.middleware_handler ( update_types = [ ' channel_post ' , ' edited_channel_post ' ] )
def print_channel_post_text ( bot_instance , channel_post ) :
print ( channel_post . text )
# Print update id before entering to any handlers
@bot.middleware_handler ( )
def print_channel_post_text ( bot_instance , update ) :
print ( update . update_id )
: param update_types : Optional list of update types that can be passed into the middleware handler .
"""
def decorator ( handler ) :
self . add_middleware_handler ( handler , update_types )
return handler
return decorator
def add_middleware_handler ( self , handler , update_types = None ) :
"""
Add middleware handler
: param handler :
: param update_types :
: return :
"""
if update_types :
for update_type in update_types :
self . typed_middleware_handlers [ update_type ] . append ( handler )
else :
self . default_middleware_handlers . append ( handler )
2020-01-08 20:06:40 +03:00
def message_handler ( self , commands = None , regexp = None , func = None , content_types = None , * * kwargs ) :
2015-07-02 04:38:31 +03:00
"""
Message handler decorator .
This decorator can be used to decorate functions that must handle certain types of messages .
All message handlers are tested in the order they were added .
Example :
bot = TeleBot ( ' TOKEN ' )
# Handles all messages which text matches regexp.
@bot.message_handler ( regexp = ' someregexp ' )
def command_help ( message ) :
bot . send_message ( message . chat . id , ' Did someone call for help? ' )
# Handle all sent documents of type 'text/plain'.
@bot.message_handler ( func = lambda message : message . document . mime_type == ' text/plain ' , content_types = [ ' document ' ] )
def command_handle_document ( message ) :
bot . send_message ( message . chat . id , ' Document received, sir! ' )
2020-04-24 18:19:30 +03:00
# Handle all other messages.
2019-01-20 21:04:11 +03:00
@bot.message_handler ( func = lambda message : True , content_types = [ ' audio ' , ' photo ' , ' voice ' , ' video ' , ' document ' , ' text ' , ' location ' , ' contact ' , ' sticker ' ] )
2015-07-02 04:38:31 +03:00
def default_command ( message ) :
bot . send_message ( message . chat . id , " This is the default command handler. " )
2020-04-24 18:19:30 +03:00
: param commands : Optional list of strings ( commands to handle ) .
2015-07-02 04:38:31 +03:00
: param regexp : Optional regular expression .
: param func : Optional lambda function . The lambda receives the message to test as the first parameter . It must return True if the command should handle the message .
: param content_types : This commands ' supported content types. Must be a list. Defaults to [ ' text ' ].
"""
2015-09-30 18:18:26 +03:00
2020-01-08 20:06:40 +03:00
if content_types is None :
content_types = [ " text " ]
2016-02-20 13:58:16 +03:00
def decorator ( handler ) :
2016-06-13 14:15:15 +03:00
handler_dict = self . _build_handler_dict ( handler ,
commands = commands ,
regexp = regexp ,
func = func ,
2016-06-13 16:47:15 +03:00
content_types = content_types ,
* * kwargs )
2015-07-03 02:42:47 +03:00
2016-06-13 14:15:15 +03:00
self . add_message_handler ( handler_dict )
2016-02-20 13:58:16 +03:00
2016-06-13 14:15:15 +03:00
return handler
2016-02-20 13:58:16 +03:00
2016-06-13 14:15:15 +03:00
return decorator
2016-02-20 13:58:16 +03:00
2016-06-13 14:15:15 +03:00
def add_message_handler ( self , handler_dict ) :
2020-01-08 20:17:25 +03:00
"""
2020-01-08 20:06:40 +03:00
Adds a message handler
: param handler_dict :
: return :
"""
2016-02-20 13:58:16 +03:00
self . message_handlers . append ( handler_dict )
2020-01-08 20:06:40 +03:00
def edited_message_handler ( self , commands = None , regexp = None , func = None , content_types = None , * * kwargs ) :
"""
Edit message handler decorator
: param commands :
: param regexp :
: param func :
: param content_types :
: param kwargs :
: return :
"""
if content_types is None :
content_types = [ " text " ]
2016-06-07 14:29:12 +03:00
def decorator ( handler ) :
2016-06-13 14:15:15 +03:00
handler_dict = self . _build_handler_dict ( handler ,
commands = commands ,
regexp = regexp ,
func = func ,
2016-06-13 16:47:15 +03:00
content_types = content_types ,
* * kwargs )
2016-06-13 14:15:15 +03:00
self . add_edited_message_handler ( handler_dict )
2016-06-07 14:29:12 +03:00
return handler
return decorator
2016-06-13 14:15:15 +03:00
def add_edited_message_handler ( self , handler_dict ) :
2020-01-08 20:06:40 +03:00
"""
Adds the edit message handler
: param handler_dict :
: return :
"""
2016-06-07 14:29:12 +03:00
self . edited_message_handlers . append ( handler_dict )
2020-01-08 20:06:40 +03:00
def channel_post_handler ( self , commands = None , regexp = None , func = None , content_types = None , * * kwargs ) :
"""
Channel post handler decorator
: param commands :
: param regexp :
: param func :
: param content_types :
: param kwargs :
: return :
"""
if content_types is None :
content_types = [ " text " ]
2016-12-03 08:28:22 +03:00
def decorator ( handler ) :
handler_dict = self . _build_handler_dict ( handler ,
commands = commands ,
regexp = regexp ,
func = func ,
content_types = content_types ,
* * kwargs )
self . add_channel_post_handler ( handler_dict )
return handler
return decorator
def add_channel_post_handler ( self , handler_dict ) :
2020-01-08 20:06:40 +03:00
"""
Adds channel post handler
: param handler_dict :
: return :
"""
2016-12-03 08:28:22 +03:00
self . channel_post_handlers . append ( handler_dict )
2020-01-08 20:06:40 +03:00
def edited_channel_post_handler ( self , commands = None , regexp = None , func = None , content_types = None , * * kwargs ) :
"""
Edit channel post handler decorator
: param commands :
: param regexp :
: param func :
: param content_types :
: param kwargs :
: return :
"""
if content_types is None :
content_types = [ " text " ]
2016-12-03 08:28:22 +03:00
def decorator ( handler ) :
handler_dict = self . _build_handler_dict ( handler ,
commands = commands ,
regexp = regexp ,
func = func ,
content_types = content_types ,
* * kwargs )
2017-03-04 16:30:07 +03:00
self . add_edited_channel_post_handler ( handler_dict )
2016-12-03 08:28:22 +03:00
return handler
return decorator
def add_edited_channel_post_handler ( self , handler_dict ) :
2020-01-08 20:06:40 +03:00
"""
Adds the edit channel post handler
: param handler_dict :
: return :
"""
2016-12-03 08:28:22 +03:00
self . edited_channel_post_handlers . append ( handler_dict )
2016-06-16 11:49:51 +03:00
def inline_handler ( self , func , * * kwargs ) :
2020-01-08 20:06:40 +03:00
"""
Inline call handler decorator
: param func :
: param kwargs :
: return :
"""
2016-02-20 13:58:16 +03:00
def decorator ( handler ) :
2016-06-16 11:49:51 +03:00
handler_dict = self . _build_handler_dict ( handler , func = func , * * kwargs )
2016-06-13 14:24:27 +03:00
self . add_inline_handler ( handler_dict )
2016-02-20 13:58:16 +03:00
return handler
2016-01-04 18:10:32 +03:00
return decorator
2016-06-13 14:24:27 +03:00
def add_inline_handler ( self , handler_dict ) :
2020-01-08 20:06:40 +03:00
"""
Adds inline call handler
: param handler_dict :
: return :
"""
2016-02-20 13:58:16 +03:00
self . inline_handlers . append ( handler_dict )
2016-06-16 11:49:51 +03:00
def chosen_inline_handler ( self , func , * * kwargs ) :
2020-01-08 20:06:40 +03:00
"""
Description : TBD
: param func :
: param kwargs :
: return :
"""
2016-02-20 13:58:16 +03:00
def decorator ( handler ) :
2016-06-16 11:49:51 +03:00
handler_dict = self . _build_handler_dict ( handler , func = func , * * kwargs )
2016-06-13 14:24:27 +03:00
self . add_chosen_inline_handler ( handler_dict )
2016-02-20 13:58:16 +03:00
return handler
2016-01-05 08:18:32 +03:00
return decorator
2016-06-13 14:24:27 +03:00
def add_chosen_inline_handler ( self , handler_dict ) :
2020-01-08 20:06:40 +03:00
"""
Description : TBD
: param handler_dict :
: return :
"""
2016-02-20 13:58:16 +03:00
self . chosen_inline_handlers . append ( handler_dict )
2016-06-16 11:49:51 +03:00
def callback_query_handler ( self , func , * * kwargs ) :
2020-01-08 20:06:40 +03:00
"""
Callback request handler decorator
: param func :
: param kwargs :
: return :
"""
2016-04-16 09:18:19 +03:00
def decorator ( handler ) :
2016-06-16 11:49:51 +03:00
handler_dict = self . _build_handler_dict ( handler , func = func , * * kwargs )
2016-06-13 14:24:27 +03:00
self . add_callback_query_handler ( handler_dict )
2016-09-28 14:41:36 +03:00
return handler
2016-04-16 09:18:19 +03:00
return decorator
2016-06-13 14:24:27 +03:00
def add_callback_query_handler ( self , handler_dict ) :
2020-01-08 20:06:40 +03:00
"""
Adds a callback request handler
: param handler_dict :
: return :
"""
2016-04-16 09:18:19 +03:00
self . callback_query_handlers . append ( handler_dict )
2017-05-25 06:22:40 +03:00
def shipping_query_handler ( self , func , * * kwargs ) :
2020-01-08 20:06:40 +03:00
"""
Shipping request handler
: param func :
: param kwargs :
: return :
"""
2017-05-25 06:22:40 +03:00
def decorator ( handler ) :
handler_dict = self . _build_handler_dict ( handler , func = func , * * kwargs )
self . add_shipping_query_handler ( handler_dict )
return handler
return decorator
def add_shipping_query_handler ( self , handler_dict ) :
2020-01-08 20:06:40 +03:00
"""
Adds a shipping request handler
: param handler_dict :
: return :
"""
2017-05-25 06:22:40 +03:00
self . shipping_query_handlers . append ( handler_dict )
def pre_checkout_query_handler ( self , func , * * kwargs ) :
2020-01-08 20:06:40 +03:00
"""
Pre - checkout request handler
: param func :
: param kwargs :
: return :
"""
2017-05-25 06:22:40 +03:00
def decorator ( handler ) :
handler_dict = self . _build_handler_dict ( handler , func = func , * * kwargs )
2017-05-25 06:45:44 +03:00
self . add_pre_checkout_query_handler ( handler_dict )
2017-05-25 06:22:40 +03:00
return handler
return decorator
2017-05-25 06:23:37 +03:00
def add_pre_checkout_query_handler ( self , handler_dict ) :
2020-01-08 20:06:40 +03:00
"""
Adds a pre - checkout request handler
: param handler_dict :
: return :
"""
2017-05-25 06:22:40 +03:00
self . pre_checkout_query_handlers . append ( handler_dict )
2020-03-09 13:25:54 +03:00
def poll_handler ( self , func , * * kwargs ) :
"""
Poll request handler
: param func :
: param kwargs :
: return :
"""
def decorator ( handler ) :
handler_dict = self . _build_handler_dict ( handler , func = func , * * kwargs )
self . add_poll_handler ( handler_dict )
return handler
return decorator
def add_poll_handler ( self , handler_dict ) :
"""
Adds a poll request handler
: param handler_dict :
: return :
"""
self . poll_handlers . append ( handler_dict )
2020-05-12 03:09:34 +03:00
def poll_answer_handler ( self , func = None , * * kwargs ) :
"""
Poll_answer request handler
: param func :
: param kwargs :
: return :
"""
def decorator ( handler ) :
handler_dict = self . _build_handler_dict ( handler , func = func , * * kwargs )
self . add_poll_answer_handler ( handler_dict )
return handler
return decorator
def add_poll_answer_handler ( self , handler_dict ) :
"""
Adds a poll_answer request handler
: param handler_dict :
: return :
"""
self . poll_answer_handlers . append ( handler_dict )
2016-06-13 16:47:15 +03:00
def _test_message_handler ( self , message_handler , message ) :
2020-01-08 20:17:25 +03:00
"""
2020-01-08 20:06:40 +03:00
Test message handler
: param message_handler :
: param message :
: return :
"""
2020-04-24 18:19:30 +03:00
for message_filter , filter_value in six . iteritems ( message_handler [ ' filters ' ] ) :
2016-06-13 16:47:15 +03:00
if filter_value is None :
continue
2020-04-24 18:19:30 +03:00
if not self . _test_filter ( message_filter , filter_value , message ) :
2015-09-08 11:44:31 +03:00
return False
2016-06-13 16:47:15 +03:00
2015-09-08 11:44:31 +03:00
return True
2018-05-19 00:42:06 +03:00
@staticmethod
2020-04-24 18:19:30 +03:00
def _test_filter ( message_filter , filter_value , message ) :
2020-01-08 20:17:25 +03:00
"""
Test filters
2020-04-24 18:19:30 +03:00
: param message_filter :
2020-01-08 20:17:25 +03:00
: param filter_value :
: param message :
2020-01-08 20:06:40 +03:00
: return :
"""
2016-06-13 16:47:15 +03:00
test_cases = {
' content_types ' : lambda msg : msg . content_type in filter_value ,
2017-03-28 16:24:28 +03:00
' regexp ' : lambda msg : msg . content_type == ' text ' and re . search ( filter_value , msg . text , re . IGNORECASE ) ,
2016-06-13 16:47:15 +03:00
' commands ' : lambda msg : msg . content_type == ' text ' and util . extract_command ( msg . text ) in filter_value ,
' func ' : lambda msg : filter_value ( msg )
}
2020-04-24 18:19:30 +03:00
return test_cases . get ( message_filter , lambda msg : False ) ( message )
2015-07-02 04:38:31 +03:00
2016-01-05 08:18:32 +03:00
def _notify_command_handlers ( self , handlers , new_messages ) :
2020-01-08 20:06:40 +03:00
"""
Notifies command handlers
: param handlers :
: param new_messages :
: return :
"""
2015-07-02 04:38:31 +03:00
for message in new_messages :
2017-11-14 19:42:27 +03:00
for message_handler in handlers :
if self . _test_message_handler ( message_handler , message ) :
self . _exec_task ( message_handler [ ' function ' ] , message )
break
2015-07-03 02:42:47 +03:00
class AsyncTeleBot ( TeleBot ) :
def __init__ ( self , * args , * * kwargs ) :
TeleBot . __init__ ( self , * args , * * kwargs )
2018-07-22 04:43:53 +03:00
@util.async_dec ( )
2018-08-17 12:48:59 +03:00
def enable_save_next_step_handlers ( self , delay = 120 , filename = " ./.handler-saves/step.save " ) :
return TeleBot . enable_save_next_step_handlers ( self , delay , filename )
2018-05-25 03:10:40 +03:00
2018-07-22 04:43:53 +03:00
@util.async_dec ( )
2018-08-17 12:48:59 +03:00
def enable_save_reply_handlers ( self , delay = 120 , filename = " ./.handler-saves/reply.save " ) :
return TeleBot . enable_save_reply_handlers ( self , delay , filename )
2018-05-25 03:10:40 +03:00
2018-07-22 04:43:53 +03:00
@util.async_dec ( )
2018-05-25 08:56:31 +03:00
def disable_save_next_step_handlers ( self ) :
return TeleBot . disable_save_next_step_handlers ( self )
2018-07-22 04:43:53 +03:00
@util.async_dec ( )
2018-05-25 08:56:31 +03:00
def disable_save_reply_handlers ( self ) :
return TeleBot . enable_save_reply_handlers ( self )
2018-05-25 03:10:40 +03:00
2018-07-22 04:43:53 +03:00
@util.async_dec ( )
2018-08-17 12:48:59 +03:00
def load_next_step_handlers ( self , filename = " ./.handler-saves/step.save " , del_file_after_loading = True ) :
return TeleBot . load_next_step_handlers ( self , filename , del_file_after_loading )
2018-05-25 03:10:40 +03:00
2018-07-22 04:43:53 +03:00
@util.async_dec ( )
2018-08-17 12:48:59 +03:00
def load_reply_handlers ( self , filename = " ./.handler-saves/reply.save " , del_file_after_loading = True ) :
return TeleBot . load_reply_handlers ( self , filename , del_file_after_loading )
2018-05-25 03:10:40 +03:00
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2015-07-03 02:42:47 +03:00
def get_me ( self ) :
return TeleBot . get_me ( self )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def get_file ( self , * args ) :
return TeleBot . get_file ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def download_file ( self , * args ) :
return TeleBot . download_file ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2015-07-03 02:42:47 +03:00
def get_user_profile_photos ( self , * args , * * kwargs ) :
return TeleBot . get_user_profile_photos ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def get_chat ( self , * args ) :
return TeleBot . get_chat ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def leave_chat ( self , * args ) :
return TeleBot . leave_chat ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def get_chat_administrators ( self , * args ) :
return TeleBot . get_chat_administrators ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def get_chat_members_count ( self , * args ) :
return TeleBot . get_chat_members_count ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-10-22 19:50:51 +03:00
def set_chat_sticker_set ( self , * args ) :
return TeleBot . set_chat_sticker_set ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-10-22 19:50:51 +03:00
def delete_chat_sticker_set ( self , * args ) :
return TeleBot . delete_chat_sticker_set ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def get_chat_member ( self , * args ) :
return TeleBot . get_chat_member ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2015-07-03 02:42:47 +03:00
def send_message ( self , * args , * * kwargs ) :
return TeleBot . send_message ( self , * args , * * kwargs )
2020-04-15 08:10:05 +03:00
@util.async_dec ( )
def send_dice ( self , * args , * * kwargs ) :
return TeleBot . send_dice ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2015-07-03 02:42:47 +03:00
def forward_message ( self , * args , * * kwargs ) :
return TeleBot . forward_message ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def delete_message ( self , * args ) :
return TeleBot . delete_message ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2015-07-03 02:42:47 +03:00
def send_photo ( self , * args , * * kwargs ) :
return TeleBot . send_photo ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2015-07-03 02:42:47 +03:00
def send_audio ( self , * args , * * kwargs ) :
return TeleBot . send_audio ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def send_voice ( self , * args , * * kwargs ) :
return TeleBot . send_voice ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2015-07-03 02:42:47 +03:00
def send_document ( self , * args , * * kwargs ) :
return TeleBot . send_document ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2015-07-03 02:42:47 +03:00
def send_sticker ( self , * args , * * kwargs ) :
return TeleBot . send_sticker ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2015-07-03 02:42:47 +03:00
def send_video ( self , * args , * * kwargs ) :
return TeleBot . send_video ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-19 17:19:15 +03:00
def send_video_note ( self , * args , * * kwargs ) :
return TeleBot . send_video_note ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-11-29 08:53:39 +03:00
def send_media_group ( self , * args , * * kwargs ) :
return TeleBot . send_media_group ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2015-07-03 02:42:47 +03:00
def send_location ( self , * args , * * kwargs ) :
return TeleBot . send_location ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-10-22 20:07:51 +03:00
def edit_message_live_location ( self , * args , * * kwargs ) :
return TeleBot . edit_message_live_location ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-10-22 20:07:51 +03:00
def stop_message_live_location ( self , * args , * * kwargs ) :
return TeleBot . stop_message_live_location ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def send_venue ( self , * args , * * kwargs ) :
return TeleBot . send_venue ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def send_contact ( self , * args , * * kwargs ) :
return TeleBot . send_contact ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2015-07-03 02:42:47 +03:00
def send_chat_action ( self , * args , * * kwargs ) :
return TeleBot . send_chat_action ( self , * args , * * kwargs )
2017-05-18 12:55:55 +03:00
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-07-07 00:54:18 +03:00
def kick_chat_member ( self , * args , * * kwargs ) :
return TeleBot . kick_chat_member ( self , * args , * * kwargs )
2017-05-18 12:55:55 +03:00
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def unban_chat_member ( self , * args ) :
return TeleBot . unban_chat_member ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-07-07 00:54:18 +03:00
def restrict_chat_member ( self , * args , * * kwargs ) :
return TeleBot . restrict_chat_member ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-07-07 00:54:18 +03:00
def promote_chat_member ( self , * args , * * kwargs ) :
return TeleBot . promote_chat_member ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-07-07 00:54:18 +03:00
def export_chat_invite_link ( self , * args ) :
return TeleBot . export_chat_invite_link ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-07-07 00:54:18 +03:00
def set_chat_photo ( self , * args ) :
return TeleBot . set_chat_photo ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-07-07 00:54:18 +03:00
def delete_chat_photo ( self , * args ) :
return TeleBot . delete_chat_photo ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-07-07 00:54:18 +03:00
def set_chat_title ( self , * args ) :
return TeleBot . set_chat_title ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-07-07 00:54:18 +03:00
def set_chat_description ( self , * args ) :
return TeleBot . set_chat_description ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-07-07 00:54:18 +03:00
def pin_chat_message ( self , * args , * * kwargs ) :
return TeleBot . pin_chat_message ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-07-07 00:54:18 +03:00
def unpin_chat_message ( self , * args ) :
return TeleBot . unpin_chat_message ( self , * args )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def edit_message_text ( self , * args , * * kwargs ) :
return TeleBot . edit_message_text ( self , * args , * * kwargs )
2018-08-09 19:10:01 +03:00
@util.async_dec ( )
def edit_message_media ( self , * args , * * kwargs ) :
return TeleBot . edit_message_media ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def edit_message_reply_markup ( self , * args , * * kwargs ) :
return TeleBot . edit_message_reply_markup ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def send_game ( self , * args , * * kwargs ) :
return TeleBot . send_game ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def set_game_score ( self , * args , * * kwargs ) :
return TeleBot . set_game_score ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def get_game_high_scores ( self , * args , * * kwargs ) :
return TeleBot . get_game_high_scores ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-25 05:56:58 +03:00
def send_invoice ( self , * args , * * kwargs ) :
return TeleBot . send_invoice ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-25 05:56:58 +03:00
def answer_shipping_query ( self , * args , * * kwargs ) :
return TeleBot . answer_shipping_query ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-25 05:56:58 +03:00
def answer_pre_checkout_query ( self , * args , * * kwargs ) :
return TeleBot . answer_pre_checkout_query ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def edit_message_caption ( self , * args , * * kwargs ) :
return TeleBot . edit_message_caption ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def answer_inline_query ( self , * args , * * kwargs ) :
return TeleBot . answer_inline_query ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-05-18 12:55:55 +03:00
def answer_callback_query ( self , * args , * * kwargs ) :
return TeleBot . answer_callback_query ( self , * args , * * kwargs )
2017-08-06 09:25:25 +03:00
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-08-06 09:25:25 +03:00
def get_sticker_set ( self , * args , * * kwargs ) :
return TeleBot . get_sticker_set ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-08-06 09:25:25 +03:00
def upload_sticker_file ( self , * args , * * kwargs ) :
return TeleBot . upload_sticker_file ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-08-06 09:25:25 +03:00
def create_new_sticker_set ( self , * args , * * kwargs ) :
return TeleBot . create_new_sticker_set ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-08-06 09:25:25 +03:00
def add_sticker_to_set ( self , * args , * * kwargs ) :
return TeleBot . add_sticker_to_set ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-08-06 09:25:25 +03:00
def set_sticker_position_in_set ( self , * args , * * kwargs ) :
return TeleBot . set_sticker_position_in_set ( self , * args , * * kwargs )
2018-07-02 18:13:11 +03:00
@util.async_dec ( )
2017-08-06 09:25:25 +03:00
def delete_sticker_from_set ( self , * args , * * kwargs ) :
return TeleBot . delete_sticker_from_set ( self , * args , * * kwargs )
2019-06-27 15:07:41 +03:00
@util.async_dec ( )
def send_poll ( self , * args , * * kwargs ) :
return TeleBot . send_poll ( self , * args , * * kwargs )
@util.async_dec ( )
def stop_poll ( self , * args , * * kwargs ) :
return TeleBot . stop_poll ( self , * args , * * kwargs )