2015-07-07 22:04:42 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
2015-08-11 21:58:17 +02:00
|
|
|
#
|
|
|
|
# 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/].
|
2015-08-10 18:57:31 +02:00
|
|
|
|
|
|
|
|
2015-07-07 22:04:42 +02:00
|
|
|
import os
|
2015-08-28 18:02:02 +02:00
|
|
|
import sys
|
|
|
|
sys.path.append('.')
|
2015-08-17 14:56:51 +02:00
|
|
|
import json
|
2015-07-07 22:04:42 +02:00
|
|
|
import telegram
|
|
|
|
import unittest
|
2015-08-14 20:47:31 +02:00
|
|
|
from datetime import datetime
|
2015-07-07 22:04:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BotTest(unittest.TestCase):
|
2015-08-17 14:56:51 +02:00
|
|
|
@staticmethod
|
|
|
|
def is_json(string):
|
|
|
|
try:
|
|
|
|
json.loads(string)
|
2015-08-17 15:01:17 +02:00
|
|
|
except ValueError:
|
2015-08-17 14:56:51 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2015-07-07 22:04:42 +02:00
|
|
|
def setUp(self):
|
2015-08-09 14:59:41 +02:00
|
|
|
bot = telegram.Bot(token=os.environ.get('TOKEN'))
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id = os.environ.get('CHAT_ID')
|
|
|
|
|
2015-07-07 22:04:42 +02:00
|
|
|
self._bot = bot
|
2015-08-28 18:28:58 +02:00
|
|
|
self._chat_id = chat_id
|
|
|
|
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing the Bot API class')
|
2015-07-07 22:04:42 +02:00
|
|
|
|
|
|
|
def testGetMe(self):
|
|
|
|
'''Test the telegram.Bot getMe method'''
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing getMe')
|
2015-07-14 10:24:08 +02:00
|
|
|
bot = self._bot.getMe()
|
2015-08-28 18:28:58 +02:00
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(bot.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(bot.id, 120405045)
|
|
|
|
self.assertEqual(bot.first_name, 'Toledo\'s Palace Bot')
|
|
|
|
self.assertEqual(bot.last_name, None)
|
|
|
|
self.assertEqual(bot.username, 'ToledosPalaceBot')
|
|
|
|
self.assertEqual(bot.name, '@ToledosPalaceBot')
|
2015-07-07 22:04:42 +02:00
|
|
|
|
|
|
|
def testSendMessage(self):
|
|
|
|
'''Test the telegram.Bot sendMessage method'''
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing sendMessage')
|
2015-08-28 18:28:58 +02:00
|
|
|
message = self._bot.sendMessage(chat_id=self._chat_id,
|
2015-07-12 15:31:18 +02:00
|
|
|
text='Моё судно на воздушной подушке полно угрей')
|
2015-08-28 18:28:58 +02:00
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.text, u'Моё судно на воздушной подушке полно угрей')
|
2015-08-28 18:07:12 +02:00
|
|
|
self.assertTrue(isinstance(message.date, datetime))
|
2015-08-14 20:47:31 +02:00
|
|
|
|
2015-07-07 22:04:42 +02:00
|
|
|
def testGetUpdates(self):
|
|
|
|
'''Test the telegram.Bot getUpdates method'''
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing getUpdates')
|
2015-07-07 22:04:42 +02:00
|
|
|
updates = self._bot.getUpdates()
|
2015-08-28 18:28:58 +02:00
|
|
|
|
2015-08-21 19:49:07 +02:00
|
|
|
if updates:
|
|
|
|
self.assertTrue(self.is_json(updates[0].to_json()))
|
2015-08-28 18:07:12 +02:00
|
|
|
self.assertTrue(isinstance(updates[0], telegram.Update))
|
2015-07-07 22:04:42 +02:00
|
|
|
|
|
|
|
def testForwardMessage(self):
|
|
|
|
'''Test the telegram.Bot forwardMessage method'''
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing forwardMessage')
|
2015-08-28 18:28:58 +02:00
|
|
|
message = self._bot.forwardMessage(chat_id=self._chat_id,
|
|
|
|
from_chat_id=self._chat_id,
|
2015-07-07 22:04:42 +02:00
|
|
|
message_id=138)
|
2015-08-28 18:28:58 +02:00
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.text, 'Oi')
|
|
|
|
self.assertEqual(message.forward_from.username, 'leandrotoledo')
|
2015-08-28 18:07:12 +02:00
|
|
|
self.assertTrue(isinstance(message.forward_date, datetime))
|
2015-07-07 22:04:42 +02:00
|
|
|
|
2015-07-07 23:46:32 +02:00
|
|
|
def testSendPhoto(self):
|
|
|
|
'''Test the telegram.Bot sendPhoto method'''
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing sendPhoto - File')
|
2015-08-28 18:02:02 +02:00
|
|
|
message = self._bot.sendPhoto(photo=open('tests/data/telegram.png', 'rb'),
|
2015-08-11 22:32:06 +02:00
|
|
|
caption='testSendPhoto',
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id)
|
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.photo[0].file_size, 1451)
|
|
|
|
self.assertEqual(message.caption, 'testSendPhoto')
|
2015-07-08 00:06:17 +02:00
|
|
|
|
2015-07-08 01:10:43 +02:00
|
|
|
def testResendPhoto(self):
|
2015-07-08 00:06:17 +02:00
|
|
|
'''Test the telegram.Bot sendPhoto method'''
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing sendPhoto - Resend')
|
2015-08-17 16:34:42 +02:00
|
|
|
message = self._bot.sendPhoto(photo='AgADAQADr6cxGzU8LQe6q0dMJD2rHYkP2ykABFymiQqJgjxRGGMAAgI',
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id)
|
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.photo[0].file_id, 'AgADAQADr6cxGzU8LQe6q0dMJD2rHYkP2ykABFymiQqJgjxRGGMAAgI')
|
2015-07-08 01:10:43 +02:00
|
|
|
|
2015-08-15 20:00:28 +02:00
|
|
|
def testSendJPGURLPhoto(self):
|
2015-07-14 10:24:08 +02:00
|
|
|
'''Test the telegram.Bot sendPhoto method'''
|
2015-08-15 20:00:28 +02:00
|
|
|
print('Testing testSendJPGURLPhoto - URL')
|
2015-08-17 16:34:42 +02:00
|
|
|
message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.jpg&text=telegram',
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id)
|
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.photo[0].file_size, 822)
|
2015-07-14 10:24:08 +02:00
|
|
|
|
2015-08-15 20:00:28 +02:00
|
|
|
def testSendPNGURLPhoto(self):
|
|
|
|
'''Test the telegram.Bot sendPhoto method'''
|
|
|
|
print('Testing testSendPNGURLPhoto - URL')
|
2015-08-17 16:34:42 +02:00
|
|
|
message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.png&text=telegram',
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id)
|
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.photo[0].file_size, 684)
|
2015-08-15 20:00:28 +02:00
|
|
|
|
|
|
|
def testSendGIFURLPhoto(self):
|
|
|
|
'''Test the telegram.Bot sendPhoto method'''
|
|
|
|
print('Testing testSendGIFURLPhoto - URL')
|
2015-08-17 16:34:42 +02:00
|
|
|
message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.gif&text=telegram',
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id)
|
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.photo[0].file_size, 684)
|
2015-08-15 20:00:28 +02:00
|
|
|
|
2015-07-08 01:10:43 +02:00
|
|
|
def testSendAudio(self):
|
|
|
|
'''Test the telegram.Bot sendAudio method'''
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing sendAudio - File')
|
2015-08-28 18:02:02 +02:00
|
|
|
message = self._bot.sendAudio(audio=open('tests/data/telegram.mp3', 'rb'),
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id,
|
2015-08-17 16:34:42 +02:00
|
|
|
performer='Leandro Toledo',
|
|
|
|
title='Teste')
|
2015-08-28 18:28:58 +02:00
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.audio.file_size, 28232)
|
|
|
|
self.assertEqual(message.audio.performer, 'Leandro Toledo')
|
|
|
|
self.assertEqual(message.audio.title, 'Teste')
|
2015-07-08 01:10:43 +02:00
|
|
|
|
|
|
|
def testResendAudio(self):
|
|
|
|
'''Test the telegram.Bot sendAudio method'''
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing sendAudio - Resend')
|
2015-08-17 16:34:42 +02:00
|
|
|
message = self._bot.sendAudio(audio='BQADAQADwwcAAjU8LQdBRsl3_qD2TAI',
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id,
|
2015-08-17 16:34:42 +02:00
|
|
|
performer='Leandro Toledo', # Bug #39
|
|
|
|
title='Teste') # Bug #39
|
2015-08-28 18:28:58 +02:00
|
|
|
|
2015-08-17 16:34:42 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.audio.file_id, 'BQADAQADwwcAAjU8LQdBRsl3_qD2TAI')
|
2015-08-17 16:34:42 +02:00
|
|
|
|
|
|
|
def testSendVoice(self):
|
|
|
|
'''Test the telegram.Bot sendVoice method'''
|
|
|
|
print('Testing sendVoice - File')
|
2015-08-28 18:02:02 +02:00
|
|
|
message = self._bot.sendVoice(voice=open('tests/data/telegram.ogg', 'rb'),
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id)
|
|
|
|
|
2015-08-17 16:34:42 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.voice.file_size, 9199)
|
2015-08-17 16:34:42 +02:00
|
|
|
|
|
|
|
def testResendVoice(self):
|
|
|
|
'''Test the telegram.Bot sendVoice method'''
|
|
|
|
print('Testing sendVoice - Resend')
|
|
|
|
message = self._bot.sendVoice(voice='AwADAQADIQEAAvjAuQABSAXg_GhkhZcC',
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id)
|
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.voice.file_id, 'AwADAQADIQEAAvjAuQABSAXg_GhkhZcC')
|
2015-07-08 02:12:51 +02:00
|
|
|
|
|
|
|
def testSendDocument(self):
|
|
|
|
'''Test the telegram.Bot sendDocument method'''
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing sendDocument - File')
|
2015-08-28 18:02:02 +02:00
|
|
|
message = self._bot.sendDocument(document=open('tests/data/telegram.png', 'rb'),
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id)
|
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.document.file_size, 12948)
|
2015-07-08 02:12:51 +02:00
|
|
|
|
2015-08-15 20:00:28 +02:00
|
|
|
def testSendGIFURLDocument(self):
|
|
|
|
'''Test the telegram.Bot sendDocument method'''
|
|
|
|
print('Testing sendDocument - File')
|
2015-08-17 16:34:42 +02:00
|
|
|
message = self._bot.sendPhoto(photo='http://dummyimage.com/600x400/000/fff.gif&text=telegram',
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id)
|
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.photo[0].file_size, 684)
|
2015-08-15 20:00:28 +02:00
|
|
|
|
2015-07-08 02:12:51 +02:00
|
|
|
def testResendDocument(self):
|
|
|
|
'''Test the telegram.Bot sendDocument method'''
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing sendDocument - Resend')
|
2015-08-17 16:34:42 +02:00
|
|
|
message = self._bot.sendDocument(document='BQADAQADHAADNTwtBxZxUGKyxYbYAg',
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id)
|
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.document.file_id, 'BQADAQADHAADNTwtBxZxUGKyxYbYAg')
|
2015-07-08 04:52:12 +02:00
|
|
|
|
2015-07-10 18:43:35 +02:00
|
|
|
def testSendVideo(self):
|
|
|
|
'''Test the telegram.Bot sendVideo method'''
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing sendVideo - File')
|
2015-08-28 18:02:02 +02:00
|
|
|
message = self._bot.sendVideo(video=open('tests/data/telegram.mp4', 'rb'),
|
2015-08-11 22:32:06 +02:00
|
|
|
caption='testSendVideo',
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id)
|
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.video.file_size, 326534)
|
|
|
|
self.assertEqual(message.caption, 'testSendVideo')
|
2015-07-10 18:43:35 +02:00
|
|
|
|
2015-07-08 14:17:18 +02:00
|
|
|
def testResendVideo(self):
|
|
|
|
'''Test the telegram.Bot sendVideo method'''
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing sendVideo - Resend')
|
2015-08-17 16:34:42 +02:00
|
|
|
message = self._bot.sendVideo(video='BAADAQADIgEAAvjAuQABOuTB937fPTgC',
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id)
|
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.video.duration, 4)
|
2015-07-08 14:37:25 +02:00
|
|
|
|
2015-07-14 10:24:08 +02:00
|
|
|
def testResendSticker(self):
|
|
|
|
'''Test the telegram.Bot sendSticker method'''
|
|
|
|
print('Testing sendSticker - Resend')
|
2015-08-17 16:34:42 +02:00
|
|
|
message = self._bot.sendSticker(sticker='BQADAQADHAADyIsGAAFZfq1bphjqlgI',
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id)
|
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.sticker.file_size, 39518)
|
2015-07-14 10:24:08 +02:00
|
|
|
|
2015-07-08 14:37:25 +02:00
|
|
|
def testSendLocation(self):
|
|
|
|
'''Test the telegram.Bot sendLocation method'''
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing sendLocation')
|
2015-07-08 14:37:25 +02:00
|
|
|
message = self._bot.sendLocation(latitude=-23.558873,
|
|
|
|
longitude=-46.659732,
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id)
|
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(message.location.latitude, -23.558873)
|
|
|
|
self.assertEqual(message.location.longitude, -46.659732)
|
2015-07-08 14:55:06 +02:00
|
|
|
|
|
|
|
def testSendChatAction(self):
|
|
|
|
'''Test the telegram.Bot sendChatAction method'''
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing sendChatAction - ChatAction.TYPING')
|
2015-08-28 18:28:58 +02:00
|
|
|
|
2015-07-08 22:58:50 +02:00
|
|
|
self._bot.sendChatAction(action=telegram.ChatAction.TYPING,
|
2015-08-28 18:28:58 +02:00
|
|
|
chat_id=self._chat_id)
|
2015-07-08 15:10:08 +02:00
|
|
|
|
|
|
|
def testGetUserProfilePhotos(self):
|
|
|
|
'''Test the telegram.Bot getUserProfilePhotos method'''
|
2015-07-12 15:31:18 +02:00
|
|
|
print('Testing getUserProfilePhotos')
|
2015-08-28 18:28:58 +02:00
|
|
|
upf = self._bot.getUserProfilePhotos(user_id=self._chat_id)
|
|
|
|
|
2015-08-17 14:56:51 +02:00
|
|
|
self.assertTrue(self.is_json(upf.to_json()))
|
2015-08-28 18:28:58 +02:00
|
|
|
self.assertEqual(upf.photos[0][0].file_size, 6547)
|
2015-08-28 18:02:02 +02:00
|
|
|
|
|
|
|
unittest.main()
|