Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c4c17e8

Browse files
committed
Merge branch 'testing'
2 parents 6893da5 + 3024c1c commit c4c17e8

15 files changed

+871
-34
lines changed

CHANGES.rst

-19
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
2015-09-10
2-
Released 2.8.3
3-
Moved Bot._requestURL to its own class (telegram.utils.request)
4-
Much better, such wow, Telegram Objects tests
5-
Add consistency for str properties on Telegram Objects
6-
Better design to test if chat_id is invalid
7-
Add ability to set custom filename on Bot.sendDocument(..,filename='')
8-
Fix Sticker as InputFile
9-
Send JSON requests over urlencoded post data
10-
Markdown support for Bot.sendMessage(..., parse_mode=ParseMode.MARKDOWN)
11-
Refactor of TelegramError class (no more handling IOError or URLError)
12-
13-
14-
2015-09-05
15-
Released 2.8.2
16-
Fix regression on Telegram ReplyMarkup
17-
Add certificate to is_inputfile method
18-
19-
201
2015-09-05
212
Released 2.8.1
223
Fix regression on Telegram objects with thumb properties

README.rst

+9-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ sendLocation Yes
9494
sendChatAction Yes
9595
getUpdates Yes
9696
getUserProfilePhotos Yes
97+
getFile Yes
9798
setWebhook Yes
9899
========================= ============
99100

@@ -220,6 +221,12 @@ To hide `Custom Keyboards <https://core.telegram.org/bots#keyboards>`_::
220221
>>> reply_markup = telegram.ReplyKeyboardHide()
221222
>>> bot.sendMessage(chat_id=chat_id, text="I'm back.", reply_markup=reply_markup)
222223

224+
To download a file (you will need its file_id)::
225+
226+
>>> file_id = message.voice.file_id
227+
>>> newFile = bot.getFile(file_id)
228+
>>> newFile.download('voice.ogg')
229+
223230
There are many more API methods, to read the full API documentation::
224231

225232
$ pydoc telegram.Bot
@@ -268,9 +275,9 @@ You may copy, distribute and modify the software provided that modifications are
268275
_`Contact`
269276
==========
270277

271-
Feel free to join to our `Telegram group <https://telegram.me/joinchat/00b9c0f802509b946b2e8e98b73e19be>`_.
278+
Feel free to join to our `Telegram group <https://telegram.me/joinchat/00b9c0f802509b94d52953d3fa1ec504>`_.
272279

273-
If you face trouble joining in the group please ping me `via Telegram <https://telegram.me/leandrotoledo>`_, I'll be glad to add you.
280+
*If you face trouble joining in the group please ping me on Telegram (@leandrotoledo), I'll be glad to add you.*
274281

275282
=======
276283
_`TODO`

docs/source/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
# The short X.Y version.
6161
version = '2.8'
6262
# The full version, including alpha/beta/rc tags.
63-
release = '2.8.3'
63+
release = '2.8.1'
6464

6565
# The language for content autogenerated by Sphinx. Refer to documentation
6666
# for a list of supported languages.

examples/command_handler_example.py

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from telegram import CommandHandlerWithHelpAndFather, CommandHandler
2+
class ExampleCommandHandler(CommandHandlerWithHelpAndFather):
3+
"""This is an example how to use a CommandHandlerWithHelp or just a CommandHandler.
4+
5+
If You want to use a CommandHandler it is very easy.
6+
create a class which inherits a CommandHandler.
7+
create a method in this class which start with 'command_' and takes 1 argument: 'update' (which comes directly from
8+
getUpdate()).
9+
If you inherit CommandHandlerWithHelp it also creates a nice /help for you.
10+
"""
11+
def __init__(self, bot): # only necessary for a WithHelp
12+
super(ExampleCommandHandler, self).__init__(bot)
13+
self._help_title = 'Welcome this is a help file!' # optional
14+
self._help_before_list = """
15+
Yeah here I explain some things about this bot.
16+
and of course I can do this in Multiple lines.
17+
""" # default is empty
18+
self._help_list_title = ' These are the available commands:' # optional
19+
self._help_after_list = ' These are some footnotes' # default is empty
20+
self.is_reply = True # default is True
21+
22+
# only necessary if you want to override to default
23+
def _command_not_found(self, update):
24+
"""Inform the telegram user that the command was not found."""
25+
chat_id = update.message.chat.id
26+
reply_to = update.message.message_id
27+
message = "Sorry, I don't know how to do {command}.".format(command=update.message.text.split(' ')[0])
28+
self.bot.sendMessage(chat_id=chat_id, text=message, reply_to_message_id=reply_to)
29+
30+
# creates /test command. This code gets called when a telegram user enters /test
31+
def command_test(self, update):
32+
""" Test if the server is online. """
33+
chat_id = update.message.chat.id
34+
reply_to = update.message.message_id
35+
message = 'Yeah, the server is online!'
36+
self.bot.sendMessage(chat_id=chat_id, text=message, reply_to_message_id=reply_to)
37+
38+
# creates /parrot command
39+
def command_parrot(self, update):
40+
""" Says back what you say after the command"""
41+
chat_id = update.message.chat.id
42+
reply_to = update.message.message_id
43+
send = update.message.text.split(' ')
44+
message = update.message.text[len(send[0]):]
45+
if len(send) == 1:
46+
message = '...'
47+
self.bot.sendMessage(chat_id=chat_id, text=message, reply_to_message_id=reply_to)
48+
49+
# creates /p command
50+
def command_p(self, update):
51+
"""Does the same as parrot."""
52+
return self.command_parrot(update)
53+
54+
# this doesn't create a command.
55+
def another_test(self, update):
56+
""" This won't be called by the CommandHandler.
57+
58+
This is an example of a function that isn't a command in telegram.
59+
Because it didn't start with 'command_'.
60+
"""
61+
chat_id = update.message.chat.id
62+
reply_to = update.message.message_id
63+
message = 'Yeah, this is another test'
64+
self.bot.sendMessage(chat_id=chat_id, text=message, reply_to_message_id=reply_to)
65+
66+
67+
class Exampe2CommandHandler(CommandHandler):
68+
"""
69+
This is an example of a small working CommandHandler with only one command.
70+
"""
71+
def command_test(self, update):
72+
""" Test if the server is online. """
73+
chat_id = update.message.chat.id
74+
reply_to = update.message.message_id
75+
message = 'Yeah, the server is online!'
76+
self.bot.sendMessage(chat_id=chat_id, text=message, reply_to_message_id=reply_to)
77+
78+
if __name__ == '__main__':
79+
import telegram as telegram
80+
try:
81+
from mytoken import token
82+
except:
83+
token = '' # use your own token here
84+
print ('token = ', token)
85+
Bot = telegram.Bot(token=token)
86+
test_command_handler = ExampleCommandHandler(Bot)
87+
test_command_handler.run()

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def read(*paths):
1515

1616
setup(
1717
name='python-telegram-bot',
18-
version='2.8.3',
18+
version='2.8.1',
1919
author='Leandro Toledo',
2020
author_email='[email protected]',
2121
license='LGPLv3',

telegram/__init__.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"""A library that provides a Python interface to the Telegram Bot API"""
2020

2121
__author__ = '[email protected]'
22-
__version__ = '2.8.3'
22+
__version__ = '2.8.1'
2323

2424
from .base import TelegramObject
2525
from .user import User
@@ -40,6 +40,7 @@
4040
from .forcereply import ForceReply
4141
from .error import TelegramError
4242
from .inputfile import InputFile
43+
from .file import File
4344
from .nullhandler import NullHandler
4445
from .emoji import Emoji
4546
from .parsemode import ParseMode
@@ -50,6 +51,6 @@
5051
__all__ = ['Bot', 'Emoji', 'TelegramError', 'InputFile', 'ReplyMarkup',
5152
'ForceReply', 'ReplyKeyboardHide', 'ReplyKeyboardMarkup',
5253
'UserProfilePhotos', 'ChatAction', 'Location', 'Contact',
53-
'Video', 'Sticker', 'Document', 'Audio', 'PhotoSize', 'GroupChat',
54-
'Update', 'ParseMode', 'Message', 'User', 'TelegramObject',
55-
'NullHandler', 'Voice']
54+
'Video', 'Sticker', 'Document', 'File', 'Audio', 'PhotoSize',
55+
'GroupChat', 'Update', 'ParseMode', 'Message', 'User',
56+
'TelegramObject', 'NullHandler', 'Voice']

telegram/bot.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@
2222
import functools
2323
import logging
2424

25-
from telegram import (User, Message, Update, UserProfilePhotos, TelegramError,
26-
ReplyMarkup, TelegramObject, NullHandler)
25+
from telegram import (User, Message, Update, UserProfilePhotos, File,
26+
TelegramError, ReplyMarkup, TelegramObject, NullHandler)
2727
from telegram.utils import request
2828

2929
H = NullHandler()
3030
logging.getLogger(__name__).addHandler(H)
3131

3232

3333
class Bot(TelegramObject):
34+
3435
"""This object represents a Telegram Bot.
3536
3637
Attributes:
@@ -58,6 +59,8 @@ def __init__(self,
5859
else:
5960
self.base_url = base_url + self.token
6061

62+
self.base_file_url = 'https://api.telegram.org/file/bot%s' % self.token
63+
6164
self.bot = None
6265

6366
self.logger = logging.getLogger(__name__)
@@ -615,6 +618,33 @@ def getUserProfilePhotos(self,
615618

616619
return UserProfilePhotos.de_json(result)
617620

621+
@log
622+
def getFile(self,
623+
file_id):
624+
"""Use this method to get basic info about a file and prepare it for
625+
downloading. For the moment, bots can download files of up to 20MB in
626+
size.
627+
628+
Args:
629+
file_id:
630+
File identifier to get info about.
631+
632+
Returns:
633+
Returns a telegram.File object
634+
"""
635+
636+
url = '%s/getFile' % self.base_url
637+
638+
data = {'file_id': file_id}
639+
640+
result = request.post(url, data)
641+
642+
if result.get('file_path'):
643+
result['file_path'] = '%s/%s' % (self.base_file_url,
644+
result['file_path'])
645+
646+
return File.de_json(result)
647+
618648
@log
619649
def getUpdates(self,
620650
offset=None,

0 commit comments

Comments
 (0)