2015-07-15 05:52:32 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import sys
|
2022-01-10 16:40:33 +03:00
|
|
|
import warnings
|
2015-09-09 04:43:19 +03:00
|
|
|
|
2015-08-19 23:15:40 +03:00
|
|
|
sys.path.append('../')
|
|
|
|
|
2015-07-15 05:52:32 +03:00
|
|
|
import time
|
2015-08-19 23:10:33 +03:00
|
|
|
import pytest
|
|
|
|
import os
|
2021-06-19 18:59:55 +03:00
|
|
|
from datetime import datetime, timedelta
|
2015-07-15 05:52:32 +03:00
|
|
|
|
2015-08-19 23:10:33 +03:00
|
|
|
import telebot
|
2015-07-15 05:52:32 +03:00
|
|
|
from telebot import types
|
2015-08-31 12:46:18 +03:00
|
|
|
from telebot import util
|
2015-08-01 05:12:15 +03:00
|
|
|
|
2020-04-15 08:16:07 +03:00
|
|
|
should_skip = 'TOKEN' and 'CHAT_ID' not in os.environ
|
2015-08-19 23:10:33 +03:00
|
|
|
|
|
|
|
if not should_skip:
|
|
|
|
TOKEN = os.environ['TOKEN']
|
|
|
|
CHAT_ID = os.environ['CHAT_ID']
|
2016-06-07 14:16:17 +03:00
|
|
|
GROUP_ID = os.environ['GROUP_ID']
|
2015-08-19 23:10:33 +03:00
|
|
|
|
2021-06-30 14:47:39 +03:00
|
|
|
|
2022-01-10 16:40:33 +03:00
|
|
|
def deprecated1_new_function():
|
|
|
|
print("deprecated1_new_function")
|
|
|
|
def deprecated1_old_function():
|
|
|
|
print("deprecated1_old_function")
|
|
|
|
warnings.warn("The 'deprecated1_old_function' is deprecated. Use `deprecated1_new_function` instead", DeprecationWarning, 2)
|
|
|
|
deprecated1_new_function()
|
2021-06-30 14:47:39 +03:00
|
|
|
|
2022-01-10 16:40:33 +03:00
|
|
|
def deprecated2_new_function():
|
|
|
|
print("deprecated2_new_function")
|
|
|
|
@util.deprecated(alternative=deprecated2_new_function)
|
|
|
|
def deprecated2_old_function():
|
|
|
|
print("deprecated2_old_function")
|
2015-08-19 23:15:40 +03:00
|
|
|
|
2015-08-19 23:10:33 +03:00
|
|
|
@pytest.mark.skipif(should_skip, reason="No environment variables configured")
|
|
|
|
class TestTeleBot:
|
|
|
|
def test_message_listener(self):
|
|
|
|
msg_list = []
|
|
|
|
for x in range(100):
|
|
|
|
msg_list.append(self.create_text_message('Message ' + str(x)))
|
|
|
|
|
|
|
|
def listener(messages):
|
|
|
|
assert len(messages) == 100
|
|
|
|
|
|
|
|
tb = telebot.TeleBot('')
|
|
|
|
tb.set_update_listener(listener)
|
|
|
|
|
|
|
|
def test_message_handler(self):
|
|
|
|
tb = telebot.TeleBot('')
|
|
|
|
msg = self.create_text_message('/help')
|
|
|
|
|
|
|
|
@tb.message_handler(commands=['help', 'start'])
|
|
|
|
def command_handler(message):
|
|
|
|
message.text = 'got'
|
|
|
|
|
|
|
|
tb.process_new_messages([msg])
|
|
|
|
time.sleep(1)
|
|
|
|
assert msg.text == 'got'
|
|
|
|
|
|
|
|
def test_message_handler_reg(self):
|
|
|
|
bot = telebot.TeleBot('')
|
|
|
|
msg = self.create_text_message(r'https://web.telegram.org/')
|
|
|
|
|
2021-01-09 21:22:49 +03:00
|
|
|
# noinspection PyUnusedLocal
|
2020-05-01 11:25:51 +03:00
|
|
|
@bot.message_handler(regexp=r'((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)')
|
2015-08-19 23:10:33 +03:00
|
|
|
def command_url(message):
|
|
|
|
msg.text = 'got'
|
|
|
|
|
|
|
|
bot.process_new_messages([msg])
|
|
|
|
time.sleep(1)
|
|
|
|
assert msg.text == 'got'
|
|
|
|
|
2015-09-10 16:38:09 +03:00
|
|
|
def test_message_handler_lambda(self):
|
|
|
|
bot = telebot.TeleBot('')
|
|
|
|
msg = self.create_text_message(r'lambda_text')
|
|
|
|
|
2021-01-09 21:22:49 +03:00
|
|
|
# noinspection PyUnusedLocal
|
2015-09-10 16:38:09 +03:00
|
|
|
@bot.message_handler(func=lambda message: r'lambda' in message.text)
|
|
|
|
def command_url(message):
|
|
|
|
msg.text = 'got'
|
|
|
|
|
|
|
|
bot.process_new_messages([msg])
|
|
|
|
time.sleep(1)
|
|
|
|
assert msg.text == 'got'
|
|
|
|
|
2015-09-10 16:44:14 +03:00
|
|
|
def test_message_handler_lambda_fail(self):
|
|
|
|
bot = telebot.TeleBot('')
|
|
|
|
msg = self.create_text_message(r'text')
|
|
|
|
|
2021-01-09 21:22:49 +03:00
|
|
|
# noinspection PyUnusedLocal
|
2015-09-10 16:44:14 +03:00
|
|
|
@bot.message_handler(func=lambda message: r'lambda' in message.text)
|
|
|
|
def command_url(message):
|
|
|
|
msg.text = 'got'
|
|
|
|
|
|
|
|
bot.process_new_messages([msg])
|
|
|
|
time.sleep(1)
|
|
|
|
assert not msg.text == 'got'
|
2015-09-10 16:38:09 +03:00
|
|
|
|
2015-08-19 23:10:33 +03:00
|
|
|
def test_message_handler_reg_fail(self):
|
|
|
|
bot = telebot.TeleBot('')
|
|
|
|
msg = self.create_text_message(r'web.telegram.org/')
|
|
|
|
|
2021-01-09 21:22:49 +03:00
|
|
|
# noinspection PyUnusedLocal
|
2020-05-01 11:25:51 +03:00
|
|
|
@bot.message_handler(regexp=r'((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)')
|
2015-08-19 23:10:33 +03:00
|
|
|
def command_url(message):
|
|
|
|
msg.text = 'got'
|
|
|
|
|
|
|
|
bot.process_new_messages([msg])
|
|
|
|
time.sleep(1)
|
|
|
|
assert not msg.text == 'got'
|
|
|
|
|
2015-09-09 04:43:19 +03:00
|
|
|
def test_send_message_with_markdown(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
markdown = """
|
|
|
|
*bold text*
|
|
|
|
_italic text_
|
|
|
|
[text](URL)
|
|
|
|
"""
|
|
|
|
ret_msg = tb.send_message(CHAT_ID, markdown, parse_mode="Markdown")
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2016-02-27 06:36:53 +03:00
|
|
|
def test_send_message_with_disable_notification(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
markdown = """
|
|
|
|
*bold text*
|
|
|
|
_italic text_
|
|
|
|
[text](URL)
|
|
|
|
"""
|
|
|
|
ret_msg = tb.send_message(CHAT_ID, markdown, parse_mode="Markdown", disable_notification=True)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2015-08-19 23:10:33 +03:00
|
|
|
def test_send_file(self):
|
|
|
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_document(CHAT_ID, file_data)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2015-08-31 12:46:18 +03:00
|
|
|
ret_msg = tb.send_document(CHAT_ID, ret_msg.document.file_id)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2021-06-27 11:58:33 +03:00
|
|
|
def test_send_file_with_filename(self):
|
|
|
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
|
|
|
|
ret_msg = tb.send_document(CHAT_ID, file_data)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2021-06-29 13:30:01 +03:00
|
|
|
ret_msg = tb.send_document(CHAT_ID, file_data, visible_file_name="test.jpg")
|
2021-06-27 11:58:33 +03:00
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2016-02-27 06:36:53 +03:00
|
|
|
def test_send_file_dis_noti(self):
|
|
|
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_document(CHAT_ID, file_data, disable_notification=True)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
|
|
|
ret_msg = tb.send_document(CHAT_ID, ret_msg.document.file_id)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2016-06-02 08:15:22 +03:00
|
|
|
def test_send_file_caption(self):
|
|
|
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_document(CHAT_ID, file_data, caption="Test")
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
|
|
|
ret_msg = tb.send_document(CHAT_ID, ret_msg.document.file_id)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2015-08-19 23:10:33 +03:00
|
|
|
def test_send_video(self):
|
|
|
|
file_data = open('./test_data/test_video.mp4', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_video(CHAT_ID, file_data)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2016-02-27 06:36:53 +03:00
|
|
|
def test_send_video_dis_noti(self):
|
|
|
|
file_data = open('./test_data/test_video.mp4', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_video(CHAT_ID, file_data, disable_notification=True)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2015-08-19 23:10:33 +03:00
|
|
|
def test_send_video_more_params(self):
|
|
|
|
file_data = open('./test_data/test_video.mp4', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_video(CHAT_ID, file_data, 1)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2016-02-27 06:36:53 +03:00
|
|
|
def test_send_video_more_params_dis_noti(self):
|
|
|
|
file_data = open('./test_data/test_video.mp4', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_video(CHAT_ID, file_data, 1, disable_notification=True)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2015-08-19 23:10:33 +03:00
|
|
|
def test_send_file_exception(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
try:
|
|
|
|
tb.send_document(CHAT_ID, None)
|
|
|
|
assert False
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
assert True
|
|
|
|
|
|
|
|
def test_send_photo(self):
|
|
|
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_photo(CHAT_ID, file_data)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2015-08-31 12:46:18 +03:00
|
|
|
ret_msg = tb.send_photo(CHAT_ID, ret_msg.photo[0].file_id)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2016-02-27 06:36:53 +03:00
|
|
|
def test_send_photo_dis_noti(self):
|
|
|
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_photo(CHAT_ID, file_data)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
|
|
|
ret_msg = tb.send_photo(CHAT_ID, ret_msg.photo[0].file_id, disable_notification=True)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2015-08-19 23:10:33 +03:00
|
|
|
def test_send_audio(self):
|
|
|
|
file_data = open('./test_data/record.mp3', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
2022-01-10 16:49:49 +03:00
|
|
|
ret_msg = tb.send_audio(CHAT_ID, file_data, duration = 1, performer='eternnoir', title='pyTelegram')
|
2015-08-19 23:10:33 +03:00
|
|
|
assert ret_msg.content_type == 'audio'
|
|
|
|
assert ret_msg.audio.performer == 'eternnoir'
|
|
|
|
assert ret_msg.audio.title == 'pyTelegram'
|
|
|
|
|
2016-02-27 06:36:53 +03:00
|
|
|
def test_send_audio_dis_noti(self):
|
|
|
|
file_data = open('./test_data/record.mp3', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
2022-01-10 16:49:49 +03:00
|
|
|
ret_msg = tb.send_audio(CHAT_ID, file_data, duration = 1, performer='eternnoir', title='pyTelegram',
|
2017-11-14 19:42:27 +03:00
|
|
|
disable_notification=True)
|
2016-02-27 06:36:53 +03:00
|
|
|
assert ret_msg.content_type == 'audio'
|
|
|
|
assert ret_msg.audio.performer == 'eternnoir'
|
|
|
|
assert ret_msg.audio.title == 'pyTelegram'
|
|
|
|
|
2015-08-19 23:10:33 +03:00
|
|
|
def test_send_voice(self):
|
|
|
|
file_data = open('./test_data/record.ogg', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_voice(CHAT_ID, file_data)
|
|
|
|
assert ret_msg.voice.mime_type == 'audio/ogg'
|
|
|
|
|
2016-02-27 06:36:53 +03:00
|
|
|
def test_send_voice_dis_noti(self):
|
|
|
|
file_data = open('./test_data/record.ogg', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_voice(CHAT_ID, file_data, disable_notification=True)
|
|
|
|
assert ret_msg.voice.mime_type == 'audio/ogg'
|
|
|
|
|
2015-09-18 21:31:29 +03:00
|
|
|
def test_get_file(self):
|
2015-09-18 22:11:56 +03:00
|
|
|
file_data = open('./test_data/record.ogg', 'rb')
|
2015-09-18 21:31:29 +03:00
|
|
|
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
|
|
|
|
|
2016-02-27 06:36:53 +03:00
|
|
|
def test_get_file_dis_noti(self):
|
|
|
|
file_data = open('./test_data/record.ogg', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_voice(CHAT_ID, file_data, disable_notification=True)
|
|
|
|
file_id = ret_msg.voice.file_id
|
|
|
|
file_info = tb.get_file(file_id)
|
|
|
|
assert file_info.file_id == file_id
|
|
|
|
|
2015-08-19 23:10:33 +03:00
|
|
|
def test_send_message(self):
|
|
|
|
text = 'CI Test Message'
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_message(CHAT_ID, text)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2020-04-15 08:10:05 +03:00
|
|
|
def test_send_dice(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
2020-04-27 08:30:05 +03:00
|
|
|
ret_msg = tb.send_dice(CHAT_ID, emoji='🎯')
|
2020-04-15 08:10:05 +03:00
|
|
|
assert ret_msg.message_id
|
|
|
|
assert ret_msg.content_type == 'dice'
|
|
|
|
|
2016-02-27 06:36:53 +03:00
|
|
|
def test_send_message_dis_noti(self):
|
|
|
|
text = 'CI Test Message'
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_message(CHAT_ID, text, disable_notification=True)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2016-04-14 08:35:18 +03:00
|
|
|
def test_send_message_with_markup(self):
|
|
|
|
text = 'CI Test Message'
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
markup = types.ReplyKeyboardMarkup()
|
|
|
|
markup.add(types.KeyboardButton("1"))
|
|
|
|
markup.add(types.KeyboardButton("2"))
|
|
|
|
ret_msg = tb.send_message(CHAT_ID, text, disable_notification=True, reply_markup=markup)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2016-05-03 16:05:11 +03:00
|
|
|
def test_send_message_with_markup_use_string(self):
|
|
|
|
text = 'CI Test Message'
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
markup = types.ReplyKeyboardMarkup()
|
|
|
|
markup.add("1")
|
|
|
|
markup.add("2")
|
|
|
|
markup.add("3")
|
|
|
|
markup.add("4")
|
|
|
|
ret_msg = tb.send_message(CHAT_ID, text, disable_notification=True, reply_markup=markup)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2016-04-14 08:35:18 +03:00
|
|
|
def test_send_message_with_inlinemarkup(self):
|
|
|
|
text = 'CI Test Message'
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
markup = types.InlineKeyboardMarkup()
|
|
|
|
markup.add(types.InlineKeyboardButton("Google", url="http://www.google.com"))
|
|
|
|
markup.add(types.InlineKeyboardButton("Yahoo", url="http://www.yahoo.com"))
|
|
|
|
ret_msg = tb.send_message(CHAT_ID, text, disable_notification=True, reply_markup=markup)
|
|
|
|
assert ret_msg.message_id
|
|
|
|
|
2015-08-19 23:10:33 +03:00
|
|
|
def test_forward_message(self):
|
|
|
|
text = 'CI forward_message Test Message'
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
msg = tb.send_message(CHAT_ID, text)
|
|
|
|
ret_msg = tb.forward_message(CHAT_ID, CHAT_ID, msg.message_id)
|
|
|
|
assert ret_msg.forward_from
|
|
|
|
|
2021-01-12 10:47:53 +03:00
|
|
|
def test_copy_message(self):
|
|
|
|
text = 'CI copy_message Test Message'
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
msg = tb.send_message(CHAT_ID, text)
|
|
|
|
ret_msg = tb.copy_message(CHAT_ID, CHAT_ID, msg.message_id)
|
|
|
|
assert ret_msg
|
|
|
|
|
2016-02-27 06:36:53 +03:00
|
|
|
def test_forward_message_dis_noti(self):
|
|
|
|
text = 'CI forward_message Test Message'
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
msg = tb.send_message(CHAT_ID, text)
|
|
|
|
ret_msg = tb.forward_message(CHAT_ID, CHAT_ID, msg.message_id, disable_notification=True)
|
|
|
|
assert ret_msg.forward_from
|
|
|
|
|
2015-08-19 23:10:33 +03:00
|
|
|
def test_reply_to(self):
|
|
|
|
text = 'CI reply_to Test Message'
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
msg = tb.send_message(CHAT_ID, text)
|
|
|
|
ret_msg = tb.reply_to(msg, text + ' REPLY')
|
|
|
|
assert ret_msg.reply_to_message.message_id == msg.message_id
|
|
|
|
|
|
|
|
def test_register_for_reply(self):
|
|
|
|
text = 'CI reply_to Test Message'
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
msg = tb.send_message(CHAT_ID, text, reply_markup=types.ForceReply())
|
|
|
|
reply_msg = tb.reply_to(msg, text + ' REPLY')
|
|
|
|
|
|
|
|
def process_reply(message):
|
|
|
|
assert msg.message_id == message.reply_to_message.message_id
|
|
|
|
|
|
|
|
tb.register_for_reply(msg, process_reply)
|
|
|
|
|
|
|
|
tb.process_new_messages([reply_msg])
|
|
|
|
|
|
|
|
def test_send_location(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
lat = 26.3875591
|
|
|
|
lon = -161.2901042
|
|
|
|
ret_msg = tb.send_location(CHAT_ID, lat, lon)
|
|
|
|
assert int(ret_msg.location.longitude) == int(lon)
|
|
|
|
assert int(ret_msg.location.latitude) == int(lat)
|
|
|
|
|
2016-02-27 06:36:53 +03:00
|
|
|
def test_send_location_dis_noti(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
lat = 26.3875591
|
|
|
|
lon = -161.2901042
|
|
|
|
ret_msg = tb.send_location(CHAT_ID, lat, lon, disable_notification=True)
|
|
|
|
assert int(ret_msg.location.longitude) == int(lon)
|
|
|
|
assert int(ret_msg.location.latitude) == int(lat)
|
|
|
|
|
2016-04-14 08:55:28 +03:00
|
|
|
def test_send_venue(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
lat = 26.3875591
|
|
|
|
lon = -161.2901042
|
|
|
|
ret_msg = tb.send_venue(CHAT_ID, lat, lon, "Test Venue", "1123 Test Venue address")
|
|
|
|
assert ret_msg.venue.title == "Test Venue"
|
2016-09-17 02:55:25 +03:00
|
|
|
assert int(lat) == int(ret_msg.venue.location.latitude)
|
2016-04-14 08:55:28 +03:00
|
|
|
|
|
|
|
def test_send_venue_dis_noti(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
lat = 26.3875591
|
|
|
|
lon = -161.2901042
|
2016-04-14 10:03:07 +03:00
|
|
|
ret_msg = tb.send_venue(CHAT_ID, lat, lon, "Test Venue", "1123 Test Venue address", disable_notification=True)
|
2016-04-14 08:55:28 +03:00
|
|
|
assert ret_msg.venue.title == "Test Venue"
|
|
|
|
|
2015-10-12 05:46:20 +03:00
|
|
|
def test_Chat(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
me = tb.get_me()
|
|
|
|
msg = tb.send_message(CHAT_ID, 'Test')
|
|
|
|
assert me.id == msg.from_user.id
|
2015-10-12 05:51:29 +03:00
|
|
|
assert msg.chat.id == int(CHAT_ID)
|
2015-10-12 05:46:20 +03:00
|
|
|
|
2016-04-14 10:03:07 +03:00
|
|
|
def test_edit_message_text(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
msg = tb.send_message(CHAT_ID, 'Test')
|
2016-04-14 10:06:46 +03:00
|
|
|
new_msg = tb.edit_message_text('Edit test', chat_id=CHAT_ID, message_id=msg.message_id)
|
2016-04-14 10:03:07 +03:00
|
|
|
assert new_msg.text == 'Edit test'
|
|
|
|
|
2016-08-29 15:48:09 +03:00
|
|
|
def test_edit_message_caption(self):
|
|
|
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
2016-08-29 15:21:56 +03:00
|
|
|
tb = telebot.TeleBot(TOKEN)
|
2016-08-29 15:48:09 +03:00
|
|
|
msg = tb.send_document(CHAT_ID, file_data, caption="Test")
|
|
|
|
new_msg = tb.edit_message_caption(caption='Edit test', chat_id=CHAT_ID, message_id=msg.message_id)
|
|
|
|
assert new_msg.caption == 'Edit test'
|
2016-08-29 15:21:56 +03:00
|
|
|
|
2018-08-09 19:10:01 +03:00
|
|
|
def test_edit_message_media(self):
|
|
|
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
|
|
|
file_data_2 = open('../examples/detailed_example/rooster.jpg', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
msg = tb.send_photo(CHAT_ID, file_data)
|
2018-08-10 16:47:59 +03:00
|
|
|
new_msg = tb.edit_message_media(chat_id=CHAT_ID, message_id=msg.message_id,
|
|
|
|
media=types.InputMediaPhoto(file_data_2, caption='Test editMessageMedia 0'))
|
2018-08-09 19:10:01 +03:00
|
|
|
assert type(new_msg) != bool
|
|
|
|
|
2018-08-10 16:47:59 +03:00
|
|
|
new_msg = tb.edit_message_media(chat_id=CHAT_ID, message_id=msg.message_id,
|
|
|
|
media=types.InputMediaPhoto(msg.photo[0].file_id, caption='Test editMessageMedia'))
|
2018-08-09 19:10:01 +03:00
|
|
|
assert type(new_msg) != bool
|
2018-08-10 16:47:59 +03:00
|
|
|
assert new_msg.caption == 'Test editMessageMedia'
|
2018-08-09 19:10:01 +03:00
|
|
|
|
2016-06-07 14:16:17 +03:00
|
|
|
def test_get_chat(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ch = tb.get_chat(GROUP_ID)
|
2016-06-07 14:34:46 +03:00
|
|
|
assert str(ch.id) == GROUP_ID
|
2016-06-07 14:16:17 +03:00
|
|
|
|
|
|
|
def test_get_chat_administrators(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
cas = tb.get_chat_administrators(GROUP_ID)
|
2016-06-07 14:32:12 +03:00
|
|
|
assert len(cas) > 0
|
2016-06-07 14:16:17 +03:00
|
|
|
|
|
|
|
def test_get_chat_members_count(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
cn = tb.get_chat_members_count(GROUP_ID)
|
|
|
|
assert cn > 1
|
|
|
|
|
2021-06-19 18:59:55 +03:00
|
|
|
def test_export_chat_invite_link(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
il = tb.export_chat_invite_link(GROUP_ID)
|
|
|
|
assert isinstance(il, str)
|
|
|
|
|
|
|
|
def test_create_revoke_detailed_chat_invite_link(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
2022-01-10 16:49:49 +03:00
|
|
|
cil = tb.create_chat_invite_link(
|
|
|
|
GROUP_ID,
|
|
|
|
expire_date = datetime.now() + timedelta(minutes=1),
|
|
|
|
member_limit=5)
|
2021-06-19 18:59:55 +03:00
|
|
|
assert isinstance(cil.invite_link, str)
|
|
|
|
assert cil.creator.id == tb.get_me().id
|
|
|
|
assert isinstance(cil.expire_date, (float, int))
|
|
|
|
assert cil.member_limit == 5
|
|
|
|
assert not cil.is_revoked
|
|
|
|
rcil = tb.revoke_chat_invite_link(GROUP_ID, cil.invite_link)
|
|
|
|
assert rcil.is_revoked
|
|
|
|
|
2016-04-14 10:17:53 +03:00
|
|
|
def test_edit_markup(self):
|
|
|
|
text = 'CI Test Message'
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
markup = types.InlineKeyboardMarkup()
|
|
|
|
markup.add(types.InlineKeyboardButton("Google", url="http://www.google.com"))
|
|
|
|
markup.add(types.InlineKeyboardButton("Yahoo", url="http://www.yahoo.com"))
|
|
|
|
ret_msg = tb.send_message(CHAT_ID, text, disable_notification=True, reply_markup=markup)
|
|
|
|
markup.add(types.InlineKeyboardButton("Google2", url="http://www.google.com"))
|
|
|
|
markup.add(types.InlineKeyboardButton("Yahoo2", url="http://www.yahoo.com"))
|
2016-04-23 04:29:48 +03:00
|
|
|
new_msg = tb.edit_message_reply_markup(chat_id=CHAT_ID, message_id=ret_msg.message_id, reply_markup=markup)
|
2016-04-14 10:17:53 +03:00
|
|
|
assert new_msg.message_id
|
|
|
|
|
2021-11-03 17:30:10 +03:00
|
|
|
def test_antiflood(self):
|
2022-01-10 18:19:21 +03:00
|
|
|
text = "Testing antiflood function"
|
2021-11-03 17:30:10 +03:00
|
|
|
tb = telebot.TeleBot(TOKEN)
|
2022-01-10 16:49:49 +03:00
|
|
|
i = -1
|
2022-01-10 18:19:21 +03:00
|
|
|
for i in range(0,200):
|
2021-11-03 17:30:10 +03:00
|
|
|
util.antiflood(tb.send_message, CHAT_ID, text)
|
2022-01-10 18:19:21 +03:00
|
|
|
assert i == 199
|
2021-11-03 17:30:10 +03:00
|
|
|
|
2018-08-17 12:49:37 +03:00
|
|
|
@staticmethod
|
|
|
|
def create_text_message(text):
|
2015-08-19 23:10:33 +03:00
|
|
|
params = {'text': text}
|
2017-08-28 07:13:26 +03:00
|
|
|
chat = types.User(11, False, 'test')
|
2018-04-10 09:48:39 +03:00
|
|
|
return types.Message(1, None, None, chat, 'text', params, "")
|
2015-08-19 23:10:33 +03:00
|
|
|
|
2020-04-08 21:13:19 +03:00
|
|
|
@staticmethod
|
|
|
|
def create_message_update(text):
|
|
|
|
params = {'text': text}
|
|
|
|
chat = types.User(11, False, 'test')
|
|
|
|
message = types.Message(1, None, None, chat, 'text', params, "")
|
|
|
|
edited_message = None
|
|
|
|
channel_post = None
|
|
|
|
edited_channel_post = None
|
|
|
|
inline_query = None
|
|
|
|
chosen_inline_result = None
|
|
|
|
callback_query = None
|
|
|
|
shipping_query = None
|
|
|
|
pre_checkout_query = None
|
|
|
|
poll = None
|
2020-05-12 20:29:36 +03:00
|
|
|
poll_answer = None
|
2021-06-19 21:13:53 +03:00
|
|
|
my_chat_member = None
|
|
|
|
chat_member = None
|
2021-11-06 10:27:19 +03:00
|
|
|
chat_join_request = None
|
2020-04-08 21:13:19 +03:00
|
|
|
return types.Update(-1001234038283, message, edited_message, channel_post, edited_channel_post, inline_query,
|
2021-06-19 21:13:53 +03:00
|
|
|
chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll, poll_answer,
|
2021-11-06 10:27:19 +03:00
|
|
|
my_chat_member, chat_member, chat_join_request)
|
2020-04-08 21:13:19 +03:00
|
|
|
|
2015-08-19 23:10:33 +03:00
|
|
|
def test_is_string_unicode(self):
|
|
|
|
s1 = u'string'
|
2015-08-31 12:46:18 +03:00
|
|
|
assert util.is_string(s1)
|
2015-08-19 23:10:33 +03:00
|
|
|
|
|
|
|
def test_is_string_string(self):
|
|
|
|
s1 = 'string'
|
2015-08-31 12:46:18 +03:00
|
|
|
assert util.is_string(s1)
|
2015-08-19 23:10:33 +03:00
|
|
|
|
|
|
|
def test_not_string(self):
|
|
|
|
i1 = 10
|
2015-08-31 12:46:18 +03:00
|
|
|
assert not util.is_string(i1)
|
2017-05-21 14:58:00 +03:00
|
|
|
|
|
|
|
def test_send_video_note(self):
|
|
|
|
file_data = open('./test_data/test_video.mp4', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_video_note(CHAT_ID, file_data)
|
|
|
|
assert ret_msg.message_id
|
2017-11-29 08:59:47 +03:00
|
|
|
|
|
|
|
def test_send_media_group(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
img1 = 'https://i.imgur.com/CjXjcnU.png'
|
|
|
|
img2 = 'https://i.imgur.com/CjXjcnU.png'
|
|
|
|
medias = [types.InputMediaPhoto(img1, "View"), types.InputMediaPhoto(img2, "Dog")]
|
|
|
|
result = tb.send_media_group(CHAT_ID, medias)
|
|
|
|
assert len(result) == 2
|
2018-01-24 14:05:38 +03:00
|
|
|
assert result[0].media_group_id is not None
|
|
|
|
assert result[0].media_group_id == result[1].media_group_id
|
2018-02-14 23:27:55 +03:00
|
|
|
|
|
|
|
def test_send_media_group_local_files(self):
|
|
|
|
photo = open('../examples/detailed_example/kitten.jpg', 'rb')
|
|
|
|
video = open('./test_data/test_video.mp4', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
medias = [types.InputMediaPhoto(photo, "View"),
|
|
|
|
types.InputMediaVideo(video)]
|
|
|
|
result = tb.send_media_group(CHAT_ID, medias)
|
|
|
|
assert len(result) == 2
|
|
|
|
assert result[0].media_group_id is not None
|
|
|
|
assert result[1].media_group_id is not None
|
|
|
|
|
|
|
|
def test_send_photo_formating_caption(self):
|
|
|
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_photo(CHAT_ID, file_data, caption='_italic_', parse_mode='Markdown')
|
|
|
|
assert ret_msg.caption_entities[0].type == 'italic'
|
|
|
|
|
|
|
|
def test_send_video_formatting_caption(self):
|
|
|
|
file_data = open('./test_data/test_video.mp4', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_video(CHAT_ID, file_data, caption='_italic_', parse_mode='Markdown')
|
|
|
|
assert ret_msg.caption_entities[0].type == 'italic'
|
|
|
|
|
|
|
|
def test_send_audio_formatting_caption(self):
|
|
|
|
file_data = open('./test_data/record.mp3', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_audio(CHAT_ID, file_data, caption='<b>bold</b>', parse_mode='HTML')
|
|
|
|
assert ret_msg.caption_entities[0].type == 'bold'
|
|
|
|
|
|
|
|
def test_send_voice_formatting_caprion(self):
|
|
|
|
file_data = open('./test_data/record.ogg', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_voice(CHAT_ID, file_data, caption='<b>bold</b>', parse_mode='HTML')
|
|
|
|
assert ret_msg.caption_entities[0].type == 'bold'
|
|
|
|
assert ret_msg.voice.mime_type == 'audio/ogg'
|
|
|
|
|
|
|
|
def test_send_media_group_formatting_caption(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
img1 = 'https://i.imgur.com/CjXjcnU.png'
|
|
|
|
img2 = 'https://i.imgur.com/CjXjcnU.png'
|
|
|
|
medias = [types.InputMediaPhoto(img1, "*View*", parse_mode='Markdown'),
|
|
|
|
types.InputMediaPhoto(img2, "_Dog_", parse_mode='Markdown')]
|
|
|
|
result = tb.send_media_group(CHAT_ID, medias)
|
|
|
|
assert len(result) == 2
|
|
|
|
assert result[0].media_group_id is not None
|
|
|
|
assert result[0].caption_entities[0].type == 'bold'
|
2018-02-16 18:29:29 +03:00
|
|
|
assert result[1].caption_entities[0].type == 'italic'
|
|
|
|
|
|
|
|
def test_send_document_formating_caption(self):
|
|
|
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
ret_msg = tb.send_document(CHAT_ID, file_data, caption='_italic_', parse_mode='Markdown')
|
|
|
|
assert ret_msg.caption_entities[0].type == 'italic'
|
2020-04-08 21:13:19 +03:00
|
|
|
|
2021-06-26 14:36:14 +03:00
|
|
|
def test_chat_commands(self):
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
command, description, lang = 'command_1', 'description of command 1', 'en'
|
|
|
|
scope = telebot.types.BotCommandScopeChat(CHAT_ID)
|
|
|
|
ret_msg = tb.set_my_commands([telebot.types.BotCommand(command, description)], scope, lang)
|
|
|
|
assert ret_msg is True
|
|
|
|
|
2022-01-10 16:49:49 +03:00
|
|
|
ret_msg = tb.get_my_commands(scope = scope, language_code = lang)
|
2021-06-26 14:36:14 +03:00
|
|
|
assert ret_msg[0].command == command
|
|
|
|
assert ret_msg[0].description == description
|
|
|
|
|
2022-01-10 16:49:49 +03:00
|
|
|
ret_msg = tb.delete_my_commands(scope = scope, language_code = lang)
|
2021-06-26 14:36:14 +03:00
|
|
|
assert ret_msg is True
|
|
|
|
|
2022-01-10 16:49:49 +03:00
|
|
|
ret_msg = tb.get_my_commands(scope = scope, language_code = lang)
|
2021-06-26 14:36:14 +03:00
|
|
|
assert ret_msg == []
|
|
|
|
|
|
|
|
|
2020-04-08 21:13:19 +03:00
|
|
|
def test_typed_middleware_handler(self):
|
2020-04-11 11:02:50 +03:00
|
|
|
from telebot import apihelper
|
|
|
|
|
|
|
|
apihelper.ENABLE_MIDDLEWARE = True
|
|
|
|
|
2020-04-08 21:13:19 +03:00
|
|
|
tb = telebot.TeleBot('')
|
|
|
|
update = self.create_message_update('/help')
|
|
|
|
|
2021-01-09 21:22:49 +03:00
|
|
|
# noinspection PyUnusedLocal
|
2020-04-08 21:13:19 +03:00
|
|
|
@tb.middleware_handler(update_types=['message'])
|
|
|
|
def middleware(tb_instance, message):
|
|
|
|
message.text = 'got'
|
|
|
|
|
|
|
|
@tb.message_handler(func=lambda m: m.text == 'got')
|
|
|
|
def command_handler(message):
|
|
|
|
message.text = message.text + message.text
|
|
|
|
|
|
|
|
tb.process_new_updates([update])
|
|
|
|
time.sleep(1)
|
|
|
|
assert update.message.text == 'got' * 2
|
|
|
|
|
|
|
|
def test_default_middleware_handler(self):
|
2020-04-11 11:02:50 +03:00
|
|
|
from telebot import apihelper
|
|
|
|
|
|
|
|
apihelper.ENABLE_MIDDLEWARE = True
|
|
|
|
|
2020-04-08 21:13:19 +03:00
|
|
|
tb = telebot.TeleBot('')
|
|
|
|
update = self.create_message_update('/help')
|
|
|
|
|
2021-01-09 21:22:49 +03:00
|
|
|
# noinspection PyUnusedLocal
|
2020-04-08 21:13:19 +03:00
|
|
|
@tb.middleware_handler()
|
2021-01-09 21:22:49 +03:00
|
|
|
def middleware(tb_instance, mw_update):
|
|
|
|
mw_update.message.text = 'got'
|
2020-04-08 21:13:19 +03:00
|
|
|
|
|
|
|
@tb.message_handler(func=lambda m: m.text == 'got')
|
|
|
|
def command_handler(message):
|
|
|
|
message.text = message.text + message.text
|
|
|
|
|
|
|
|
tb.process_new_updates([update])
|
|
|
|
time.sleep(1)
|
|
|
|
assert update.message.text == 'got' * 2
|
2021-06-30 14:47:39 +03:00
|
|
|
|
|
|
|
def test_deprecated_dec(self):
|
2022-01-10 16:40:33 +03:00
|
|
|
deprecated1_old_function()
|
|
|
|
deprecated2_old_function()
|
2020-05-20 11:57:41 +03:00
|
|
|
|
|
|
|
def test_chat_permissions(self):
|
2020-05-29 12:37:23 +03:00
|
|
|
return # CHAT_ID is private chat, no permissions can be set
|
2021-01-09 21:22:49 +03:00
|
|
|
#tb = telebot.TeleBot(TOKEN)
|
|
|
|
#permissions = types.ChatPermissions(can_send_messages=True, can_send_polls=False)
|
|
|
|
#msg = tb.set_chat_permissions(CHAT_ID, permissions)
|