X Tutup
Skip to content
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ To post an Emoji (special thanks to `Tim Whitlock <http://apps.timwhitlock.info/

>>> bot.sendMessage(chat_id=chat_id, text=telegram.Emoji.PILE_OF_POO)

To post an image file via URL (right now only sendPhoto supports this)::
To post an image file via URL::

>>> bot.sendPhoto(chat_id=chat_id, photo='https://telegram.org/img/t_logo.png')

To post a voice file::
To post a voice file from disk::

>>> bot.sendVoice(chat_id=chat_id, voice=open('tests/telegram.ogg', 'rb'))

Expand Down
37 changes: 23 additions & 14 deletions telegram/inputfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
except ImportError:
from mimetools import choose_boundary
from urllib2 import urlopen

import mimetypes
import os
import sys
Expand Down Expand Up @@ -69,19 +70,30 @@ def __init__(self,
self.input_name = 'certificate'
self.input_file = data.pop('certificate')

if isinstance(self.input_file, file):
if str(self.input_file).startswith('http'):
from_url = True
self.input_file = urlopen(self.input_file)
else:
from_url = False

if isinstance(self.input_file, file) or from_url:
self.filename = None
self.input_file_content = self.input_file.read()
if 'filename' in data:
self.filename = self.data.pop('filename')
else:
elif isinstance(self.input_file, file):
self.filename = os.path.basename(self.input_file.name)
self.mimetype = mimetypes.guess_type(self.filename)[0] or \
DEFAULT_MIME_TYPE

if 'http' in self.input_file:
self.input_file_content = urlopen(self.input_file).read()
self.mimetype = InputFile.is_image(self.input_file_content)
self.filename = self.mimetype.replace('/', '.')
elif from_url:
self.filename = os.path.basename(self.input_file.url)\
.split('?')[0].split('&')[0]

try:
self.mimetype = InputFile.is_image(self.input_file_content)
if not self.filename or '.' not in self.filename:
self.filename = self.mimetype.replace('/', '.')
except TelegramError:
self.mimetype = mimetypes.guess_type(self.filename)[0] or \
DEFAULT_MIME_TYPE

@property
def headers(self):
Expand Down Expand Up @@ -185,10 +197,7 @@ def is_inputfile(data):
if file_type:
file_content = data[file_type[0]]

if file_type[0] == 'photo' or file_type[0] == 'document':
return isinstance(file_content, file) or \
str(file_content).startswith('http')

return isinstance(file_content, file)
return isinstance(file_content, file) or \
str(file_content).startswith('http')

return False
2 changes: 1 addition & 1 deletion telegram/photosize.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self,
height,
**kwargs):
# Required
self.file_id = file_id
self.file_id = str(file_id)
self.width = int(width)
self.height = int(height)
# Optionals
Expand Down
21 changes: 21 additions & 0 deletions tests/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class AudioTest(BaseTest, unittest.TestCase):
def setUp(self):
self.audio_file = open('tests/data/telegram.mp3', 'rb')
self.audio_file_id = 'BQADAQADDwADHyP1B6PSPq2HjX8kAg'
self.audio_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.mp3'
self.duration = 4
self.performer = 'Leandro Toledo'
self.title = 'Teste'
Expand Down Expand Up @@ -129,6 +130,26 @@ def test_send_audio_mp3_file_custom_filename(self):
self.assertEqual(audio.mime_type, self.mime_type)
self.assertEqual(audio.file_size, self.file_size)

def test_send_audio_mp3_url_file(self):
"""Test telegram.Bot sendAudio method"""
print('Testing bot.sendAudio - MP3 File by URL')

message = self._bot.sendAudio(chat_id=self._chat_id,
audio=self.audio_file_url,
duration=self.duration,
performer=self.performer,
title=self.title)

audio = message.audio

self.assertTrue(isinstance(audio.file_id, str))
self.assertNotEqual(audio.file_id, '')
self.assertEqual(audio.duration, self.duration)
self.assertEqual(audio.performer, self.performer)
self.assertEqual(audio.title, self.title)
self.assertEqual(audio.mime_type, self.mime_type)
self.assertEqual(audio.file_size, self.file_size)

def test_send_audio_resend(self):
"""Test telegram.Bot sendAudio method"""
print('Testing bot.sendAudio - Resend by file_id')
Expand Down
4 changes: 2 additions & 2 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DocumentTest(BaseTest, unittest.TestCase):
def setUp(self):
self.document_file = open('tests/data/telegram.png', 'rb')
self.document_file_id = 'BQADAQADpAADHyP1B04ipZxJTe2BAg'
self.document_file_url = 'http://dummyimage.com/600x400/000/fff.gif&text=telegram'
self.document_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.gif'
self.thumb = {'width': 90,
'height': 90,
'file_id': 'BQADAQADoQADHyP1B0mzJMVyzcB0Ag',
Expand Down Expand Up @@ -96,7 +96,7 @@ def test_send_document_url_gif_file(self):
self.assertTrue(isinstance(document.file_id, str))
self.assertNotEqual(document.file_id, '')
self.assertTrue(isinstance(document.thumb, telegram.PhotoSize))
self.assertEqual(document.file_name, 'image.gif')
self.assertEqual(document.file_name, 'telegram.gif')
self.assertEqual(document.mime_type, 'image/gif')
self.assertEqual(document.file_size, 3878)

Expand Down
220 changes: 220 additions & 0 deletions tests/test_photo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2016
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# 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 Photo"""

import os
import unittest
import sys
sys.path.append('.')

import telegram
from tests.base import BaseTest


class PhotoTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram Photo."""

def setUp(self):
self.photo_file = open('tests/data/telegram.jpg', 'rb')
self.photo_file_id = 'AgADAQADvb8xGx8j9QcpZDKxYoFK3bfX1i8ABFX_dgMWoKDuQugAAgI'
self.photo_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.jpg'
self.width = 300
self.height = 300
self.thumb = {'width': 90,
'height': 90,
'file_id': 'AgADAQADvb8xGx8j9QcpZDKxYoFK3bfX1i8ABBxRLXFhLnhIQ-gAAgI',
'file_size': 1478}
self.file_size = 10209

# caption is part of sendPhoto method but not Photo object
self.caption = u'PhotoTest - Caption'

self.json_dict = {
'file_id': self.photo_file_id,
'width': self.width,
'height': self.height,
'file_size': self.file_size
}

def test_sendphotoo_all_args(self):
"""Test telegram.Bot sendAudio method"""
print('Testing bot.sendPhoto - With all arguments')

message = self._bot.sendPhoto(self._chat_id,
self.photo_file,
caption=self.caption)

thumb, photo = message.photo

self.assertTrue(isinstance(thumb.file_id, str))
self.assertNotEqual(thumb.file_id, '')
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
self.assertEqual(thumb.width, self.thumb['width'])
self.assertEqual(thumb.height, self.thumb['height'])
self.assertEqual(thumb.file_size, self.thumb['file_size'])

self.assertTrue(isinstance(photo.file_id, str))
self.assertNotEqual(photo.file_id, '')
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, self.width)
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_size, self.file_size)

self.assertEqual(message.caption, self.caption)

def test_send_photo_jpg_file(self):
"""Test telegram.Bot sendPhoto method"""
print('Testing bot.sendPhoto - JPG File')

message = self._bot.sendPhoto(self._chat_id,
self.photo_file)

thumb, photo = message.photo

self.assertTrue(isinstance(thumb.file_id, str))
self.assertNotEqual(thumb.file_id, '')
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
self.assertEqual(thumb.width, self.thumb['width'])
self.assertEqual(thumb.height, self.thumb['height'])
self.assertEqual(thumb.file_size, self.thumb['file_size'])

self.assertTrue(isinstance(photo.file_id, str))
self.assertNotEqual(photo.file_id, '')
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, self.width)
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_size, self.file_size)

def test_send_photo_url_jpg_file(self):
"""Test telegram.Bot sendPhoto method"""
print('Testing bot.sendPhoto - JPG File by URL')

message = self._bot.sendPhoto(self._chat_id,
self.photo_file_url)

thumb, photo = message.photo

self.assertTrue(isinstance(thumb.file_id, str))
self.assertNotEqual(thumb.file_id, '')
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
self.assertEqual(thumb.width, self.thumb['width'])
self.assertEqual(thumb.height, self.thumb['height'])
self.assertEqual(thumb.file_size, self.thumb['file_size'])

self.assertTrue(isinstance(photo.file_id, str))
self.assertNotEqual(photo.file_id, '')
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, self.width)
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_size, self.file_size)

def test_send_photo_resend(self):
"""Test telegram.Bot sendPhoto method"""
print('Testing bot.sendPhoto - Resend by file_id')

message = self._bot.sendPhoto(chat_id=self._chat_id,
photo=self.photo_file_id)

thumb, photo = message.photo

self.assertEqual(thumb.file_id, self.thumb['file_id'])
self.assertTrue(isinstance(thumb, telegram.PhotoSize))
self.assertEqual(thumb.width, self.thumb['width'])
self.assertEqual(thumb.height, self.thumb['height'])
self.assertEqual(thumb.file_size, self.thumb['file_size'])

self.assertEqual(photo.file_id, self.photo_file_id)
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, self.width)
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_size, self.file_size)

def test_photo_de_json(self):
"""Test Photo.de_json() method"""
print('Testing Photo.de_json()')

photo = telegram.PhotoSize.de_json(self.json_dict)

self.assertEqual(photo.file_id, self.photo_file_id)
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, self.width)
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_size, self.file_size)

def test_photo_to_json(self):
"""Test Photo.to_json() method"""
print('Testing Photo.to_json()')

photo = telegram.PhotoSize.de_json(self.json_dict)

self.assertTrue(self.is_json(photo.to_json()))

def test_photo_to_dict(self):
"""Test Photo.to_dict() method"""
print('Testing Photo.to_dict()')

photo = telegram.PhotoSize.de_json(self.json_dict)

self.assertTrue(self.is_dict(photo.to_dict()))
self.assertEqual(photo['file_id'], self.photo_file_id)
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo['width'], self.width)
self.assertEqual(photo['height'], self.height)
self.assertEqual(photo['file_size'], self.file_size)

def test_error_send_photo_empty_file(self):
print('Testing bot.sendPhoto - Null file')

json_dict = self.json_dict

del(json_dict['file_id'])
json_dict['photo'] = open(os.devnull, 'rb')

self.assertRaises(telegram.TelegramError,
lambda: self._bot.sendPhoto(chat_id=self._chat_id,
**json_dict))

def test_error_send_photo_empty_file_id(self):
print('Testing bot.sendPhoto - Empty file_id')

json_dict = self.json_dict

del(json_dict['file_id'])
json_dict['photo'] = ''

self.assertRaises(telegram.TelegramError,
lambda: self._bot.sendPhoto(chat_id=self._chat_id,
**json_dict))

def test_error_photo_without_required_args(self):
print('Testing bot.sendPhoto - Without required arguments')

json_dict = self.json_dict

del(json_dict['file_id'])
del(json_dict['width'])
del(json_dict['height'])

self.assertRaises(TypeError,
lambda: self._bot.sendPhoto(chat_id=self._chat_id,
**json_dict))

if __name__ == '__main__':
unittest.main()
23 changes: 23 additions & 0 deletions tests/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class VideoTest(BaseTest, unittest.TestCase):
def setUp(self):
self.video_file = open('tests/data/telegram.mp4', 'rb')
self.video_file_id = 'BAADAQADXwADHyP1BwJFTcmY2RYCAg'
self.video_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.mp4'
self.width = 360
self.height = 640
self.duration = 4
Expand Down Expand Up @@ -139,6 +140,28 @@ def test_send_video_mp4_file_with_custom_filename(self):

self.assertEqual(message.caption, self.caption)

def test_send_video_mp4_file_url(self):
"""Test telegram.Bot sendVideo method"""
print('Testing bot.sendVideo - MP4 File by URL')

message = self._bot.sendVideo(chat_id=self._chat_id,
video=self.video_file_url,
duration=self.duration,
caption=self.caption)

video = message.video

self.assertTrue(isinstance(video.file_id, str))
self.assertNotEqual(video.file_id, '')
self.assertEqual(video.width, 0)
self.assertEqual(video.height, 0)
self.assertEqual(video.duration, self.duration)
self.assertEqual(video.thumb, None)
self.assertEqual(video.mime_type, '')
self.assertEqual(video.file_size, self.file_size)

self.assertEqual(message.caption, self.caption)

def test_send_video_resend(self):
"""Test telegram.Bot sendVideo method"""
print('Testing bot.sendVideo - Resend by file_id')
Expand Down
Loading
X Tutup