mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-13 11:18:20 +01:00
Adding File and its tests
This commit is contained in:
parent
778c63a6d3
commit
b79530b10c
6 changed files with 311 additions and 9 deletions
|
@ -94,6 +94,7 @@ sendLocation Yes
|
|||
sendChatAction Yes
|
||||
getUpdates Yes
|
||||
getUserProfilePhotos Yes
|
||||
getFile Yes
|
||||
setWebhook Yes
|
||||
========================= ============
|
||||
|
||||
|
@ -220,6 +221,12 @@ To hide `Custom Keyboards <https://core.telegram.org/bots#keyboards>`_::
|
|||
>>> reply_markup = telegram.ReplyKeyboardHide()
|
||||
>>> bot.sendMessage(chat_id=chat_id, text="I'm back.", reply_markup=reply_markup)
|
||||
|
||||
To download a file (you will need its file_id)::
|
||||
|
||||
>>> file_id = message.voice.file_id
|
||||
>>> newFile = bot.getFile(file_id)
|
||||
>>> newFile.download('voice.ogg')
|
||||
|
||||
There are many more API methods, to read the full API documentation::
|
||||
|
||||
$ pydoc telegram.Bot
|
||||
|
|
|
@ -40,18 +40,17 @@ from .replykeyboardhide import ReplyKeyboardHide
|
|||
from .forcereply import ForceReply
|
||||
from .error import TelegramError
|
||||
from .inputfile import InputFile
|
||||
from .file import File
|
||||
from .nullhandler import NullHandler
|
||||
from .emoji import Emoji
|
||||
from .parsemode import ParseMode
|
||||
from .message import Message
|
||||
from .update import Update
|
||||
from .bot import Bot
|
||||
from .command_handler import *
|
||||
|
||||
__all__ = ['Bot', 'Emoji', 'TelegramError', 'InputFile', 'ReplyMarkup',
|
||||
'ForceReply', 'ReplyKeyboardHide', 'ReplyKeyboardMarkup',
|
||||
'UserProfilePhotos', 'ChatAction', 'Location', 'Contact',
|
||||
'Video', 'Sticker', 'Document', 'Audio', 'PhotoSize', 'GroupChat',
|
||||
'Update', 'ParseMode', 'Message', 'User', 'TelegramObject', 'NullHandler',
|
||||
'Voice', 'CommandHandler', 'CommandHandlerWithHelp',
|
||||
'CommandHandlerWithFatherCommand', 'CommandHandlerWithHelpAndFather']
|
||||
'Video', 'Sticker', 'Document', 'File', 'Audio', 'PhotoSize',
|
||||
'GroupChat', 'Update', 'ParseMode', 'Message', 'User',
|
||||
'TelegramObject', 'NullHandler', 'Voice']
|
||||
|
|
|
@ -22,8 +22,8 @@
|
|||
import functools
|
||||
import logging
|
||||
|
||||
from telegram import (User, Message, Update, UserProfilePhotos, TelegramError,
|
||||
ReplyMarkup, TelegramObject, NullHandler)
|
||||
from telegram import (User, Message, Update, UserProfilePhotos, File,
|
||||
TelegramError, ReplyMarkup, TelegramObject, NullHandler)
|
||||
from telegram.utils import request
|
||||
|
||||
H = NullHandler()
|
||||
|
@ -31,6 +31,7 @@ logging.getLogger(__name__).addHandler(H)
|
|||
|
||||
|
||||
class Bot(TelegramObject):
|
||||
|
||||
"""This object represents a Telegram Bot.
|
||||
|
||||
Attributes:
|
||||
|
@ -58,6 +59,8 @@ class Bot(TelegramObject):
|
|||
else:
|
||||
self.base_url = base_url + self.token
|
||||
|
||||
self.base_file_url = 'https://api.telegram.org/file/bot%s' % self.token
|
||||
|
||||
self.bot = None
|
||||
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
@ -615,6 +618,33 @@ class Bot(TelegramObject):
|
|||
|
||||
return UserProfilePhotos.de_json(result)
|
||||
|
||||
@log
|
||||
def getFile(self,
|
||||
file_id):
|
||||
"""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.
|
||||
|
||||
Returns:
|
||||
Returns a telegram.File object
|
||||
"""
|
||||
|
||||
url = '%s/getFile' % self.base_url
|
||||
|
||||
data = {'file_id': file_id}
|
||||
|
||||
result = request.post(url, data)
|
||||
|
||||
if result.get('file_path'):
|
||||
result['file_path'] = '%s/%s' % (self.base_file_url,
|
||||
result['file_path'])
|
||||
|
||||
return File.de_json(result)
|
||||
|
||||
@log
|
||||
def getUpdates(self,
|
||||
offset=None,
|
||||
|
|
81
telegram/file.py
Normal file
81
telegram/file.py
Normal file
|
@ -0,0 +1,81 @@
|
|||
#!/usr/bin/env python
|
||||
#
|
||||
# A library that provides a Python interface to the Telegram Bot API
|
||||
# Copyright (C) 2015 Leandro Toledo de Souza <leandrotoeldodesouza@gmail.com>
|
||||
#
|
||||
# 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/].
|
||||
|
||||
"""This module contains a object that represents a Telegram File"""
|
||||
|
||||
from os.path import basename
|
||||
|
||||
from telegram import TelegramObject
|
||||
from telegram.utils.request import download as _download
|
||||
|
||||
|
||||
class File(TelegramObject):
|
||||
|
||||
"""This object represents a Telegram File.
|
||||
|
||||
Attributes:
|
||||
file_id (str):
|
||||
file_size (str):
|
||||
file_path (str):
|
||||
|
||||
Args:
|
||||
file_id (str):
|
||||
**kwargs: Arbitrary keyword arguments.
|
||||
|
||||
Keyword Args:
|
||||
file_size (Optional[int]):
|
||||
file_path (Optional[str]):
|
||||
"""
|
||||
|
||||
def __init__(self,
|
||||
file_id,
|
||||
**kwargs):
|
||||
# Required
|
||||
self.file_id = str(file_id)
|
||||
# Optionals
|
||||
self.file_size = int(kwargs.get('file_size', 0))
|
||||
self.file_path = str(kwargs.get('file_path', ''))
|
||||
|
||||
@staticmethod
|
||||
def de_json(data):
|
||||
"""
|
||||
Args:
|
||||
data (str):
|
||||
|
||||
Returns:
|
||||
telegram.File:
|
||||
"""
|
||||
if not data:
|
||||
return None
|
||||
|
||||
return File(**data)
|
||||
|
||||
def download(self,
|
||||
custom_path=None):
|
||||
"""
|
||||
Args:
|
||||
custom_path (str):
|
||||
"""
|
||||
url = self.file_path
|
||||
|
||||
if custom_path:
|
||||
filename = basename(custom_path)
|
||||
else:
|
||||
filename = basename(url)
|
||||
|
||||
_download(url, filename)
|
|
@ -23,10 +23,10 @@ import json
|
|||
|
||||
try:
|
||||
from urllib.parse import urlencode
|
||||
from urllib.request import urlopen, Request
|
||||
from urllib.request import urlopen, urlretrieve, Request
|
||||
from urllib.error import HTTPError, URLError
|
||||
except ImportError:
|
||||
from urllib import urlencode
|
||||
from urllib import urlencode, urlretrieve
|
||||
from urllib2 import urlopen, Request
|
||||
from urllib2 import HTTPError, URLError
|
||||
|
||||
|
@ -99,3 +99,17 @@ def post(url,
|
|||
raise TelegramError(message)
|
||||
|
||||
return _parse(result)
|
||||
|
||||
|
||||
def download(url,
|
||||
filename):
|
||||
"""Download a file by its URL.
|
||||
Args:
|
||||
url:
|
||||
The web location we want to retrieve.
|
||||
|
||||
filename:
|
||||
The filename wihtin the path to download the file.
|
||||
"""
|
||||
|
||||
urlretrieve(url, filename)
|
||||
|
|
171
tests/test_file.py
Normal file
171
tests/test_file.py
Normal file
|
@ -0,0 +1,171 @@
|
|||
#!/usr/bin/env python
|
||||
#
|
||||
# A library that provides a Python interface to the Telegram Bot API
|
||||
# Copyright (C) 2015 Leandro Toledo de Souza <leandrotoeldodesouza@gmail.com>
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General 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 General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
||||
|
||||
"""This module contains a object that represents Tests for Telegram File"""
|
||||
|
||||
import os
|
||||
import unittest
|
||||
import sys
|
||||
sys.path.append('.')
|
||||
|
||||
import telegram
|
||||
from tests.base import BaseTest
|
||||
|
||||
|
||||
class FileTest(BaseTest, unittest.TestCase):
|
||||
"""This object represents Tests for Telegram File."""
|
||||
|
||||
def setUp(self):
|
||||
self.audio_file_id = 'BQADAQADDwADHyP1B6PSPq2HjX8kAg'
|
||||
self.document_file_id = 'BQADAQADpAADHyP1B04ipZxJTe2BAg'
|
||||
self.sticker_file_id = 'BQADAQADHAADyIsGAAFZfq1bphjqlgI'
|
||||
self.video_file_id = 'BAADAQADXwADHyP1BwJFTcmY2RYCAg'
|
||||
self.voice_file_id = 'AwADAQADTgADHyP1B_mbw34svXPHAg'
|
||||
|
||||
|
||||
self.json_dict = {
|
||||
'file_id': self.audio_file_id,
|
||||
'file_path': 'https://api.telegram.org/file/bot133505823:AAHZFMHno3mzVLErU5b5jJvaeG--qUyLyG0/document/file_3',
|
||||
'file_size': 28232
|
||||
}
|
||||
|
||||
def test_get_and_download_file_audio(self):
|
||||
"""Test telegram.Bot getFile method - Audio"""
|
||||
print('Testing bot.getFile - With Audio.file_id')
|
||||
|
||||
newFile = self._bot.getFile(self.audio_file_id)
|
||||
|
||||
self.assertEqual(newFile.file_size, 28232)
|
||||
self.assertEqual(newFile.file_id, self.audio_file_id)
|
||||
self.assertTrue(newFile.file_path.startswith('https://'))
|
||||
|
||||
newFile.download('telegram.mp3')
|
||||
|
||||
self.assertTrue(os.path.isfile('telegram.mp3'))
|
||||
|
||||
def test_get_and_download_file_document(self):
|
||||
"""Test telegram.Bot getFile method - Document"""
|
||||
print('Testing bot.getFile - With Document.file_id')
|
||||
|
||||
newFile = self._bot.getFile(self.document_file_id)
|
||||
|
||||
self.assertEqual(newFile.file_size, 12948)
|
||||
self.assertEqual(newFile.file_id, self.document_file_id)
|
||||
self.assertTrue(newFile.file_path.startswith('https://'))
|
||||
|
||||
newFile.download('telegram.png')
|
||||
|
||||
self.assertTrue(os.path.isfile('telegram.png'))
|
||||
|
||||
def test_get_and_download_file_sticker(self):
|
||||
"""Test telegram.Bot getFile method - Sticker"""
|
||||
print('Testing bot.getFile - With Sticker.file_id')
|
||||
|
||||
newFile = self._bot.getFile(self.sticker_file_id)
|
||||
|
||||
self.assertEqual(newFile.file_size, 39518)
|
||||
self.assertEqual(newFile.file_id, self.sticker_file_id)
|
||||
self.assertTrue(newFile.file_path.startswith('https://'))
|
||||
|
||||
newFile.download('telegram.webp')
|
||||
|
||||
self.assertTrue(os.path.isfile('telegram.webp'))
|
||||
|
||||
def test_get_and_download_file_video(self):
|
||||
"""Test telegram.Bot getFile method - Video"""
|
||||
print('Testing bot.getFile - With Video.file_id')
|
||||
|
||||
newFile = self._bot.getFile(self.video_file_id)
|
||||
|
||||
self.assertEqual(newFile.file_size, 326534)
|
||||
self.assertEqual(newFile.file_id, self.video_file_id)
|
||||
self.assertTrue(newFile.file_path.startswith('https://'))
|
||||
|
||||
newFile.download('telegram.mp4')
|
||||
|
||||
self.assertTrue(os.path.isfile('telegram.mp4'))
|
||||
|
||||
def test_get_and_download_file_voice(self):
|
||||
"""Test telegram.Bot getFile method - Voice"""
|
||||
print('Testing bot.getFile - With Voice.file_id')
|
||||
|
||||
newFile = self._bot.getFile(self.voice_file_id)
|
||||
|
||||
self.assertEqual(newFile.file_size, 9199)
|
||||
self.assertEqual(newFile.file_id, self.voice_file_id)
|
||||
self.assertTrue(newFile.file_path.startswith('https://'))
|
||||
|
||||
newFile.download('telegram.ogg')
|
||||
|
||||
self.assertTrue(os.path.isfile('telegram.ogg'))
|
||||
|
||||
def test_file_de_json(self):
|
||||
"""Test File.de_json() method"""
|
||||
print('Testing File.de_json()')
|
||||
|
||||
newFile = telegram.File.de_json(self.json_dict)
|
||||
|
||||
self.assertEqual(newFile.file_id, self.json_dict['file_id'])
|
||||
self.assertEqual(newFile.file_path, self.json_dict['file_path'])
|
||||
self.assertEqual(newFile.file_size, self.json_dict['file_size'])
|
||||
|
||||
def test_file_to_json(self):
|
||||
"""Test File.to_json() method"""
|
||||
print('Testing File.to_json()')
|
||||
|
||||
newFile = telegram.File.de_json(self.json_dict)
|
||||
|
||||
self.assertTrue(self.is_json(newFile.to_json()))
|
||||
|
||||
def test_file_to_dict(self):
|
||||
"""Test File.to_dict() method"""
|
||||
print('Testing File.to_dict()')
|
||||
|
||||
newFile = telegram.File.de_json(self.json_dict)
|
||||
|
||||
self.assertTrue(self.is_dict(newFile.to_dict()))
|
||||
self.assertEqual(newFile['file_id'], self.json_dict['file_id'])
|
||||
self.assertEqual(newFile['file_path'], self.json_dict['file_path'])
|
||||
self.assertEqual(newFile['file_size'], self.json_dict['file_size'])
|
||||
|
||||
def test_error_get_empty_file_id(self):
|
||||
print('Testing bot.getFile - Null file_id')
|
||||
|
||||
json_dict = self.json_dict
|
||||
|
||||
json_dict['file_id'] = ''
|
||||
del(json_dict['file_path'])
|
||||
del(json_dict['file_size'])
|
||||
|
||||
self.assertRaises(telegram.TelegramError,
|
||||
lambda: self._bot.getFile(**json_dict))
|
||||
|
||||
def test_error_file_without_required_args(self):
|
||||
print('Testing bot.getFile - Without required arguments')
|
||||
|
||||
json_dict = self.json_dict
|
||||
|
||||
del(json_dict['file_id'])
|
||||
del(json_dict['file_path'])
|
||||
del(json_dict['file_size'])
|
||||
|
||||
self.assertRaises(TypeError,
|
||||
lambda: self._bot.getFile(**json_dict))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Add table
Reference in a new issue