2015-07-07 21:50:36 +02:00
|
|
|
#!/usr/bin/env python
|
2015-08-28 17:19:30 +02:00
|
|
|
# pylint: disable=E0611,E0213,E1102,C0103,E1101,W0613,R0913,R0904
|
2015-08-11 21:58:17 +02:00
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2016-01-05 14:12:03 +01:00
|
|
|
# Copyright (C) 2015-2016
|
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
2015-08-11 21:58:17 +02:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2015-08-10 18:57:31 +02:00
|
|
|
|
2016-01-13 17:09:35 +01:00
|
|
|
"""This module contains a object that represents a Telegram Bot."""
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
import logging
|
2016-04-24 15:06:59 +02:00
|
|
|
import functools
|
2016-04-27 00:28:21 +02:00
|
|
|
|
2016-04-21 14:21:12 +02:00
|
|
|
from telegram import User, Message, Update, UserProfilePhotos, File, \
|
|
|
|
ReplyMarkup, TelegramObject, NullHandler
|
2015-09-05 16:55:55 +02:00
|
|
|
from telegram.utils import request
|
2016-04-21 13:15:38 +02:00
|
|
|
from telegram.utils.validate import validate_token
|
2015-08-09 14:41:58 +02:00
|
|
|
|
2016-03-15 02:56:20 +01:00
|
|
|
logging.getLogger(__name__).addHandler(NullHandler())
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
|
|
|
|
class Bot(TelegramObject):
|
2015-08-28 17:19:30 +02:00
|
|
|
"""This object represents a Telegram Bot.
|
|
|
|
|
|
|
|
Attributes:
|
2016-04-21 13:15:38 +02:00
|
|
|
id (int): Unique identifier for this bot.
|
2016-04-21 16:59:18 +02:00
|
|
|
first_name (str): Bot's first name.
|
|
|
|
last_name (str): Bot's last name.
|
|
|
|
username (str): Bot's username.
|
|
|
|
name (str): Bot's @username.
|
2015-08-28 17:19:30 +02:00
|
|
|
|
|
|
|
Args:
|
2016-04-21 13:15:38 +02:00
|
|
|
token (str): Bot's unique authentication.
|
|
|
|
base_url (Optional[str]): Telegram Bot API service URL.
|
|
|
|
base_file_url (Optional[str]): Telegram Bot API file URL.
|
2016-04-26 16:43:35 +02:00
|
|
|
|
2015-08-28 17:19:30 +02:00
|
|
|
"""
|
2015-07-09 18:19:58 +02:00
|
|
|
|
2015-07-07 21:50:36 +02:00
|
|
|
def __init__(self,
|
|
|
|
token,
|
2016-04-19 14:04:25 +02:00
|
|
|
base_url=None,
|
|
|
|
base_file_url=None):
|
2016-04-21 13:15:38 +02:00
|
|
|
self.token = validate_token(token)
|
2015-07-08 22:23:18 +02:00
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
if not base_url:
|
2016-04-21 17:15:37 +02:00
|
|
|
self.base_url = 'https://api.telegram.org/bot{0}'.format(
|
2016-04-21 14:21:12 +02:00
|
|
|
self.token)
|
2015-07-07 21:50:36 +02:00
|
|
|
else:
|
2015-07-08 22:23:18 +02:00
|
|
|
self.base_url = base_url + self.token
|
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
if not base_file_url:
|
2016-04-21 17:15:37 +02:00
|
|
|
self.base_file_url = 'https://api.telegram.org/file/bot{0}'.format(
|
2016-04-21 14:21:12 +02:00
|
|
|
self.token)
|
2016-04-19 14:04:25 +02:00
|
|
|
else:
|
|
|
|
self.base_file_url = base_file_url + self.token
|
2015-09-20 17:28:10 +02:00
|
|
|
|
2015-08-14 21:25:27 +02:00
|
|
|
self.bot = None
|
|
|
|
|
2015-08-28 17:19:30 +02:00
|
|
|
self.logger = logging.getLogger(__name__)
|
2015-07-20 13:36:08 +02:00
|
|
|
|
2015-08-14 21:25:27 +02:00
|
|
|
def info(func):
|
|
|
|
@functools.wraps(func)
|
|
|
|
def decorator(self, *args, **kwargs):
|
|
|
|
if not self.bot:
|
|
|
|
self.getMe()
|
|
|
|
|
|
|
|
result = func(self, *args, **kwargs)
|
|
|
|
return result
|
2016-04-14 07:01:05 +02:00
|
|
|
|
2015-08-14 21:25:27 +02:00
|
|
|
return decorator
|
|
|
|
|
|
|
|
@property
|
|
|
|
@info
|
|
|
|
def id(self):
|
|
|
|
return self.bot.id
|
2015-07-08 22:23:18 +02:00
|
|
|
|
2015-08-14 21:25:27 +02:00
|
|
|
@property
|
|
|
|
@info
|
|
|
|
def first_name(self):
|
|
|
|
return self.bot.first_name
|
2015-07-08 22:23:18 +02:00
|
|
|
|
2015-08-14 21:25:27 +02:00
|
|
|
@property
|
|
|
|
@info
|
|
|
|
def last_name(self):
|
|
|
|
return self.bot.last_name
|
2015-07-20 13:59:41 +02:00
|
|
|
|
2015-08-14 21:25:27 +02:00
|
|
|
@property
|
|
|
|
@info
|
|
|
|
def username(self):
|
|
|
|
return self.bot.username
|
2015-07-08 22:23:18 +02:00
|
|
|
|
|
|
|
@property
|
2015-07-15 15:05:31 +02:00
|
|
|
def name(self):
|
2016-04-21 17:15:37 +02:00
|
|
|
return '@{0}'.format(self.username)
|
2015-07-08 22:23:18 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
def log(func):
|
|
|
|
logger = logging.getLogger(func.__module__)
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
@functools.wraps(func)
|
|
|
|
def decorator(self, *args, **kwargs):
|
2015-08-28 17:19:30 +02:00
|
|
|
logger.debug('Entering: %s', func.__name__)
|
2015-07-20 12:53:58 +02:00
|
|
|
result = func(self, *args, **kwargs)
|
|
|
|
logger.debug(result)
|
2015-08-28 17:19:30 +02:00
|
|
|
logger.debug('Exiting: %s', func.__name__)
|
2015-07-20 12:53:58 +02:00
|
|
|
return result
|
2016-04-14 07:01:05 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
return decorator
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-07-12 00:01:02 +02:00
|
|
|
def message(func):
|
2015-07-20 12:53:58 +02:00
|
|
|
@functools.wraps(func)
|
|
|
|
def decorator(self, *args, **kwargs):
|
2015-07-12 00:01:02 +02:00
|
|
|
url, data = func(self, *args, **kwargs)
|
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
if kwargs.get('reply_to_message_id'):
|
|
|
|
data['reply_to_message_id'] = \
|
|
|
|
kwargs.get('reply_to_message_id')
|
2015-07-12 00:01:02 +02:00
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
if kwargs.get('disable_notification'):
|
|
|
|
data['disable_notification'] = \
|
|
|
|
kwargs.get('disable_notification')
|
2015-07-12 00:01:02 +02:00
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
if kwargs.get('reply_markup'):
|
|
|
|
reply_markup = kwargs.get('reply_markup')
|
|
|
|
if isinstance(reply_markup, ReplyMarkup):
|
|
|
|
data['reply_markup'] = reply_markup.to_json()
|
|
|
|
else:
|
|
|
|
data['reply_markup'] = reply_markup
|
2016-03-09 16:47:33 +01:00
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
result = request.post(url, data,
|
2016-04-27 23:31:36 +02:00
|
|
|
timeout=kwargs.get('timeout'))
|
2015-07-12 00:01:02 +02:00
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
if result is True:
|
|
|
|
return result
|
2016-02-27 22:00:33 +01:00
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
return Message.de_json(result)
|
2016-02-27 22:00:33 +01:00
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
return decorator
|
2015-07-12 00:01:02 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
@log
|
|
|
|
def getMe(self):
|
|
|
|
"""A simple method for testing your bot's auth token.
|
|
|
|
|
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
:class:`telegram.User`: A :class:`telegram.User` instance
|
|
|
|
representing that bot if the credentials are valid, `None`
|
|
|
|
otherwise.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
"""
|
2016-04-21 13:15:38 +02:00
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/getMe'.format(self.base_url)
|
2015-07-20 12:53:58 +02:00
|
|
|
|
2015-09-05 16:55:55 +02:00
|
|
|
result = request.get(url)
|
2015-07-20 12:53:58 +02:00
|
|
|
|
2015-09-05 16:55:55 +02:00
|
|
|
self.bot = User.de_json(result)
|
2015-08-14 21:30:30 +02:00
|
|
|
|
2015-08-14 21:25:27 +02:00
|
|
|
return self.bot
|
2015-07-20 12:53:58 +02:00
|
|
|
|
|
|
|
@log
|
2015-07-12 00:01:02 +02:00
|
|
|
@message
|
2015-07-07 21:50:36 +02:00
|
|
|
def sendMessage(self,
|
|
|
|
chat_id,
|
|
|
|
text,
|
2015-09-10 19:15:20 +02:00
|
|
|
parse_mode=None,
|
2015-07-07 21:50:36 +02:00
|
|
|
disable_web_page_preview=None,
|
2015-08-28 17:19:30 +02:00
|
|
|
**kwargs):
|
2015-07-08 00:54:00 +02:00
|
|
|
"""Use this method to send text messages.
|
|
|
|
|
|
|
|
Args:
|
2016-04-19 14:04:25 +02:00
|
|
|
chat_id (str): Unique identifier for the target chat or
|
|
|
|
username of the target channel (in the format
|
|
|
|
@channelusername).
|
|
|
|
text (str): Text of the message to be sent. The current maximum
|
|
|
|
length is 4096 UTF-8 characters.
|
|
|
|
parse_mode (Optional[str]): Send Markdown or HTML, if you want
|
|
|
|
Telegram apps to show bold, italic, fixed-width text or inline
|
|
|
|
URLs in your bot's message.
|
|
|
|
disable_web_page_preview (Optional[bool]): Disables link previews
|
|
|
|
for links in this message.
|
|
|
|
**kwargs (dict): Arbitrary keyword arguments.
|
|
|
|
|
|
|
|
Keyword Args:
|
|
|
|
disable_notification (Optional[bool]): Sends the message silently.
|
|
|
|
iOS users will not receive a notification, Android users will
|
|
|
|
receive a notification with no sound.
|
|
|
|
reply_to_message_id (Optional[int]): If the message is a reply,
|
|
|
|
ID of the original message.
|
|
|
|
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional
|
|
|
|
interface options. A JSON-serialized object for an inline
|
|
|
|
keyboard, custom reply keyboard, instructions to hide reply
|
|
|
|
keyboard or to force a reply from the user.
|
2016-04-21 13:15:38 +02:00
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
2015-07-08 21:58:18 +02:00
|
|
|
|
2015-07-08 00:54:00 +02:00
|
|
|
Returns:
|
2016-04-19 14:04:25 +02:00
|
|
|
:class:`telegram.Message`: On success, the sent message is
|
|
|
|
returned.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2015-07-08 00:54:00 +02:00
|
|
|
"""
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/sendMessage'.format(self.base_url)
|
2015-07-07 21:50:36 +02:00
|
|
|
|
|
|
|
data = {'chat_id': chat_id,
|
|
|
|
'text': text}
|
2015-07-12 00:01:02 +02:00
|
|
|
|
2015-09-10 19:15:20 +02:00
|
|
|
if parse_mode:
|
|
|
|
data['parse_mode'] = parse_mode
|
2015-07-07 21:50:36 +02:00
|
|
|
if disable_web_page_preview:
|
|
|
|
data['disable_web_page_preview'] = disable_web_page_preview
|
|
|
|
|
2015-07-15 15:05:31 +02:00
|
|
|
return url, data
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
@log
|
2015-07-12 00:01:02 +02:00
|
|
|
@message
|
2015-07-07 21:50:36 +02:00
|
|
|
def forwardMessage(self,
|
|
|
|
chat_id,
|
|
|
|
from_chat_id,
|
2016-04-21 13:15:38 +02:00
|
|
|
message_id,
|
|
|
|
**kwargs):
|
2015-07-08 00:54:00 +02:00
|
|
|
"""Use this method to forward messages of any kind.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id:
|
2015-12-16 15:31:02 +01:00
|
|
|
Unique identifier for the message recipient - Chat id.
|
2015-07-08 00:54:00 +02:00
|
|
|
from_chat_id:
|
|
|
|
Unique identifier for the chat where the original message was sent
|
2015-12-16 15:31:02 +01:00
|
|
|
- Chat id.
|
2015-07-08 00:54:00 +02:00
|
|
|
message_id:
|
|
|
|
Unique message identifier.
|
2016-04-21 13:15:38 +02:00
|
|
|
|
|
|
|
Keyword Args:
|
|
|
|
disable_notification (Optional[bool]): Sends the message silently.
|
|
|
|
iOS users will not receive a notification, Android users will
|
|
|
|
receive a notification with no sound.
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
2015-07-08 21:58:18 +02:00
|
|
|
|
2015-07-08 00:54:00 +02:00
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
:class:`telegram.Message`: On success, instance representing the
|
|
|
|
message forwarded.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2015-07-08 00:54:00 +02:00
|
|
|
"""
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/forwardMessage'.format(self.base_url)
|
2015-07-07 21:50:36 +02:00
|
|
|
|
|
|
|
data = {}
|
2016-04-19 14:04:25 +02:00
|
|
|
|
2015-07-07 21:50:36 +02:00
|
|
|
if chat_id:
|
|
|
|
data['chat_id'] = chat_id
|
|
|
|
if from_chat_id:
|
|
|
|
data['from_chat_id'] = from_chat_id
|
|
|
|
if message_id:
|
|
|
|
data['message_id'] = message_id
|
|
|
|
|
2015-07-15 15:05:31 +02:00
|
|
|
return url, data
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
@log
|
2015-07-12 00:01:02 +02:00
|
|
|
@message
|
2015-07-07 21:50:36 +02:00
|
|
|
def sendPhoto(self,
|
|
|
|
chat_id,
|
|
|
|
photo,
|
|
|
|
caption=None,
|
2015-08-28 17:19:30 +02:00
|
|
|
**kwargs):
|
2015-07-08 00:54:00 +02:00
|
|
|
"""Use this method to send photos.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id:
|
2015-12-16 15:31:02 +01:00
|
|
|
Unique identifier for the message recipient - Chat id.
|
2015-07-08 00:54:00 +02:00
|
|
|
photo:
|
|
|
|
Photo to send. You can either pass a file_id as String to resend a
|
|
|
|
photo that is already on the Telegram servers, or upload a new
|
|
|
|
photo using multipart/form-data.
|
|
|
|
caption:
|
|
|
|
Photo caption (may also be used when resending photos by file_id).
|
|
|
|
[Optional]
|
2016-04-21 13:15:38 +02:00
|
|
|
|
|
|
|
Keyword Args:
|
|
|
|
disable_notification (Optional[bool]): Sends the message silently.
|
|
|
|
iOS users will not receive a notification, Android users will
|
|
|
|
receive a notification with no sound.
|
|
|
|
reply_to_message_id (Optional[int]): If the message is a reply,
|
|
|
|
ID of the original message.
|
|
|
|
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional
|
|
|
|
interface options. A JSON-serialized object for an inline
|
|
|
|
keyboard, custom reply keyboard, instructions to hide reply
|
|
|
|
keyboard or to force a reply from the user.
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
2015-07-08 21:58:18 +02:00
|
|
|
|
2015-07-08 00:54:00 +02:00
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
:class:`telegram.Message`: On success, instance representing the
|
|
|
|
message posted.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2015-07-08 00:54:00 +02:00
|
|
|
"""
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/sendPhoto'.format(self.base_url)
|
2015-07-07 21:50:36 +02:00
|
|
|
|
|
|
|
data = {'chat_id': chat_id,
|
|
|
|
'photo': photo}
|
2015-07-07 23:46:32 +02:00
|
|
|
|
2015-07-07 21:50:36 +02:00
|
|
|
if caption:
|
|
|
|
data['caption'] = caption
|
|
|
|
|
2015-07-15 15:05:31 +02:00
|
|
|
return url, data
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
@log
|
2015-07-12 00:01:02 +02:00
|
|
|
@message
|
2015-07-08 01:10:43 +02:00
|
|
|
def sendAudio(self,
|
|
|
|
chat_id,
|
|
|
|
audio,
|
2015-08-17 16:34:42 +02:00
|
|
|
duration=None,
|
|
|
|
performer=None,
|
|
|
|
title=None,
|
2015-08-28 17:19:30 +02:00
|
|
|
**kwargs):
|
2015-07-08 01:10:43 +02:00
|
|
|
"""Use this method to send audio files, if you want Telegram clients to
|
2015-08-17 16:34:42 +02:00
|
|
|
display them in the music player. Your audio must be in an .mp3 format.
|
|
|
|
On success, the sent Message is returned. Bots can currently send audio
|
|
|
|
files of up to 50 MB in size, this limit may be changed in the future.
|
|
|
|
|
|
|
|
For backward compatibility, when both fields title and description are
|
|
|
|
empty and mime-type of the sent file is not "audio/mpeg", file is sent
|
|
|
|
as playable voice message. In this case, your audio must be in an .ogg
|
|
|
|
file encoded with OPUS. This will be removed in the future. You need to
|
|
|
|
use sendVoice method instead.
|
2015-07-08 01:10:43 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id:
|
2015-12-16 15:31:02 +01:00
|
|
|
Unique identifier for the message recipient - Chat id.
|
2015-07-08 01:10:43 +02:00
|
|
|
audio:
|
|
|
|
Audio file to send. You can either pass a file_id as String to
|
|
|
|
resend an audio that is already on the Telegram servers, or upload
|
|
|
|
a new audio file using multipart/form-data.
|
2015-08-17 16:34:42 +02:00
|
|
|
duration:
|
|
|
|
Duration of sent audio in seconds. [Optional]
|
|
|
|
performer:
|
|
|
|
Performer of sent audio. [Optional]
|
|
|
|
title:
|
|
|
|
Title of sent audio. [Optional]
|
2016-04-21 13:15:38 +02:00
|
|
|
|
|
|
|
Keyword Args:
|
|
|
|
disable_notification (Optional[bool]): Sends the message silently.
|
|
|
|
iOS users will not receive a notification, Android users will
|
|
|
|
receive a notification with no sound.
|
|
|
|
reply_to_message_id (Optional[int]): If the message is a reply,
|
|
|
|
ID of the original message.
|
|
|
|
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional
|
|
|
|
interface options. A JSON-serialized object for an inline
|
|
|
|
keyboard, custom reply keyboard, instructions to hide reply
|
|
|
|
keyboard or to force a reply from the user.
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
2015-07-08 21:58:18 +02:00
|
|
|
|
2015-07-08 01:10:43 +02:00
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
:class:`telegram.Message`: On success, instance representing the
|
|
|
|
message posted.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2015-07-08 01:10:43 +02:00
|
|
|
"""
|
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/sendAudio'.format(self.base_url)
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-07-08 01:10:43 +02:00
|
|
|
data = {'chat_id': chat_id,
|
|
|
|
'audio': audio}
|
|
|
|
|
2015-08-17 16:34:42 +02:00
|
|
|
if duration:
|
|
|
|
data['duration'] = duration
|
|
|
|
if performer:
|
|
|
|
data['performer'] = performer
|
|
|
|
if title:
|
|
|
|
data['title'] = title
|
|
|
|
|
2015-07-15 15:05:31 +02:00
|
|
|
return url, data
|
2015-07-08 01:10:43 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
@log
|
2015-07-12 00:01:02 +02:00
|
|
|
@message
|
2015-07-08 02:12:51 +02:00
|
|
|
def sendDocument(self,
|
|
|
|
chat_id,
|
|
|
|
document,
|
2015-09-08 19:42:23 +02:00
|
|
|
filename=None,
|
2016-04-24 17:54:36 +02:00
|
|
|
caption=None,
|
2015-08-28 17:19:30 +02:00
|
|
|
**kwargs):
|
2015-08-11 17:41:26 +02:00
|
|
|
"""Use this method to send general files.
|
2015-07-08 02:12:51 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id:
|
2015-12-16 15:31:02 +01:00
|
|
|
Unique identifier for the message recipient - Chat id.
|
2015-07-08 02:12:51 +02:00
|
|
|
document:
|
|
|
|
File to send. You can either pass a file_id as String to resend a
|
|
|
|
file that is already on the Telegram servers, or upload a new file
|
|
|
|
using multipart/form-data.
|
2015-09-08 19:42:23 +02:00
|
|
|
filename:
|
|
|
|
File name that shows in telegram message (it is usefull when you
|
|
|
|
send file generated by temp module, for example). [Optional]
|
2016-04-24 17:51:15 +02:00
|
|
|
caption:
|
|
|
|
Document caption (may also be used when resending documents by
|
|
|
|
file_id), 0-200 characters. [Optional]
|
2016-04-21 13:15:38 +02:00
|
|
|
|
|
|
|
Keyword Args:
|
|
|
|
disable_notification (Optional[bool]): Sends the message silently.
|
|
|
|
iOS users will not receive a notification, Android users will
|
|
|
|
receive a notification with no sound.
|
|
|
|
reply_to_message_id (Optional[int]): If the message is a reply,
|
|
|
|
ID of the original message.
|
|
|
|
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional
|
|
|
|
interface options. A JSON-serialized object for an inline
|
|
|
|
keyboard, custom reply keyboard, instructions to hide reply
|
|
|
|
keyboard or to force a reply from the user.
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
2015-07-08 21:58:18 +02:00
|
|
|
|
2015-07-08 02:12:51 +02:00
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
:class:`telegram.Message`: On success, instance representing the
|
|
|
|
message posted.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2015-07-08 02:12:51 +02:00
|
|
|
"""
|
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/sendDocument'.format(self.base_url)
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-07-08 02:12:51 +02:00
|
|
|
data = {'chat_id': chat_id,
|
|
|
|
'document': document}
|
|
|
|
|
2015-09-08 19:42:23 +02:00
|
|
|
if filename:
|
|
|
|
data['filename'] = filename
|
2016-04-24 17:51:15 +02:00
|
|
|
if caption:
|
|
|
|
data['caption'] = caption
|
2015-09-11 01:08:24 +02:00
|
|
|
|
2015-07-15 15:05:31 +02:00
|
|
|
return url, data
|
2015-07-08 02:12:51 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
@log
|
2015-07-12 00:01:02 +02:00
|
|
|
@message
|
2015-07-08 04:52:12 +02:00
|
|
|
def sendSticker(self,
|
|
|
|
chat_id,
|
|
|
|
sticker,
|
2015-08-28 17:19:30 +02:00
|
|
|
**kwargs):
|
2015-07-08 14:17:18 +02:00
|
|
|
"""Use this method to send .webp stickers.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id:
|
2015-12-16 15:31:02 +01:00
|
|
|
Unique identifier for the message recipient - Chat id.
|
2015-07-08 14:17:18 +02:00
|
|
|
sticker:
|
|
|
|
Sticker to send. You can either pass a file_id as String to resend
|
|
|
|
a sticker that is already on the Telegram servers, or upload a new
|
|
|
|
sticker using multipart/form-data.
|
2016-04-21 13:15:38 +02:00
|
|
|
|
|
|
|
Keyword Args:
|
|
|
|
disable_notification (Optional[bool]): Sends the message silently.
|
|
|
|
iOS users will not receive a notification, Android users will
|
|
|
|
receive a notification with no sound.
|
|
|
|
reply_to_message_id (Optional[int]): If the message is a reply,
|
|
|
|
ID of the original message.
|
|
|
|
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional
|
|
|
|
interface options. A JSON-serialized object for an inline
|
|
|
|
keyboard, custom reply keyboard, instructions to hide reply
|
|
|
|
keyboard or to force a reply from the user.
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
2015-07-08 21:58:18 +02:00
|
|
|
|
2015-07-08 14:17:18 +02:00
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
:class:`telegram.Message`: On success, instance representing the
|
|
|
|
message posted.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2015-07-08 14:17:18 +02:00
|
|
|
"""
|
2015-07-08 04:52:12 +02:00
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/sendSticker'.format(self.base_url)
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-07-08 04:52:12 +02:00
|
|
|
data = {'chat_id': chat_id,
|
|
|
|
'sticker': sticker}
|
2015-09-11 01:08:24 +02:00
|
|
|
|
2015-07-15 15:05:31 +02:00
|
|
|
return url, data
|
2015-07-08 04:52:12 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
@log
|
2016-04-19 14:04:25 +02:00
|
|
|
@message
|
2015-07-08 14:17:18 +02:00
|
|
|
def sendVideo(self,
|
|
|
|
chat_id,
|
|
|
|
video,
|
2015-08-11 22:32:06 +02:00
|
|
|
duration=None,
|
|
|
|
caption=None,
|
2015-08-28 17:19:30 +02:00
|
|
|
**kwargs):
|
2015-07-08 14:17:18 +02:00
|
|
|
"""Use this method to send video files, Telegram clients support mp4
|
|
|
|
videos (other formats may be sent as telegram.Document).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id:
|
2015-12-16 15:31:02 +01:00
|
|
|
Unique identifier for the message recipient - Chat id.
|
2015-07-08 14:17:18 +02:00
|
|
|
video:
|
|
|
|
Video to send. You can either pass a file_id as String to resend a
|
|
|
|
video that is already on the Telegram servers, or upload a new
|
|
|
|
video file using multipart/form-data.
|
2015-08-11 22:32:06 +02:00
|
|
|
duration:
|
|
|
|
Duration of sent video in seconds. [Optional]
|
|
|
|
caption:
|
|
|
|
Video caption (may also be used when resending videos by file_id).
|
|
|
|
[Optional]
|
2016-04-21 13:15:38 +02:00
|
|
|
|
|
|
|
Keyword Args:
|
|
|
|
disable_notification (Optional[bool]): Sends the message silently.
|
|
|
|
iOS users will not receive a notification, Android users will
|
|
|
|
receive a notification with no sound.
|
|
|
|
reply_to_message_id (Optional[int]): If the message is a reply,
|
|
|
|
ID of the original message.
|
|
|
|
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional
|
|
|
|
interface options. A JSON-serialized object for an inline
|
|
|
|
keyboard, custom reply keyboard, instructions to hide reply
|
|
|
|
keyboard or to force a reply from the user.
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
2015-07-08 21:58:18 +02:00
|
|
|
|
2015-07-08 14:17:18 +02:00
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
:class:`telegram.Message`: On success, instance representing the
|
|
|
|
message posted.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2015-07-08 14:17:18 +02:00
|
|
|
"""
|
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/sendVideo'.format(self.base_url)
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-07-08 14:17:18 +02:00
|
|
|
data = {'chat_id': chat_id,
|
|
|
|
'video': video}
|
|
|
|
|
2015-08-11 22:32:06 +02:00
|
|
|
if duration:
|
|
|
|
data['duration'] = duration
|
|
|
|
if caption:
|
|
|
|
data['caption'] = caption
|
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
return url, data
|
2015-07-08 14:17:18 +02:00
|
|
|
|
2015-08-17 16:34:42 +02:00
|
|
|
@log
|
|
|
|
@message
|
|
|
|
def sendVoice(self,
|
|
|
|
chat_id,
|
|
|
|
voice,
|
|
|
|
duration=None,
|
2015-08-28 17:19:30 +02:00
|
|
|
**kwargs):
|
2015-08-17 16:34:42 +02:00
|
|
|
"""Use this method to send audio files, if you want Telegram clients to
|
|
|
|
display the file as a playable voice message. For this to work, your
|
|
|
|
audio must be in an .ogg file encoded with OPUS (other formats may be
|
|
|
|
sent as Audio or Document). On success, the sent Message is returned.
|
|
|
|
Bots can currently send audio files of up to 50 MB in size, this limit
|
|
|
|
may be changed in the future.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id:
|
2015-12-16 15:31:02 +01:00
|
|
|
Unique identifier for the message recipient - Chat id.
|
2015-08-17 16:34:42 +02:00
|
|
|
voice:
|
|
|
|
Audio file to send. You can either pass a file_id as String to
|
|
|
|
resend an audio that is already on the Telegram servers, or upload
|
|
|
|
a new audio file using multipart/form-data.
|
|
|
|
duration:
|
|
|
|
Duration of sent audio in seconds. [Optional]
|
2016-04-21 13:15:38 +02:00
|
|
|
|
|
|
|
Keyword Args:
|
|
|
|
disable_notification (Optional[bool]): Sends the message silently.
|
|
|
|
iOS users will not receive a notification, Android users will
|
|
|
|
receive a notification with no sound.
|
|
|
|
reply_to_message_id (Optional[int]): If the message is a reply,
|
|
|
|
ID of the original message.
|
|
|
|
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional
|
|
|
|
interface options. A JSON-serialized object for an inline
|
|
|
|
keyboard, custom reply keyboard, instructions to hide reply
|
|
|
|
keyboard or to force a reply from the user.
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
2015-08-17 16:34:42 +02:00
|
|
|
|
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
:class:`telegram.Message`: On success, instance representing the
|
|
|
|
message posted.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2015-08-17 16:34:42 +02:00
|
|
|
"""
|
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/sendVoice'.format(self.base_url)
|
2015-08-17 16:34:42 +02:00
|
|
|
|
|
|
|
data = {'chat_id': chat_id,
|
|
|
|
'voice': voice}
|
|
|
|
|
|
|
|
if duration:
|
|
|
|
data['duration'] = duration
|
|
|
|
|
|
|
|
return url, data
|
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
@log
|
2015-07-12 00:01:02 +02:00
|
|
|
@message
|
2015-07-08 14:37:25 +02:00
|
|
|
def sendLocation(self,
|
|
|
|
chat_id,
|
|
|
|
latitude,
|
|
|
|
longitude,
|
2015-08-28 17:19:30 +02:00
|
|
|
**kwargs):
|
2015-07-08 14:37:25 +02:00
|
|
|
"""Use this method to send point on the map.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id:
|
2015-12-16 15:31:02 +01:00
|
|
|
Unique identifier for the message recipient - Chat id.
|
2015-07-08 14:37:25 +02:00
|
|
|
latitude:
|
|
|
|
Latitude of location.
|
|
|
|
longitude:
|
|
|
|
Longitude of location.
|
2016-04-21 13:15:38 +02:00
|
|
|
|
|
|
|
Keyword Args:
|
|
|
|
disable_notification (Optional[bool]): Sends the message silently.
|
|
|
|
iOS users will not receive a notification, Android users will
|
|
|
|
receive a notification with no sound.
|
|
|
|
reply_to_message_id (Optional[int]): If the message is a reply,
|
|
|
|
ID of the original message.
|
|
|
|
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional
|
|
|
|
interface options. A JSON-serialized object for an inline
|
|
|
|
keyboard, custom reply keyboard, instructions to hide reply
|
|
|
|
keyboard or to force a reply from the user.
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
2015-07-08 21:58:18 +02:00
|
|
|
|
2015-07-08 14:37:25 +02:00
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
:class:`telegram.Message`: On success, instance representing the
|
|
|
|
message posted.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2015-07-08 14:37:25 +02:00
|
|
|
"""
|
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/sendLocation'.format(self.base_url)
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-07-08 14:37:25 +02:00
|
|
|
data = {'chat_id': chat_id,
|
|
|
|
'latitude': latitude,
|
|
|
|
'longitude': longitude}
|
|
|
|
|
2015-07-15 15:05:31 +02:00
|
|
|
return url, data
|
2015-07-08 14:37:25 +02:00
|
|
|
|
2016-04-16 16:48:36 +02:00
|
|
|
@log
|
|
|
|
@message
|
|
|
|
def sendVenue(self,
|
|
|
|
chat_id,
|
|
|
|
latitude,
|
|
|
|
longitude,
|
|
|
|
title,
|
|
|
|
address,
|
|
|
|
foursquare_id=None,
|
|
|
|
**kwargs):
|
|
|
|
"""
|
|
|
|
Use this method to send information about a venue.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id:
|
|
|
|
Unique identifier for the target chat or username of the target
|
|
|
|
channel (in the format @channelusername).
|
|
|
|
latitude:
|
|
|
|
Latitude of the venue.
|
|
|
|
longitude:
|
|
|
|
Longitude of the venue.
|
|
|
|
title:
|
|
|
|
Name of the venue.
|
|
|
|
address:
|
|
|
|
Address of the venue.
|
|
|
|
foursquare_id:
|
|
|
|
Foursquare identifier of the venue.
|
2016-04-21 13:15:38 +02:00
|
|
|
|
|
|
|
Keyword Args:
|
|
|
|
disable_notification (Optional[bool]): Sends the message silently.
|
|
|
|
iOS users will not receive a notification, Android users will
|
|
|
|
receive a notification with no sound.
|
|
|
|
reply_to_message_id (Optional[int]): If the message is a reply,
|
|
|
|
ID of the original message.
|
|
|
|
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional
|
|
|
|
interface options. A JSON-serialized object for an inline
|
|
|
|
keyboard, custom reply keyboard, instructions to hide reply
|
|
|
|
keyboard or to force a reply from the user.
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
2016-04-16 16:48:36 +02:00
|
|
|
|
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
:class:`telegram.Message`: On success, instance representing the
|
|
|
|
message posted.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2016-04-16 16:48:36 +02:00
|
|
|
"""
|
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/sendVenue'.format(self.base_url)
|
2016-04-16 16:48:36 +02:00
|
|
|
|
|
|
|
data = {'chat_id': chat_id,
|
|
|
|
'latitude': latitude,
|
|
|
|
'longitude': longitude,
|
|
|
|
'address': address,
|
|
|
|
'title': title}
|
|
|
|
|
|
|
|
if foursquare_id:
|
|
|
|
data['foursquare_id'] = foursquare_id
|
|
|
|
|
|
|
|
return url, data
|
|
|
|
|
|
|
|
@log
|
|
|
|
@message
|
|
|
|
def sendContact(self,
|
|
|
|
chat_id,
|
|
|
|
phone_number,
|
|
|
|
first_name,
|
|
|
|
last_name=None,
|
|
|
|
**kwargs):
|
|
|
|
"""
|
|
|
|
Use this method to send phone contacts.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id:
|
|
|
|
Unique identifier for the target chat or username of the target
|
|
|
|
channel (in the format @channelusername).
|
|
|
|
phone_number:
|
|
|
|
Contact's phone number.
|
|
|
|
first_name:
|
|
|
|
Contact's first name.
|
|
|
|
last_name:
|
|
|
|
Contact's last name.
|
2016-04-21 13:15:38 +02:00
|
|
|
|
|
|
|
Keyword Args:
|
|
|
|
disable_notification (Optional[bool]): Sends the message silently.
|
|
|
|
iOS users will not receive a notification, Android users will
|
|
|
|
receive a notification with no sound.
|
|
|
|
reply_to_message_id (Optional[int]): If the message is a reply,
|
|
|
|
ID of the original message.
|
|
|
|
reply_markup (Optional[:class:`telegram.ReplyMarkup`]): Additional
|
|
|
|
interface options. A JSON-serialized object for an inline
|
|
|
|
keyboard, custom reply keyboard, instructions to hide reply
|
|
|
|
keyboard or to force a reply from the user.
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
2016-04-16 16:48:36 +02:00
|
|
|
|
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
:class:`telegram.Message`: On success, instance representing the
|
|
|
|
message posted.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2016-04-16 16:48:36 +02:00
|
|
|
"""
|
2016-04-21 13:15:38 +02:00
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/sendContact'.format(self.base_url)
|
2016-04-16 16:48:36 +02:00
|
|
|
|
|
|
|
data = {'chat_id': chat_id,
|
|
|
|
'phone_number': phone_number,
|
|
|
|
'first_name': first_name}
|
|
|
|
|
|
|
|
if last_name:
|
|
|
|
data['last_name'] = last_name
|
|
|
|
|
|
|
|
return url, data
|
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
@log
|
2015-07-12 00:01:02 +02:00
|
|
|
@message
|
2015-07-08 14:55:06 +02:00
|
|
|
def sendChatAction(self,
|
|
|
|
chat_id,
|
2016-04-24 15:06:59 +02:00
|
|
|
action,
|
|
|
|
**kwargs):
|
2015-07-08 14:55:06 +02: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).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id:
|
2015-12-16 15:31:02 +01:00
|
|
|
Unique identifier for the message recipient - Chat id.
|
2015-07-08 14:55:06 +02:00
|
|
|
action:
|
|
|
|
Type of action to broadcast. Choose one, depending on what the user
|
|
|
|
is about to receive:
|
|
|
|
- ChatAction.TYPING for text messages,
|
|
|
|
- ChatAction.UPLOAD_PHOTO for photos,
|
2015-08-25 13:45:02 +02:00
|
|
|
- ChatAction.UPLOAD_VIDEO for videos,
|
|
|
|
- ChatAction.UPLOAD_AUDIO for audio files,
|
2015-08-11 17:41:26 +02:00
|
|
|
- ChatAction.UPLOAD_DOCUMENT for general files,
|
2015-07-08 14:55:06 +02:00
|
|
|
- ChatAction.FIND_LOCATION for location data.
|
|
|
|
"""
|
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/sendChatAction'.format(self.base_url)
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-07-08 14:55:06 +02:00
|
|
|
data = {'chat_id': chat_id,
|
|
|
|
'action': action}
|
|
|
|
|
2015-07-15 15:05:31 +02:00
|
|
|
return url, data
|
2015-07-08 14:55:06 +02:00
|
|
|
|
2016-01-04 17:31:06 +01:00
|
|
|
@log
|
|
|
|
def answerInlineQuery(self,
|
|
|
|
inline_query_id,
|
|
|
|
results,
|
2016-04-19 01:18:32 +02:00
|
|
|
cache_time=300,
|
2016-01-04 17:31:06 +01:00
|
|
|
is_personal=None,
|
2016-04-14 07:34:29 +02:00
|
|
|
next_offset=None,
|
|
|
|
switch_pm_text=None,
|
2016-04-24 15:06:59 +02:00
|
|
|
switch_pm_parameter=None,
|
|
|
|
**kwargs):
|
2016-04-19 14:04:25 +02:00
|
|
|
"""Use this method to send answers to an inline query. No more than
|
|
|
|
50 results per query are allowed.
|
2016-01-04 17:31:06 +01:00
|
|
|
|
|
|
|
Args:
|
2016-04-19 14:04:25 +02:00
|
|
|
inline_query_id (str): Unique identifier for the answered query.
|
|
|
|
results (list[:class:`telegram.InlineQueryResult`]): A list of
|
|
|
|
results for the inline query.
|
|
|
|
cache_time (Optional[int]): The maximum amount of time the
|
|
|
|
result of the inline query may be cached on the server.
|
|
|
|
is_personal (Optional[bool]): Pass `True`, if results may be
|
|
|
|
cached on the server side only for the user that sent the
|
|
|
|
query. By default, results may be returned to any user who
|
|
|
|
sends the same query.
|
|
|
|
next_offset (Optional[str]): Pass the offset that a client
|
|
|
|
should send in the next query with the same text to receive
|
|
|
|
more results. Pass an empty string if there are no more
|
|
|
|
results or if you don't support pagination. Offset length
|
|
|
|
can't exceed 64 bytes.
|
|
|
|
switch_pm_text (Optional[str]): 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.
|
|
|
|
switch_pm_parameter (Optional[str]): Parameter for the start
|
|
|
|
message sent to the bot when user presses the switch button.
|
2016-01-04 17:31:06 +01:00
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
Keyword Args:
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
Returns:
|
|
|
|
bool: On success, `True` is returned.
|
2016-04-14 07:34:29 +02:00
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
2016-01-04 17:31:06 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/answerInlineQuery'.format(self.base_url)
|
2016-01-04 17:31:06 +01:00
|
|
|
|
|
|
|
results = [res.to_dict() for res in results]
|
|
|
|
|
|
|
|
data = {'inline_query_id': inline_query_id,
|
|
|
|
'results': results}
|
|
|
|
|
2016-04-19 00:08:47 +02:00
|
|
|
if cache_time or cache_time == 0:
|
2016-04-21 13:15:38 +02:00
|
|
|
data['cache_time'] = cache_time
|
2016-04-14 07:38:51 +02:00
|
|
|
if is_personal:
|
2016-04-21 13:15:38 +02:00
|
|
|
data['is_personal'] = is_personal
|
2016-04-25 16:26:36 +02:00
|
|
|
if next_offset is not None:
|
2016-01-04 17:31:06 +01:00
|
|
|
data['next_offset'] = next_offset
|
2016-04-14 07:34:29 +02:00
|
|
|
if switch_pm_text:
|
|
|
|
data['switch_pm_text'] = switch_pm_text
|
|
|
|
if switch_pm_parameter:
|
|
|
|
data['switch_pm_parameter'] = switch_pm_parameter
|
2016-01-04 17:31:06 +01:00
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
result = request.post(url, data,
|
2016-04-27 23:31:36 +02:00
|
|
|
timeout=kwargs.get('timeout'))
|
2016-01-04 17:31:06 +01:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
@log
|
2015-07-08 15:10:08 +02:00
|
|
|
def getUserProfilePhotos(self,
|
|
|
|
user_id,
|
|
|
|
offset=None,
|
2016-04-24 15:06:59 +02:00
|
|
|
limit=100,
|
|
|
|
**kwargs):
|
2015-07-08 15:14:07 +02:00
|
|
|
"""Use this method to get a list of profile pictures for a user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id:
|
|
|
|
Unique identifier of the target user.
|
|
|
|
offset:
|
|
|
|
Sequential number of the first photo to be returned. By default,
|
|
|
|
all photos are returned. [Optional]
|
|
|
|
limit:
|
2015-08-10 18:57:31 +02:00
|
|
|
Limits the number of photos to be retrieved. Values between 1-100
|
2015-07-08 15:14:07 +02:00
|
|
|
are accepted. Defaults to 100. [Optional]
|
2015-07-08 21:58:18 +02:00
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
Keyword Args:
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
|
|
|
|
2015-07-08 15:14:07 +02:00
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
list[:class:`telegram.UserProfilePhotos`]: A list of
|
|
|
|
:class:`telegram.UserProfilePhotos` objects are returned.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2015-07-08 15:14:07 +02:00
|
|
|
"""
|
2015-07-08 15:10:08 +02:00
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/getUserProfilePhotos'.format(self.base_url)
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-07-08 15:10:08 +02:00
|
|
|
data = {'user_id': user_id}
|
|
|
|
|
|
|
|
if offset:
|
|
|
|
data['offset'] = offset
|
|
|
|
if limit:
|
|
|
|
data['limit'] = limit
|
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
result = request.post(url, data,
|
2016-04-27 23:31:36 +02:00
|
|
|
timeout=kwargs.get('timeout'))
|
2015-07-08 15:10:08 +02:00
|
|
|
|
2015-09-05 16:55:55 +02:00
|
|
|
return UserProfilePhotos.de_json(result)
|
2015-07-08 15:10:08 +02:00
|
|
|
|
2015-09-20 17:28:10 +02:00
|
|
|
@log
|
|
|
|
def getFile(self,
|
2016-04-24 15:06:59 +02:00
|
|
|
file_id,
|
|
|
|
**kwargs):
|
2015-09-20 17:28:10 +02:00
|
|
|
"""Use this method to get basic info about a file and prepare it for
|
|
|
|
downloading. For the moment, bots can download files of up to 20MB in
|
|
|
|
size.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
file_id:
|
|
|
|
File identifier to get info about.
|
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
Keyword Args:
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
|
|
|
|
2015-09-20 17:28:10 +02:00
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
:class:`telegram.File`: On success, a :class:`telegram.File`
|
|
|
|
object is returned.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2015-09-20 17:28:10 +02:00
|
|
|
"""
|
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/getFile'.format(self.base_url)
|
2015-09-20 17:28:10 +02:00
|
|
|
|
|
|
|
data = {'file_id': file_id}
|
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
result = request.post(url, data,
|
2016-04-27 23:31:36 +02:00
|
|
|
timeout=kwargs.get('timeout'))
|
2015-09-20 17:28:10 +02:00
|
|
|
|
|
|
|
if result.get('file_path'):
|
|
|
|
result['file_path'] = '%s/%s' % (self.base_file_url,
|
|
|
|
result['file_path'])
|
|
|
|
|
|
|
|
return File.de_json(result)
|
|
|
|
|
2016-04-12 05:46:50 +02:00
|
|
|
@log
|
|
|
|
def kickChatMember(self,
|
|
|
|
chat_id,
|
2016-04-24 15:06:59 +02:00
|
|
|
user_id,
|
|
|
|
**kwargs):
|
2016-04-12 05:46:50 +02:00
|
|
|
"""Use this method to kick a user from a group or a supergroup. In the
|
|
|
|
case of supergroups, the user will not be able to return to the group
|
|
|
|
on their own using invite links, etc., unless unbanned first. The bot
|
|
|
|
must be an administrator in the group for this to work.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id:
|
|
|
|
Unique identifier for the target group or username of the target
|
|
|
|
supergroup (in the format @supergroupusername).
|
|
|
|
user_id:
|
|
|
|
Unique identifier of the target user.
|
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
Keyword Args:
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
|
|
|
|
2016-04-12 05:46:50 +02:00
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
bool: On success, `True` is returned.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2016-04-12 05:46:50 +02:00
|
|
|
"""
|
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/kickChatMember'.format(self.base_url)
|
2016-04-12 05:46:50 +02:00
|
|
|
|
|
|
|
data = {'chat_id': chat_id,
|
|
|
|
'user_id': user_id}
|
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
result = request.post(url, data,
|
2016-04-27 23:31:36 +02:00
|
|
|
timeout=kwargs.get('timeout'))
|
2016-04-12 05:46:50 +02:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
@log
|
|
|
|
def unbanChatMember(self,
|
|
|
|
chat_id,
|
2016-04-24 15:06:59 +02:00
|
|
|
user_id,
|
|
|
|
**kwargs):
|
2016-04-12 05:46:50 +02:00
|
|
|
"""Use this method to unban a previously kicked user in a supergroup.
|
|
|
|
The user will not return to the group automatically, but will be able
|
|
|
|
to join via link, etc. The bot must be an administrator in the group
|
|
|
|
for this to work.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id:
|
|
|
|
Unique identifier for the target group or username of the target
|
|
|
|
supergroup (in the format @supergroupusername).
|
|
|
|
user_id:
|
|
|
|
Unique identifier of the target user.
|
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
Keyword Args:
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
|
|
|
|
2016-04-12 05:46:50 +02:00
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
bool: On success, `True` is returned.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2016-04-12 05:46:50 +02:00
|
|
|
"""
|
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/unbanChatMember'.format(self.base_url)
|
2016-04-12 05:46:50 +02:00
|
|
|
|
|
|
|
data = {'chat_id': chat_id,
|
|
|
|
'user_id': user_id}
|
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
result = request.post(url, data,
|
2016-04-27 23:31:36 +02:00
|
|
|
timeout=kwargs.get('timeout'))
|
2016-04-12 05:46:50 +02:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2016-04-14 02:25:26 +02:00
|
|
|
@log
|
|
|
|
def answerCallbackQuery(self,
|
|
|
|
callback_query_id,
|
|
|
|
text=None,
|
2016-04-24 15:06:59 +02:00
|
|
|
show_alert=False,
|
|
|
|
**kwargs):
|
2016-04-19 14:04:25 +02: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.
|
2016-04-14 02:25:26 +02:00
|
|
|
|
|
|
|
Args:
|
2016-04-19 14:04:25 +02:00
|
|
|
callback_query_id (str): Unique identifier for the query to be
|
|
|
|
answered.
|
|
|
|
text (Optional[str]): Text of the notification. If not
|
|
|
|
specified, nothing will be shown to the user.
|
|
|
|
show_alert (Optional[bool]): If `True`, an alert will be shown
|
|
|
|
by the client instead of a notification at the top of the chat
|
|
|
|
screen. Defaults to `False`.
|
2016-04-14 02:25:26 +02:00
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
Keyword Args:
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
|
|
|
network_delay (Optional[float]): If using the timeout (which is
|
|
|
|
a `timeout` for the Telegram servers operation),
|
|
|
|
then `network_delay` as an extra delay (in seconds) to
|
|
|
|
compensate for network latency. Defaults to 2.
|
|
|
|
|
2016-04-14 02:25:26 +02:00
|
|
|
Returns:
|
2016-04-19 14:04:25 +02:00
|
|
|
bool: On success, `True` is returned.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2016-04-14 02:25:26 +02:00
|
|
|
"""
|
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/answerCallbackQuery'.format(self.base_url)
|
2016-04-14 02:25:26 +02:00
|
|
|
|
|
|
|
data = {'callback_query_id': callback_query_id}
|
|
|
|
|
|
|
|
if text:
|
|
|
|
data['text'] = text
|
|
|
|
if show_alert:
|
|
|
|
data['show_alert'] = show_alert
|
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
result = request.post(url, data,
|
2016-04-27 23:31:36 +02:00
|
|
|
timeout=kwargs.get('timeout'))
|
2016-04-14 02:25:26 +02:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2016-04-14 05:28:06 +02:00
|
|
|
@log
|
|
|
|
def editMessageText(self,
|
|
|
|
text,
|
|
|
|
chat_id=None,
|
|
|
|
message_id=None,
|
|
|
|
inline_message_id=None,
|
|
|
|
parse_mode=None,
|
|
|
|
disable_web_page_preview=None,
|
2016-04-24 15:06:59 +02:00
|
|
|
reply_markup=None,
|
|
|
|
**kwargs):
|
2016-04-14 05:28:06 +02:00
|
|
|
"""Use this method to edit text messages sent by the bot or via the bot
|
|
|
|
(for inline bots).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
text:
|
|
|
|
New text of the message.
|
|
|
|
chat_id:
|
|
|
|
Required if inline_message_id is not specified. Unique identifier
|
|
|
|
for the target chat or username of the target channel (in the
|
|
|
|
format @channelusername).
|
|
|
|
message_id:
|
|
|
|
Required if inline_message_id is not specified. Unique identifier
|
|
|
|
of the sent message.
|
|
|
|
inline_message_id:
|
|
|
|
Required if chat_id and message_id are not specified. Identifier of
|
|
|
|
the inline message.
|
|
|
|
parse_mode:
|
|
|
|
Send Markdown or HTML, if you want Telegram apps to show bold,
|
|
|
|
italic, fixed-width text or inline URLs in your bot's message.
|
|
|
|
disable_web_page_preview:
|
|
|
|
Disables link previews for links in this message.
|
|
|
|
reply_markup:
|
|
|
|
A JSON-serialized object for an inline keyboard.
|
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
Keyword Args:
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
|
|
|
|
2016-04-14 05:28:06 +02:00
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
:class:`telegram.Message`: On success, if edited message is sent by
|
|
|
|
the bot, the edited message is returned, otherwise `True` is
|
|
|
|
returned.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2016-04-14 05:28:06 +02:00
|
|
|
"""
|
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/editMessageText'.format(self.base_url)
|
2016-04-14 05:28:06 +02:00
|
|
|
|
2016-04-16 20:57:50 +02:00
|
|
|
data = {'text': text}
|
2016-04-14 05:28:06 +02:00
|
|
|
|
|
|
|
if chat_id:
|
|
|
|
data['chat_id'] = chat_id
|
|
|
|
if message_id:
|
|
|
|
data['message_id'] = message_id
|
|
|
|
if inline_message_id:
|
|
|
|
data['inline_message_id'] = inline_message_id
|
2016-04-19 14:04:25 +02:00
|
|
|
if parse_mode:
|
2016-04-16 20:57:50 +02:00
|
|
|
data['parse_mode'] = parse_mode
|
2016-04-19 14:04:25 +02:00
|
|
|
if disable_web_page_preview:
|
2016-04-16 20:57:50 +02:00
|
|
|
data['disable_web_page_preview'] = disable_web_page_preview
|
|
|
|
if reply_markup:
|
2016-04-17 22:11:29 +02:00
|
|
|
if isinstance(reply_markup, ReplyMarkup):
|
|
|
|
data['reply_markup'] = reply_markup.to_json()
|
|
|
|
else:
|
|
|
|
data['reply_markup'] = reply_markup
|
2016-04-14 05:28:06 +02:00
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
result = request.post(url, data,
|
2016-04-27 23:31:36 +02:00
|
|
|
timeout=kwargs.get('timeout'))
|
2016-04-14 05:28:06 +02:00
|
|
|
|
|
|
|
return Message.de_json(result)
|
|
|
|
|
|
|
|
@log
|
2016-04-19 14:04:25 +02:00
|
|
|
@message
|
2016-04-14 05:28:06 +02:00
|
|
|
def editMessageCaption(self,
|
|
|
|
chat_id=None,
|
|
|
|
message_id=None,
|
|
|
|
inline_message_id=None,
|
2016-04-19 14:04:25 +02:00
|
|
|
caption=None,
|
|
|
|
**kwargs):
|
|
|
|
"""Use this method to edit captions of messages sent by the bot or
|
|
|
|
via the bot (for inline bots).
|
2016-04-14 05:28:06 +02:00
|
|
|
|
|
|
|
Args:
|
2016-04-19 14:04:25 +02:00
|
|
|
chat_id (Optional[str]): Required if inline_message_id is not
|
|
|
|
specified. Unique identifier for the target chat or username of
|
|
|
|
the target channel (in the format @channelusername).
|
|
|
|
message_id (Optional[str]): Required if inline_message_id is not
|
|
|
|
specified. Unique identifier of the sent message.
|
|
|
|
inline_message_id (Optional[str]): Required if chat_id and
|
|
|
|
message_id are not specified. Identifier of the inline message.
|
|
|
|
caption (Optional[str]): New caption of the message.
|
|
|
|
**kwargs (Optional[dict]): Arbitrary keyword arguments.
|
|
|
|
|
|
|
|
Keyword Args:
|
|
|
|
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
|
|
|
|
A JSON-serialized object for an inline keyboard.
|
2016-04-21 13:15:38 +02:00
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
2016-04-14 05:28:06 +02:00
|
|
|
|
|
|
|
Returns:
|
2016-04-19 14:04:25 +02:00
|
|
|
:class:`telegram.Message`: On success, if edited message is sent by
|
2016-04-21 13:15:38 +02:00
|
|
|
the bot, the edited message is returned, otherwise `True` is
|
2016-04-19 14:04:25 +02:00
|
|
|
returned.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2016-04-14 05:28:06 +02:00
|
|
|
"""
|
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/editMessageCaption'.format(self.base_url)
|
2016-04-14 05:28:06 +02:00
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
data = {}
|
2016-04-14 05:28:06 +02:00
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
if caption:
|
|
|
|
data['caption'] = caption
|
2016-04-14 05:28:06 +02:00
|
|
|
if chat_id:
|
|
|
|
data['chat_id'] = chat_id
|
|
|
|
if message_id:
|
|
|
|
data['message_id'] = message_id
|
|
|
|
if inline_message_id:
|
|
|
|
data['inline_message_id'] = inline_message_id
|
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
return url, data
|
2016-04-14 05:28:06 +02:00
|
|
|
|
|
|
|
@log
|
2016-04-19 14:04:25 +02:00
|
|
|
@message
|
2016-04-14 05:28:06 +02:00
|
|
|
def editMessageReplyMarkup(self,
|
|
|
|
chat_id=None,
|
|
|
|
message_id=None,
|
2016-04-19 14:04:25 +02:00
|
|
|
inline_message_id=None,
|
|
|
|
**kwargs):
|
2016-04-14 05:28:06 +02:00
|
|
|
"""Use this method to edit only the reply markup of messages sent by
|
|
|
|
the bot or via the bot (for inline bots).
|
|
|
|
|
|
|
|
Args:
|
2016-04-19 14:04:25 +02:00
|
|
|
chat_id (Optional[str]): Required if inline_message_id is not
|
|
|
|
specified. Unique identifier for the target chat or username of
|
|
|
|
the target channel (in the format @channelusername).
|
|
|
|
message_id (Optional[str]): Required if inline_message_id is not
|
|
|
|
specified. Unique identifier of the sent message.
|
|
|
|
inline_message_id (Optional[str]): Required if chat_id and
|
|
|
|
message_id are not specified. Identifier of the inline message.
|
|
|
|
**kwargs (Optional[dict]): Arbitrary keyword arguments.
|
|
|
|
|
|
|
|
Keyword Args:
|
|
|
|
reply_markup (Optional[:class:`telegram.InlineKeyboardMarkup`]):
|
|
|
|
A JSON-serialized object for an inline keyboard.
|
2016-04-21 13:15:38 +02:00
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
2016-04-14 05:28:06 +02:00
|
|
|
|
|
|
|
Returns:
|
2016-04-19 14:04:25 +02:00
|
|
|
:class:`telegram.Message`: On success, if edited message is sent by
|
|
|
|
the bot, the edited message is returned, otherwise `True` is
|
|
|
|
returned.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2016-04-14 05:28:06 +02:00
|
|
|
"""
|
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/editMessageReplyMarkup'.format(self.base_url)
|
2016-04-14 05:28:06 +02:00
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
data = {}
|
2016-04-14 05:28:06 +02:00
|
|
|
|
|
|
|
if chat_id:
|
|
|
|
data['chat_id'] = chat_id
|
|
|
|
if message_id:
|
|
|
|
data['message_id'] = message_id
|
|
|
|
if inline_message_id:
|
|
|
|
data['inline_message_id'] = inline_message_id
|
|
|
|
|
2016-04-19 14:04:25 +02:00
|
|
|
return url, data
|
2016-04-14 05:28:06 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
@log
|
2015-07-07 21:50:36 +02:00
|
|
|
def getUpdates(self,
|
|
|
|
offset=None,
|
|
|
|
limit=100,
|
2016-04-27 23:31:36 +02:00
|
|
|
timeout=0,
|
|
|
|
network_delay=.2):
|
2015-07-08 14:22:31 +02:00
|
|
|
"""Use this method to receive incoming updates using long polling.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
offset:
|
|
|
|
Identifier of the first update to be returned. Must be greater by
|
|
|
|
one than the highest among the identifiers of previously received
|
|
|
|
updates. By default, updates starting with the earliest unconfirmed
|
|
|
|
update are returned. An update is considered confirmed as soon as
|
|
|
|
getUpdates is called with an offset higher than its update_id.
|
|
|
|
limit:
|
2015-08-10 18:57:31 +02:00
|
|
|
Limits the number of updates to be retrieved. Values between 1-100
|
2015-07-08 14:22:31 +02:00
|
|
|
are accepted. Defaults to 100.
|
2016-04-27 23:31:36 +02:00
|
|
|
timeout:
|
|
|
|
Timeout in seconds for long polling. Defaults to 0, i.e. usual
|
|
|
|
short polling.
|
|
|
|
network_delay:
|
|
|
|
Additional timeout in seconds to allow the response from Telegram
|
|
|
|
to take some time when using long polling. Defaults to 2, which
|
|
|
|
should be enough for most connections. Increase it if it takes very
|
|
|
|
long for data to be transmitted from and to the Telegram servers.
|
2015-07-08 21:58:18 +02:00
|
|
|
|
2015-07-08 14:22:31 +02:00
|
|
|
Returns:
|
2016-05-01 14:25:07 +02:00
|
|
|
list[:class:`telegram.Update`]: A list of :class:`telegram.Update`
|
2016-04-21 13:15:38 +02:00
|
|
|
objects are returned.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2015-07-08 14:22:31 +02:00
|
|
|
"""
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/getUpdates'.format(self.base_url)
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2016-04-27 23:31:36 +02:00
|
|
|
data = {'timeout': timeout}
|
2016-04-19 14:04:25 +02:00
|
|
|
|
2015-07-07 21:50:36 +02:00
|
|
|
if offset:
|
|
|
|
data['offset'] = offset
|
|
|
|
if limit:
|
|
|
|
data['limit'] = limit
|
|
|
|
|
2016-04-27 23:31:36 +02:00
|
|
|
urlopen_timeout = timeout + network_delay
|
|
|
|
|
|
|
|
result = request.post(url, data, timeout=urlopen_timeout)
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-09-05 16:55:55 +02:00
|
|
|
if result:
|
2016-03-15 02:56:20 +01:00
|
|
|
self.logger.debug(
|
2015-09-05 16:55:55 +02:00
|
|
|
'Getting updates: %s', [u['update_id'] for u in result])
|
2015-07-20 13:59:41 +02:00
|
|
|
else:
|
2016-01-31 10:32:34 +01:00
|
|
|
self.logger.debug('No new updates found.')
|
2015-07-20 13:59:41 +02:00
|
|
|
|
2015-09-05 16:55:55 +02:00
|
|
|
return [Update.de_json(x) for x in result]
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
@log
|
2015-07-12 00:01:02 +02:00
|
|
|
def setWebhook(self,
|
2015-09-04 22:53:39 +02:00
|
|
|
webhook_url=None,
|
2016-04-24 15:06:59 +02:00
|
|
|
certificate=None,
|
|
|
|
**kwargs):
|
2015-07-09 18:19:58 +02:00
|
|
|
"""Use this method to specify a url and receive incoming updates via an
|
|
|
|
outgoing webhook. Whenever there is an update for the bot, we will send
|
|
|
|
an HTTPS POST request to the specified url, containing a
|
|
|
|
JSON-serialized Update. In case of an unsuccessful request, we will
|
|
|
|
give up after a reasonable amount of attempts.
|
|
|
|
|
|
|
|
Args:
|
2016-04-24 15:06:59 +02:00
|
|
|
webhook_url:
|
2015-07-09 18:19:58 +02:00
|
|
|
HTTPS url to send updates to.
|
|
|
|
Use an empty string to remove webhook integration
|
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
Keyword Args:
|
|
|
|
timeout (Optional[float]): If this value is specified, use it as
|
|
|
|
the definitive timeout (in seconds) for urlopen() operations.
|
|
|
|
|
2015-07-09 18:19:58 +02:00
|
|
|
Returns:
|
2016-04-21 13:15:38 +02:00
|
|
|
bool: On success, `True` is returned.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:class:`telegram.TelegramError`
|
|
|
|
|
2015-07-09 18:19:58 +02:00
|
|
|
"""
|
2016-04-21 13:15:38 +02:00
|
|
|
|
2016-04-21 17:15:37 +02:00
|
|
|
url = '{0}/setWebhook'.format(self.base_url)
|
2015-07-07 21:50:36 +02:00
|
|
|
|
2015-09-04 22:53:39 +02:00
|
|
|
data = {}
|
2016-04-19 14:04:25 +02:00
|
|
|
|
2016-04-25 16:26:36 +02:00
|
|
|
if webhook_url is not None:
|
2015-09-04 22:53:39 +02:00
|
|
|
data['url'] = webhook_url
|
|
|
|
if certificate:
|
|
|
|
data['certificate'] = certificate
|
2015-07-09 18:19:58 +02:00
|
|
|
|
2016-04-24 15:06:59 +02:00
|
|
|
result = request.post(url, data,
|
2016-04-27 23:31:36 +02:00
|
|
|
timeout=kwargs.get('timeout'))
|
2015-07-09 18:19:58 +02:00
|
|
|
|
2015-09-05 16:55:55 +02:00
|
|
|
return result
|
2015-07-20 12:53:58 +02:00
|
|
|
|
2015-08-28 17:19:30 +02:00
|
|
|
@staticmethod
|
|
|
|
def de_json(data):
|
2016-04-21 13:15:38 +02:00
|
|
|
data = super(Bot, Bot).de_json(data)
|
|
|
|
|
|
|
|
return Bot(**data)
|
2015-08-28 17:19:30 +02:00
|
|
|
|
2015-07-20 13:36:08 +02:00
|
|
|
def to_dict(self):
|
2015-07-20 12:53:58 +02:00
|
|
|
data = {'id': self.id,
|
|
|
|
'username': self.username,
|
|
|
|
'first_name': self.username}
|
2016-04-21 13:15:38 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
if self.last_name:
|
|
|
|
data['last_name'] = self.last_name
|
2016-04-21 13:15:38 +02:00
|
|
|
|
2015-07-20 12:53:58 +02:00
|
|
|
return data
|
2015-08-11 17:41:26 +02:00
|
|
|
|
|
|
|
def __reduce__(self):
|
2015-08-12 04:46:09 +02:00
|
|
|
return (self.__class__, (self.token,
|
2016-04-21 14:21:12 +02:00
|
|
|
self.base_url.replace(self.token, ''),
|
|
|
|
self.base_file_url.replace(self.token, '')))
|