Added File & getFile, including testing

This commit is contained in:
pieter 2015-09-18 20:31:29 +02:00
parent 07c28830db
commit fd1f16598b
5 changed files with 45 additions and 0 deletions

View File

@ -251,6 +251,16 @@ tb.send_location(chat_id, lat, lon)
# action_string can be one of the following strings: 'typing', 'upload_photo', 'record_video', 'upload_video',
# 'record_audio', 'upload_audio', 'upload_document' or 'find_location'.
tb.send_chat_action(chat_id, action_string)
# getFile
# Downloading a file is straightforward
# Returns a File object
import requests
file_info = tb.get_file(file_id)
file = requests.get('https://api.telegram.org/file/bot{0}/{1}'.format(API_TOKEN, file_info.file_path))
```
#### Reply markup
All `send_xyz` functions of TeleBot take an optional `reply_markup` argument. This argument must be an instance of `ReplyKeyboardMarkup`, `ReplyKeyboardHide` or `ForceReply`, which are defined in types.py.

View File

@ -169,6 +169,9 @@ class TeleBot:
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))
def get_user_profile_photos(self, user_id, offset=None, limit=None):
"""
Retrieves the user profile photos of the person with 'user_id'

View File

@ -61,6 +61,10 @@ def get_me(token):
method_url = 'getMe'
return _make_request(token, method_url)
def get_file(token, file_id):
method_url = 'getFile'
return _make_request(token, method_url, params={'file_id': file_id})
def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None,
parse_mode=None):

View File

@ -408,6 +408,26 @@ class UserProfilePhotos(JsonDeserializable):
self.total_count = total_count
self.photos = photos
class File(JsonDeserializable):
@classmethod
def de_json(cls, json_type):
obj = cls.check_json(json_type)
file_id = obj['file_id']
file_size = None
file_path = None
if 'file_size' in obj:
file_size = obj['file_size']
if 'file_path' in obj:
file_path = obj['file_path']
return File(file_id, file_size, file_path)
def __init__(self, file_id, file_size, file_path):
self.file_id = file_id
self.file_size = file_size
self.file_path = file_path
class ForceReply(JsonSerializable):
def __init__(self, selective=None):

View File

@ -155,6 +155,14 @@ class TestTeleBot:
ret_msg = tb.send_voice(CHAT_ID, file_data)
assert ret_msg.voice.mime_type == 'audio/ogg'
def test_get_file(self):
file_data = open('./test_data/record.ogg')
tb = telebot.TeleBot(TOKEN)
ret_msg = tb.send_voice(CHAT_ID, file_data)
file_id = ret_msg.voice.file_id
file_info = tb.get_file(file_id)
assert file_info.file_id == file_id
def test_send_message(self):
text = 'CI Test Message'
tb = telebot.TeleBot(TOKEN)