1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00
pyTelegramBotAPI/pytelegrambotapi/__init__.py

1580 lines
68 KiB
Python
Raw Normal View History

2015-06-26 09:55:13 +03:00
# -*- coding: utf-8 -*-
from __future__ import print_function
2015-06-26 09:55:13 +03:00
2015-06-26 13:02:30 +03:00
import threading
2015-07-03 20:22:26 +03:00
import time
import re
2015-09-08 20:47:55 +03:00
import sys
import six
2015-06-26 09:55:13 +03:00
2015-07-20 04:56:17 +03:00
import logging
2015-09-30 18:18:26 +03:00
2015-09-08 20:47:55 +03:00
logger = logging.getLogger('TeleBot')
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-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)
from pytelegrambotapi import apihelper, types, util
2015-06-26 09:55:13 +03:00
"""
Module : telebot
"""
2015-09-30 18:18:26 +03:00
2015-06-26 09:55:13 +03:00
class TeleBot:
""" This is TeleBot Class
Methods:
getMe
sendMessage
forwardMessage
deleteMessage
2015-06-26 09:55:13 +03:00
sendPhoto
sendAudio
sendDocument
sendSticker
sendVideo
sendVideoNote
2015-06-26 09:55:13 +03:00
sendLocation
sendChatAction
getUserProfilePhotos
getUpdates
2017-06-30 19:47:09 +03:00
getFile
kickChatMember
unbanChatMember
restrictChatMember
promoteChatMember
exportChatInviteLink
setChatPhoto
deleteChatPhoto
setChatTitle
setChatDescription
pinChatMessage
unpinChatMessage
leaveChat
getChat
getChatAdministrators
getChatMembersCount
getChatMember
answerCallbackQuery
answerInlineQuery
"""
2015-06-26 09:55:13 +03:00
def __init__(self, token, threaded=True, skip_pending=False, num_threads=2):
2015-07-04 12:00:42 +03:00
"""
:param token: bot API token
2015-07-20 04:56:17 +03:00
:return: Telebot object.
2015-07-04 12:00:42 +03:00
"""
2015-06-26 09:55:13 +03:00
self.token = token
2015-06-26 13:02:30 +03:00
self.update_listener = []
self.skip_pending = skip_pending
self.__stop_polling = threading.Event()
self.last_update_id = 0
self.exc_info = None
# key: message_id, value: handler list
self.reply_handlers = {}
2015-07-30 06:02:08 +03:00
# key: chat_id, value: handler list
self.next_step_handlers = {}
2015-07-30 06:02:08 +03:00
self.message_handlers = []
2016-06-07 14:29:12 +03:00
self.edited_message_handlers = []
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 = []
2015-10-02 01:00:54 +03:00
self.threaded = threaded
if self.threaded:
self.worker_pool = util.ThreadPool(num_threads=num_threads)
2015-07-02 04:38:31 +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)
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)
def get_webhook_info(self):
result = apihelper.get_webhook_info(self.token)
return types.WebhookInfo.de_json(result)
def remove_webhook(self):
2015-10-02 01:00:54 +03:00
return self.set_webhook() # No params resets webhook
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
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)
return total
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.
"""
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)
self.process_new_updates(updates)
def process_new_updates(self, updates):
new_messages = []
2016-06-07 14:29:12 +03:00
edited_new_messages = []
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 = []
2017-05-25 06:22:40 +03:00
2015-06-26 13:02:30 +03:00
for update in updates:
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:
edited_new_messages.append(update.edited_message)
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)
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)))
if len(new_messages) > 0:
self.process_new_messages(new_messages)
2016-06-07 14:29:12 +03:00
if len(edited_new_messages) > 0:
self.process_new_edited_messages(edited_new_messages)
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_pre_checkout_querys) > 0:
self.process_new_pre_checkout_query(new_pre_checkout_querys)
if len(new_shipping_querys) > 0:
self.process_new_shipping_query(new_shipping_querys)
def process_new_messages(self, new_messages):
self._notify_next_handlers(new_messages)
self._notify_reply_handlers(new_messages)
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)
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)
2015-06-26 13:02:30 +03:00
def __notify_update(self, new_messages):
for listener in self.update_listener:
self._exec_task(listener, new_messages)
2015-06-26 13:02:30 +03:00
2018-04-04 10:47:37 +03:00
def infinity_polling(self, *args, **kwargs):
while not self.__stop_polling.is_set():
try:
self.polling(*args, **kwargs)
except Exception as e:
time.sleep(5)
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
"""
This function creates a new Thread that calls an internal __retrieve_updates function.
This allows the bot to retrieve Updates automagically and notify listeners and message handlers accordingly.
Warning: Do not call this function more than once!
2015-06-26 13:02:30 +03:00
Always get updates.
2017-08-06 09:25:25 +03:00
:param interval:
: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-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()
error_interval = .25
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
)
while not self.__stop_polling.wait(interval):
or_event.clear()
2015-06-27 17:31:40 +03:00
try:
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
polling_thread.raise_exceptions()
self.worker_pool.raise_exceptions()
error_interval = .25
except apihelper.ApiException as e:
logger.error(e)
2015-07-12 10:49:22 +03:00
if not none_stop:
self.__stop_polling.set()
2015-09-08 20:47:55 +03:00
logger.info("Exception occurred. Stopping.")
else:
polling_thread.clear_exceptions()
self.worker_pool.clear_exceptions()
logger.info("Waiting for {0} seconds until retry".format(error_interval))
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()
error_interval = .25
while not self.__stop_polling.wait(interval):
try:
self.__retrieve_updates(timeout)
error_interval = .25
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.')
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):
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)
return types.User.de_json(result)
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)
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:
:return: API reply.
"""
result = apihelper.get_user_profile_photos(self.token, user_id, offset, limit)
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
"""
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:
: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
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,
2016-02-27 06:17:35 +03:00
parse_mode=None, disable_notification=None):
2015-06-28 12:27:25 +03:00
"""
Use this method to send text messages.
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.
:return: API reply.
2015-06-28 12:27:25 +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,
reply_markup, parse_mode, disable_notification))
2015-06-26 17:16:11 +03:00
2016-02-27 06:17:35 +03:00
def forward_message(self, chat_id, from_chat_id, message_id, disable_notification=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
:return: API reply.
2015-06-26 17:35:52 +03:00
"""
2016-02-27 06:17:35 +03:00
return types.Message.de_json(
apihelper.forward_message(self.token, chat_id, from_chat_id, message_id, disable_notification))
2015-06-26 20:53:07 +03:00
def delete_message(self, chat_id, message_id):
"""
2018-04-04 10:47:37 +03:00
Use this method to delete message. Returns True on success.
:param chat_id: in which chat to delete
:param message_id: which message to delete
:return: API reply.
"""
return apihelper.delete_message(self.token, chat_id, message_id)
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,
2018-02-14 23:27:55 +03:00
parse_mode=None, disable_notification=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:
:return: API reply.
2015-06-26 21:14:45 +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,
2018-02-14 23:27:55 +03:00
parse_mode, disable_notification))
2015-06-26 20:53:07 +03:00
def send_audio(self, chat_id, audio, caption=None, duration=None, performer=None, title=None,
reply_to_message_id=None, reply_markup=None, parse_mode=None, disable_notification=None,
timeout=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.
:param duration:Duration of the audio in seconds
:param performer:Performer
:param title:Track name
2018-02-14 23:27:55 +03:00
:param parse_mode
: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:
2015-08-19 13:27:35 +03:00
:return: Message
2015-06-26 21:14:45 +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,
2018-02-14 23:27:55 +03:00
reply_markup, parse_mode, disable_notification, timeout))
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:
: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
2015-08-19 13:27:35 +03:00
:return: Message
"""
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:
:param reply_markup:
:param parse_mode:
:param disable_notification:
:return: API reply.
2015-06-26 21:14:45 +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
2016-06-07 14:29:12 +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:
:return: API reply.
2015-06-26 21:14:45 +03:00
"""
return types.Message.de_json(
2016-02-27 06:17:35 +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,
2018-02-14 23:27:55 +03:00
parse_mode=None, supports_streaming=None, disable_notification=None, timeout=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:
2015-08-01 05:12:15 +03:00
:return:
2015-06-26 21:14:45 +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,
2018-02-14 23:27:55 +03:00
parse_mode, supports_streaming, disable_notification, timeout))
2015-06-27 16:55:45 +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):
"""
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:
: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-11-29 08:45:25 +03:00
def send_media_group(self, chat_id, media, disable_notification=None, reply_to_message_id=None):
"""
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:
"""
result = apihelper.send_media_group(self.token, chat_id, media, disable_notification, reply_to_message_id)
ret = []
for msg in result:
ret.append(types.Message.de_json(msg))
return ret
def send_location(self, chat_id, latitude, longitude, live_period=None, reply_to_message_id=None, reply_markup=None,
2016-02-27 06:17:35 +03:00
disable_notification=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:
:param live_period
2015-06-28 12:27:25 +03:00
:param reply_to_message_id:
:param reply_markup:
:return: API reply.
2015-06-28 12:27:25 +03:00
"""
return types.Message.de_json(
2017-11-29 08:53:39 +03:00
apihelper.send_location(self.token, chat_id, latitude, longitude, live_period, reply_to_message_id,
reply_markup,
2016-02-27 06:17:35 +03:00
disable_notification))
2015-06-28 12:56:32 +03:00
def edit_message_live_location(self, latitude, longitude, chat_id=None, message_id=None,
inline_message_id=None, reply_markup=None):
"""
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(
apihelper.edit_message_live_location(self.token, latitude, longitude, chat_id, message_id,
inline_message_id, reply_markup))
def stop_message_live_location(self, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None):
"""
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(
apihelper.stop_message_live_location(self.token, chat_id, message_id, inline_message_id, reply_markup))
2016-04-14 08:55:28 +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):
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(
apihelper.send_venue(self.token, chat_id, latitude, longitude, title, address, foursquare_id,
disable_notification, reply_to_message_id, reply_markup)
)
2016-04-16 10:07:52 +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):
return types.Message.de_json(
apihelper.send_contact(self.token, chat_id, phone_number, first_name, last_name, disable_notification,
reply_to_message_id, reply_markup)
)
2015-06-28 12:56:32 +03:00
def send_chat_action(self, chat_id, action):
"""
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:
:param action: One of the following strings: 'typing', 'upload_photo', 'record_video', 'upload_video',
'record_audio', 'upload_audio', 'upload_document', 'find_location', 'record_video_note', 'upload_video_note'.
:return: API reply. :type: boolean
2015-06-28 12:56:32 +03:00
"""
return apihelper.send_chat_action(self.token, chat_id, action)
2015-07-02 04:38:31 +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
: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
2016-04-14 09:48:26 +03:00
:return: types.Message
"""
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):
return apihelper.unban_chat_member(self.token, chat_id, user_id)
2017-07-02 16:08:36 +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_other_messages=None,
can_add_web_page_previews=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
: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
: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
2017-06-30 19:47:09 +03:00
:return: types.Message
"""
return apihelper.restrict_chat_member(self.token, chat_id, user_id, until_date, can_send_messages,
can_send_media_messages, can_send_other_messages,
can_add_web_page_previews)
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)
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)
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):
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.
return result
return types.Message.de_json(result)
2016-04-14 10:03:07 +03:00
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):
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
2016-10-08 15:36:48 +03:00
def send_game(self, chat_id, game_short_name, disable_notification=None, reply_to_message_id=None,
reply_markup=None):
result = apihelper.send_game(self.token, chat_id, game_short_name, disable_notification, reply_to_message_id,
2016-10-08 16:55:28 +03:00
reply_markup)
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):
result = apihelper.set_game_score(self.token, user_id, score, force, chat_id, message_id, inline_message_id,
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):
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,
is_flexible=None, disable_notification=None, reply_to_message_id=None, reply_markup=None,
provider_data=None):
2017-05-25 05:56:58 +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, is_flexible,
2017-11-29 08:53:39 +03:00
disable_notification, reply_to_message_id, reply_markup, provider_data)
2017-05-25 05:56:58 +03:00
return types.Message.de_json(result)
def answer_shipping_query(self, shipping_query_id, ok, shipping_options=None, error_message=None):
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):
return apihelper.answer_pre_checkout_query(self.token, pre_checkout_query_id, ok, error_message)
def edit_message_caption(self, caption, chat_id=None, message_id=None, inline_message_id=None,
parse_mode=None, reply_markup=None):
2016-10-08 15:36:48 +03:00
result = apihelper.edit_message_caption(self.token, caption, chat_id, message_id, inline_message_id,
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
def reply_to(self, message, text, **kwargs):
"""
Convenience function for `send_message(message.chat.id, text, reply_to_message_id=message.message_id, **kwargs)`
"""
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:
: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 10:22:23 +03:00
# def send_sticker(self, chat_id, sticker, disable_notification=None, reply_to_message_id=None, reply_markup=None):
# """
# Use this method to send .webp stickers. On success, the sent Message is returned.
# :param chat_id:
# :param sticker:
# :param disable_notification:
# :param reply_to_message_id:
# :param reply_markup:
# :return:
# """
# result = apihelper.send_sticker(self.token, chat_id, sticker, disable_notification, reply_markup, reply_markup)
# return types.Message.de_json(result)
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 token:
: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)
def register_for_reply(self, message, callback, *args, **kwargs):
"""
Registers a callback function to be notified when a reply to `message` arrives.
Warning: `message` must be sent with reply_markup=types.ForceReply(), otherwise TeleBot will not be able to see
the difference between a reply to `message` and an ordinary message.
: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.
"""
message_id = message.message_id
self.register_for_reply_by_message_id(message_id, callback, *args, **kwargs)
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.
Warning: `message` must be sent with reply_markup=types.ForceReply(), otherwise TeleBot will not be able to see
the difference between a reply to `message` and an ordinary message.
: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.
"""
if message_id in self.reply_handlers.keys():
self.reply_handlers[message_id].append({"callback": callback, "args": args, "kwargs": kwargs})
else:
self.reply_handlers[message_id] = [{"callback": callback, "args": args, "kwargs": kwargs}]
def _notify_reply_handlers(self, new_messages):
for message in new_messages:
if hasattr(message, "reply_to_message") and message.reply_to_message is not None:
reply_msg_id = message.reply_to_message.message_id
if reply_msg_id in self.reply_handlers.keys():
handlers = self.reply_handlers[reply_msg_id]
for handler in handlers:
self._exec_task(handler["callback"], message, *handler["args"], **handler["kwargs"])
self.reply_handlers.pop(reply_msg_id)
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`.
: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.
: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
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`.
: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
"""
if chat_id in self.next_step_handlers.keys():
self.next_step_handlers[chat_id].append({"callback": callback, "args": args, "kwargs": kwargs})
2015-07-30 06:02:08 +03:00
else:
self.next_step_handlers[chat_id] = [{"callback": callback, "args": args, "kwargs": kwargs}]
2017-11-29 08:53:39 +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
self.clear_step_handler_by_chat_id(chat_id)
2015-07-30 06:02:08 +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
"""
self.next_step_handlers[chat_id] = []
def clear_reply_handlers(self, message):
"""
Clears all callback functions registered by register_for_reply() and register_for_reply_by_message_id().
:param message_id: The message for which we want to clear reply handlers
"""
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
"""
self.reply_handlers[message_id] = []
def _notify_next_handlers(self, new_messages):
i = 0
while i < len(new_messages):
message = new_messages[i]
2015-07-30 06:02:08 +03:00
chat_id = message.chat.id
if chat_id in self.next_step_handlers.keys():
handlers = self.next_step_handlers[chat_id]
2015-07-30 06:02:08 +03:00
for handler in handlers:
self._exec_task(handler["callback"], message, *handler["args"], **handler["kwargs"])
self.next_step_handlers.pop(chat_id, None)
new_messages.pop(i) # removing message that detects with next_step_handler
i += 1
@staticmethod
def _build_handler_dict(handler, **filters):
return {
'function': handler,
'filters': filters
}
def message_handler(self, commands=None, regexp=None, func=None, content_types=['text'], **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!')
# Handle all other commands.
@bot.message_handler(func=lambda message: True, content_types=['audio', 'video', 'document', 'text', 'location', 'contact', 'sticker'])
def default_command(message):
bot.send_message(message.chat.id, "This is the default command handler.")
: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
def decorator(handler):
handler_dict = self._build_handler_dict(handler,
commands=commands,
regexp=regexp,
func=func,
content_types=content_types,
**kwargs)
self.add_message_handler(handler_dict)
return handler
return decorator
def add_message_handler(self, handler_dict):
self.message_handlers.append(handler_dict)
def edited_message_handler(self, commands=None, regexp=None, func=None, content_types=['text'], **kwargs):
2016-06-07 14:29:12 +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_edited_message_handler(handler_dict)
2016-06-07 14:29:12 +03:00
return handler
return decorator
def add_edited_message_handler(self, handler_dict):
2016-06-07 14:29:12 +03:00
self.edited_message_handlers.append(handler_dict)
def channel_post_handler(self, commands=None, regexp=None, func=None, content_types=['text'], **kwargs):
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):
self.channel_post_handlers.append(handler_dict)
def edited_channel_post_handler(self, commands=None, regexp=None, func=None, content_types=['text'], **kwargs):
def decorator(handler):
handler_dict = self._build_handler_dict(handler,
commands=commands,
regexp=regexp,
func=func,
content_types=content_types,
**kwargs)
self.add_edited_channel_post_handler(handler_dict)
return handler
return decorator
def add_edited_channel_post_handler(self, handler_dict):
self.edited_channel_post_handlers.append(handler_dict)
def inline_handler(self, func, **kwargs):
def decorator(handler):
handler_dict = self._build_handler_dict(handler, func=func, **kwargs)
2016-06-13 14:24:27 +03:00
self.add_inline_handler(handler_dict)
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):
self.inline_handlers.append(handler_dict)
def chosen_inline_handler(self, func, **kwargs):
def decorator(handler):
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)
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):
self.chosen_inline_handlers.append(handler_dict)
def callback_query_handler(self, func, **kwargs):
2016-04-16 09:18:19 +03:00
def decorator(handler):
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)
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):
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):
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):
self.shipping_query_handlers.append(handler_dict)
def pre_checkout_query_handler(self, func, **kwargs):
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):
2017-05-25 06:22:40 +03:00
self.pre_checkout_query_handlers.append(handler_dict)
def _test_message_handler(self, message_handler, message):
for filter, filter_value in six.iteritems(message_handler['filters']):
if filter_value is None:
continue
if not self._test_filter(filter, filter_value, message):
2015-09-08 11:44:31 +03:00
return False
2015-09-08 11:44:31 +03:00
return True
def _test_filter(self, filter, filter_value, message):
test_cases = {
'content_types': lambda msg: msg.content_type in filter_value,
'regexp': lambda msg: msg.content_type == 'text' and re.search(filter_value, msg.text, re.IGNORECASE),
'commands': lambda msg: msg.content_type == 'text' and util.extract_command(msg.text) in filter_value,
'func': lambda msg: filter_value(msg)
}
return test_cases.get(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):
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
class AsyncTeleBot(TeleBot):
def __init__(self, *args, **kwargs):
TeleBot.__init__(self, *args, **kwargs)
@util.async()
def get_me(self):
return TeleBot.get_me(self)
2017-05-18 12:55:55 +03:00
@util.async()
def get_file(self, *args):
return TeleBot.get_file(self, *args)
@util.async()
def download_file(self, *args):
return TeleBot.download_file(self, *args)
@util.async()
def get_user_profile_photos(self, *args, **kwargs):
return TeleBot.get_user_profile_photos(self, *args, **kwargs)
2017-05-18 12:55:55 +03:00
@util.async()
def get_chat(self, *args):
return TeleBot.get_chat(self, *args)
@util.async()
def leave_chat(self, *args):
return TeleBot.leave_chat(self, *args)
@util.async()
def get_chat_administrators(self, *args):
return TeleBot.get_chat_administrators(self, *args)
@util.async()
def get_chat_members_count(self, *args):
return TeleBot.get_chat_members_count(self, *args)
@util.async()
def set_chat_sticker_set(self, *args):
return TeleBot.set_chat_sticker_set(self, *args)
@util.async()
def delete_chat_sticker_set(self, *args):
return TeleBot.delete_chat_sticker_set(self, *args)
2017-05-18 12:55:55 +03:00
@util.async()
def get_chat_member(self, *args):
return TeleBot.get_chat_member(self, *args)
@util.async()
def send_message(self, *args, **kwargs):
return TeleBot.send_message(self, *args, **kwargs)
@util.async()
def forward_message(self, *args, **kwargs):
return TeleBot.forward_message(self, *args, **kwargs)
2017-05-18 12:55:55 +03:00
@util.async()
def delete_message(self, *args):
return TeleBot.delete_message(self, *args)
@util.async()
def send_photo(self, *args, **kwargs):
return TeleBot.send_photo(self, *args, **kwargs)
@util.async()
def send_audio(self, *args, **kwargs):
return TeleBot.send_audio(self, *args, **kwargs)
2017-05-18 12:55:55 +03:00
@util.async()
def send_voice(self, *args, **kwargs):
return TeleBot.send_voice(self, *args, **kwargs)
@util.async()
def send_document(self, *args, **kwargs):
return TeleBot.send_document(self, *args, **kwargs)
@util.async()
def send_sticker(self, *args, **kwargs):
return TeleBot.send_sticker(self, *args, **kwargs)
@util.async()
def send_video(self, *args, **kwargs):
return TeleBot.send_video(self, *args, **kwargs)
@util.async()
def send_video_note(self, *args, **kwargs):
return TeleBot.send_video_note(self, *args, **kwargs)
2017-11-29 08:53:39 +03:00
@util.async()
def send_media_group(self, *args, **kwargs):
return TeleBot.send_media_group(self, *args, **kwargs)
@util.async()
def send_location(self, *args, **kwargs):
return TeleBot.send_location(self, *args, **kwargs)
@util.async()
def edit_message_live_location(self, *args, **kwargs):
return TeleBot.edit_message_live_location(self, *args, **kwargs)
@util.async()
def stop_message_live_location(self, *args, **kwargs):
return TeleBot.stop_message_live_location(self, *args, **kwargs)
2017-05-18 12:55:55 +03:00
@util.async()
def send_venue(self, *args, **kwargs):
return TeleBot.send_venue(self, *args, **kwargs)
@util.async()
def send_contact(self, *args, **kwargs):
return TeleBot.send_contact(self, *args, **kwargs)
@util.async()
def send_chat_action(self, *args, **kwargs):
return TeleBot.send_chat_action(self, *args, **kwargs)
2017-05-18 12:55:55 +03:00
@util.async()
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
@util.async()
def unban_chat_member(self, *args):
return TeleBot.unban_chat_member(self, *args)
2017-07-07 00:54:18 +03:00
@util.async()
def restrict_chat_member(self, *args, **kwargs):
return TeleBot.restrict_chat_member(self, *args, **kwargs)
@util.async()
def promote_chat_member(self, *args, **kwargs):
return TeleBot.promote_chat_member(self, *args, **kwargs)
@util.async()
def export_chat_invite_link(self, *args):
return TeleBot.export_chat_invite_link(self, *args)
@util.async()
def set_chat_photo(self, *args):
return TeleBot.set_chat_photo(self, *args)
@util.async()
def delete_chat_photo(self, *args):
return TeleBot.delete_chat_photo(self, *args)
@util.async()
def set_chat_title(self, *args):
return TeleBot.set_chat_title(self, *args)
@util.async()
def set_chat_description(self, *args):
return TeleBot.set_chat_description(self, *args)
@util.async()
def pin_chat_message(self, *args, **kwargs):
return TeleBot.pin_chat_message(self, *args, **kwargs)
@util.async()
def unpin_chat_message(self, *args):
return TeleBot.unpin_chat_message(self, *args)
2017-05-18 12:55:55 +03:00
@util.async()
def edit_message_text(self, *args, **kwargs):
return TeleBot.edit_message_text(self, *args, **kwargs)
@util.async()
def edit_message_reply_markup(self, *args, **kwargs):
return TeleBot.edit_message_reply_markup(self, *args, **kwargs)
@util.async()
def send_game(self, *args, **kwargs):
return TeleBot.send_game(self, *args, **kwargs)
@util.async()
def set_game_score(self, *args, **kwargs):
return TeleBot.set_game_score(self, *args, **kwargs)
@util.async()
def get_game_high_scores(self, *args, **kwargs):
return TeleBot.get_game_high_scores(self, *args, **kwargs)
2017-05-25 05:56:58 +03:00
@util.async()
def send_invoice(self, *args, **kwargs):
return TeleBot.send_invoice(self, *args, **kwargs)
@util.async()
def answer_shipping_query(self, *args, **kwargs):
return TeleBot.answer_shipping_query(self, *args, **kwargs)
@util.async()
def answer_pre_checkout_query(self, *args, **kwargs):
return TeleBot.answer_pre_checkout_query(self, *args, **kwargs)
2017-05-18 12:55:55 +03:00
@util.async()
def edit_message_caption(self, *args, **kwargs):
return TeleBot.edit_message_caption(self, *args, **kwargs)
@util.async()
def answer_inline_query(self, *args, **kwargs):
return TeleBot.answer_inline_query(self, *args, **kwargs)
@util.async()
def answer_callback_query(self, *args, **kwargs):
return TeleBot.answer_callback_query(self, *args, **kwargs)
2017-08-06 09:25:25 +03:00
@util.async()
def send_sticker(self, *args, **kwargs):
return TeleBot.send_sticker(self, *args, **kwargs)
@util.async()
def get_sticker_set(self, *args, **kwargs):
return TeleBot.get_sticker_set(self, *args, **kwargs)
@util.async()
def upload_sticker_file(self, *args, **kwargs):
return TeleBot.upload_sticker_file(self, *args, **kwargs)
@util.async()
def create_new_sticker_set(self, *args, **kwargs):
return TeleBot.create_new_sticker_set(self, *args, **kwargs)
@util.async()
def add_sticker_to_set(self, *args, **kwargs):
return TeleBot.add_sticker_to_set(self, *args, **kwargs)
@util.async()
def set_sticker_position_in_set(self, *args, **kwargs):
return TeleBot.set_sticker_position_in_set(self, *args, **kwargs)
@util.async()
def delete_sticker_from_set(self, *args, **kwargs):
return TeleBot.delete_sticker_from_set(self, *args, **kwargs)