mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Fixing and upgrading next step and reply handlers. + minor fixes
Rename telebot package to pytelegrambotapi becouse lib named telebot exists and it raising many errors Add methods: | register_for_reply_by_message_id, | register_next_step_handler_by_chat_id, | clear_reply_handlers, | clear_reply_handlers_by_message_id
This commit is contained in:
parent
373d4d37ff
commit
7957bc45a8
@ -30,10 +30,10 @@
|
|||||||
# Steps 1 to 4 will have to be implemented in a web server, using a language such as PHP, Python, C# or Java. These
|
# Steps 1 to 4 will have to be implemented in a web server, using a language such as PHP, Python, C# or Java. These
|
||||||
# steps are not shown here. Only steps 5 to 7 are illustrated, some in pseudo-code, with this example.
|
# steps are not shown here. Only steps 5 to 7 are illustrated, some in pseudo-code, with this example.
|
||||||
|
|
||||||
import telebot
|
import pytelegrambotapi
|
||||||
import time
|
import time
|
||||||
|
|
||||||
bot = telebot.TeleBot('TOKEN')
|
bot = pytelegrambotapi.TeleBot('TOKEN')
|
||||||
|
|
||||||
def extract_unique_code(text):
|
def extract_unique_code(text):
|
||||||
# Extracts the unique_code from the sent /start command.
|
# Extracts the unique_code from the sent /start command.
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
This is a detailed example using almost every command of the API
|
This is a detailed example using almost every command of the API
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import telebot
|
import pytelegrambotapi
|
||||||
from telebot import types
|
from pytelegrambotapi import types
|
||||||
import time
|
import time
|
||||||
|
|
||||||
TOKEN = '<token_string>'
|
TOKEN = '<token_string>'
|
||||||
@ -48,7 +48,7 @@ def listener(messages):
|
|||||||
print str(m.chat.first_name) + " [" + str(m.chat.id) + "]: " + m.text
|
print str(m.chat.first_name) + " [" + str(m.chat.id) + "]: " + m.text
|
||||||
|
|
||||||
|
|
||||||
bot = telebot.TeleBot(TOKEN)
|
bot = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
bot.set_update_listener(listener) # register listener
|
bot.set_update_listener(listener) # register listener
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
# This is a simple echo bot using the decorator mechanism.
|
# This is a simple echo bot using the decorator mechanism.
|
||||||
# It echoes any incoming text messages.
|
# It echoes any incoming text messages.
|
||||||
|
|
||||||
import telebot
|
import pytelegrambotapi
|
||||||
|
|
||||||
API_TOKEN = '<api_token>'
|
API_TOKEN = '<api_token>'
|
||||||
|
|
||||||
bot = telebot.TeleBot(API_TOKEN)
|
bot = pytelegrambotapi.TeleBot(API_TOKEN)
|
||||||
|
|
||||||
# Handle '/start' and '/help'
|
# Handle '/start' and '/help'
|
||||||
@bot.message_handler(commands=['help', 'start'])
|
@bot.message_handler(commands=['help', 'start'])
|
||||||
|
@ -4,12 +4,12 @@ This Example will show you how to use register_next_step handler.
|
|||||||
"""
|
"""
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import telebot
|
import pytelegrambotapi
|
||||||
from telebot import types
|
from pytelegrambotapi import types
|
||||||
|
|
||||||
API_TOKEN = '<api_token>'
|
API_TOKEN = '<api_token>'
|
||||||
|
|
||||||
bot = telebot.TeleBot(API_TOKEN)
|
bot = pytelegrambotapi.TeleBot(API_TOKEN)
|
||||||
|
|
||||||
user_dict = {}
|
user_dict = {}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
# and goes by the name 'TeleBot (@pyTeleBot)'. Join our group to talk to him!
|
# and goes by the name 'TeleBot (@pyTeleBot)'. Join our group to talk to him!
|
||||||
# WARNING: Tested with Python 2.7
|
# WARNING: Tested with Python 2.7
|
||||||
|
|
||||||
import telebot
|
import pytelegrambotapi
|
||||||
import os
|
import os
|
||||||
|
|
||||||
text_messages = {
|
text_messages = {
|
||||||
@ -30,7 +30,7 @@ text_messages = {
|
|||||||
if "TELEBOT_BOT_TOKEN" not in os.environ or "GROUP_CHAT_ID" not in os.environ:
|
if "TELEBOT_BOT_TOKEN" not in os.environ or "GROUP_CHAT_ID" not in os.environ:
|
||||||
raise AssertionError("Please configure TELEBOT_BOT_TOKEN and GROUP_CHAT_ID as environment variables")
|
raise AssertionError("Please configure TELEBOT_BOT_TOKEN and GROUP_CHAT_ID as environment variables")
|
||||||
|
|
||||||
bot = telebot.AsyncTeleBot(os.environ["TELEBOT_BOT_TOKEN"])
|
bot = pytelegrambotapi.AsyncTeleBot(os.environ["TELEBOT_BOT_TOKEN"])
|
||||||
GROUP_CHAT_ID = int(os.environ["GROUP_CHAT_ID"])
|
GROUP_CHAT_ID = int(os.environ["GROUP_CHAT_ID"])
|
||||||
|
|
||||||
def is_api_group(chat_id):
|
def is_api_group(chat_id):
|
||||||
|
@ -9,7 +9,7 @@ import ssl
|
|||||||
|
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
|
|
||||||
import telebot
|
import pytelegrambotapi
|
||||||
|
|
||||||
|
|
||||||
API_TOKEN = '<api_token>'
|
API_TOKEN = '<api_token>'
|
||||||
@ -33,10 +33,10 @@ WEBHOOK_URL_BASE = "https://{}:{}".format(WEBHOOK_HOST, WEBHOOK_PORT)
|
|||||||
WEBHOOK_URL_PATH = "/{}/".format(API_TOKEN)
|
WEBHOOK_URL_PATH = "/{}/".format(API_TOKEN)
|
||||||
|
|
||||||
|
|
||||||
logger = telebot.logger
|
logger = pytelegrambotapi.logger
|
||||||
telebot.logger.setLevel(logging.INFO)
|
pytelegrambotapi.logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
bot = telebot.TeleBot(API_TOKEN)
|
bot = pytelegrambotapi.TeleBot(API_TOKEN)
|
||||||
|
|
||||||
app = web.Application()
|
app = web.Application()
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ app = web.Application()
|
|||||||
async def handle(request):
|
async def handle(request):
|
||||||
if request.match_info.get('token') == bot.token:
|
if request.match_info.get('token') == bot.token:
|
||||||
request_body_dict = await request.json()
|
request_body_dict = await request.json()
|
||||||
update = telebot.types.Update.de_json(request_body_dict)
|
update = pytelegrambotapi.types.Update.de_json(request_body_dict)
|
||||||
bot.process_new_updates([update])
|
bot.process_new_updates([update])
|
||||||
return web.Response()
|
return web.Response()
|
||||||
else:
|
else:
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
# It echoes any incoming text messages and does not use the polling method.
|
# It echoes any incoming text messages and does not use the polling method.
|
||||||
|
|
||||||
import cherrypy
|
import cherrypy
|
||||||
import telebot
|
import pytelegrambotapi
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
@ -30,10 +30,10 @@ WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
|
|||||||
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)
|
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)
|
||||||
|
|
||||||
|
|
||||||
logger = telebot.logger
|
logger = pytelegrambotapi.logger
|
||||||
telebot.logger.setLevel(logging.INFO)
|
pytelegrambotapi.logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
bot = telebot.TeleBot(API_TOKEN)
|
bot = pytelegrambotapi.TeleBot(API_TOKEN)
|
||||||
|
|
||||||
|
|
||||||
# WebhookServer, process webhook calls
|
# WebhookServer, process webhook calls
|
||||||
@ -45,7 +45,7 @@ class WebhookServer(object):
|
|||||||
cherrypy.request.headers['content-type'] == 'application/json':
|
cherrypy.request.headers['content-type'] == 'application/json':
|
||||||
length = int(cherrypy.request.headers['content-length'])
|
length = int(cherrypy.request.headers['content-length'])
|
||||||
json_string = cherrypy.request.body.read(length).decode("utf-8")
|
json_string = cherrypy.request.body.read(length).decode("utf-8")
|
||||||
update = telebot.types.Update.de_json(json_string)
|
update = pytelegrambotapi.types.Update.de_json(json_string)
|
||||||
bot.process_new_updates([update])
|
bot.process_new_updates([update])
|
||||||
return ''
|
return ''
|
||||||
else:
|
else:
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
import BaseHTTPServer
|
import BaseHTTPServer
|
||||||
import ssl
|
import ssl
|
||||||
import telebot
|
import pytelegrambotapi
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
@ -31,10 +31,10 @@ WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
|
|||||||
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)
|
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)
|
||||||
|
|
||||||
|
|
||||||
logger = telebot.logger
|
logger = pytelegrambotapi.logger
|
||||||
telebot.logger.setLevel(logging.INFO)
|
pytelegrambotapi.logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
bot = telebot.TeleBot(API_TOKEN)
|
bot = pytelegrambotapi.TeleBot(API_TOKEN)
|
||||||
|
|
||||||
|
|
||||||
# WebhookHandler, process webhook calls
|
# WebhookHandler, process webhook calls
|
||||||
@ -59,7 +59,7 @@ class WebhookHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
|
|
||||||
update = telebot.types.Update.de_json(json_string)
|
update = pytelegrambotapi.types.Update.de_json(json_string)
|
||||||
bot.process_new_messages([update.message])
|
bot.process_new_messages([update.message])
|
||||||
else:
|
else:
|
||||||
self.send_error(403)
|
self.send_error(403)
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
# It echoes any incoming text messages and does not use the polling method.
|
# It echoes any incoming text messages and does not use the polling method.
|
||||||
|
|
||||||
import flask
|
import flask
|
||||||
import telebot
|
import pytelegrambotapi
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
@ -30,10 +30,10 @@ WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
|
|||||||
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)
|
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)
|
||||||
|
|
||||||
|
|
||||||
logger = telebot.logger
|
logger = pytelegrambotapi.logger
|
||||||
telebot.logger.setLevel(logging.INFO)
|
pytelegrambotapi.logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
bot = telebot.TeleBot(API_TOKEN)
|
bot = pytelegrambotapi.TeleBot(API_TOKEN)
|
||||||
|
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
@ -49,7 +49,7 @@ def index():
|
|||||||
def webhook():
|
def webhook():
|
||||||
if flask.request.headers.get('content-type') == 'application/json':
|
if flask.request.headers.get('content-type') == 'application/json':
|
||||||
json_string = flask.request.get_data().decode('utf-8')
|
json_string = flask.request.get_data().decode('utf-8')
|
||||||
update = telebot.types.Update.de_json(json_string)
|
update = pytelegrambotapi.types.Update.de_json(json_string)
|
||||||
bot.process_new_updates([update])
|
bot.process_new_updates([update])
|
||||||
return ''
|
return ''
|
||||||
else:
|
else:
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
import telebot
|
import pytelegrambotapi
|
||||||
from flask import Flask, request
|
from flask import Flask, request
|
||||||
|
|
||||||
TOKEN = '<api_token>'
|
TOKEN = '<api_token>'
|
||||||
bot = telebot.TeleBot(TOKEN)
|
bot = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
server = Flask(__name__)
|
server = Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ def echo_message(message):
|
|||||||
|
|
||||||
@server.route('/' + TOKEN, methods=['POST'])
|
@server.route('/' + TOKEN, methods=['POST'])
|
||||||
def getMessage():
|
def getMessage():
|
||||||
bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
|
bot.process_new_updates([pytelegrambotapi.types.Update.de_json(request.stream.read().decode("utf-8"))])
|
||||||
return "!", 200
|
return "!", 200
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
# This example shows webhook echo bot with Tornado web framework
|
# This example shows webhook echo bot with Tornado web framework
|
||||||
# Documenation to Tornado: http://tornadoweb.org
|
# Documenation to Tornado: http://tornadoweb.org
|
||||||
|
|
||||||
import telebot
|
import pytelegrambotapi
|
||||||
import tornado.web
|
import tornado.web
|
||||||
import tornado.ioloop
|
import tornado.ioloop
|
||||||
import tornado.httpserver
|
import tornado.httpserver
|
||||||
@ -27,7 +27,7 @@ WEBHOOK_URL_BASE = "https://{0}:{1}/{2}".format(WEBHOOK_HOST, str(WEBHOOK_PORT),
|
|||||||
# When asked for "Common Name (e.g. server FQDN or YOUR name)" you should reply
|
# When asked for "Common Name (e.g. server FQDN or YOUR name)" you should reply
|
||||||
# with the same value in you put in WEBHOOK_HOST
|
# with the same value in you put in WEBHOOK_HOST
|
||||||
|
|
||||||
bot = telebot.TeleBot(API_TOKEN)
|
bot = pytelegrambotapi.TeleBot(API_TOKEN)
|
||||||
|
|
||||||
class Root(tornado.web.RequestHandler):
|
class Root(tornado.web.RequestHandler):
|
||||||
def get(self):
|
def get(self):
|
||||||
@ -45,7 +45,7 @@ class webhook_serv(tornado.web.RequestHandler):
|
|||||||
|
|
||||||
# length = int(self.request.headers['Content-Length'])
|
# length = int(self.request.headers['Content-Length'])
|
||||||
json_data = self.request.body.decode("utf-8")
|
json_data = self.request.body.decode("utf-8")
|
||||||
update = telebot.types.Update.de_json(json_data)
|
update = pytelegrambotapi.types.Update.de_json(json_data)
|
||||||
bot.process_new_updates([update])
|
bot.process_new_updates([update])
|
||||||
self.write("")
|
self.write("")
|
||||||
self.finish()
|
self.finish()
|
||||||
|
@ -20,7 +20,7 @@ logger.addHandler(console_output_handler)
|
|||||||
|
|
||||||
logger.setLevel(logging.ERROR)
|
logger.setLevel(logging.ERROR)
|
||||||
|
|
||||||
from telebot import apihelper, types, util
|
from pytelegrambotapi import apihelper, types, util
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Module : telebot
|
Module : telebot
|
||||||
@ -70,6 +70,7 @@ class TeleBot:
|
|||||||
:param token: bot API token
|
:param token: bot API token
|
||||||
:return: Telebot object.
|
:return: Telebot object.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.token = token
|
self.token = token
|
||||||
self.update_listener = []
|
self.update_listener = []
|
||||||
self.skip_pending = skip_pending
|
self.skip_pending = skip_pending
|
||||||
@ -78,13 +79,11 @@ class TeleBot:
|
|||||||
self.last_update_id = 0
|
self.last_update_id = 0
|
||||||
self.exc_info = None
|
self.exc_info = None
|
||||||
|
|
||||||
self.message_subscribers_messages = []
|
# key: message_id, value: handler list
|
||||||
self.message_subscribers_callbacks = []
|
self.reply_handlers = {}
|
||||||
self.message_subscribers_lock = threading.Lock()
|
|
||||||
|
|
||||||
# key: chat_id, value: handler list
|
# key: chat_id, value: handler list
|
||||||
self.message_subscribers_next_step = {}
|
self.next_step_handlers = {}
|
||||||
self.pre_message_subscribers_next_step = {}
|
|
||||||
|
|
||||||
self.message_handlers = []
|
self.message_handlers = []
|
||||||
self.edited_message_handlers = []
|
self.edited_message_handlers = []
|
||||||
@ -213,11 +212,10 @@ class TeleBot:
|
|||||||
self.process_new_shipping_query(new_shipping_querys)
|
self.process_new_shipping_query(new_shipping_querys)
|
||||||
|
|
||||||
def process_new_messages(self, new_messages):
|
def process_new_messages(self, new_messages):
|
||||||
self._append_pre_next_step_handler()
|
self._notify_next_handlers(new_messages)
|
||||||
|
self._notify_reply_handlers(new_messages)
|
||||||
self.__notify_update(new_messages)
|
self.__notify_update(new_messages)
|
||||||
self._notify_command_handlers(self.message_handlers, new_messages)
|
self._notify_command_handlers(self.message_handlers, new_messages)
|
||||||
self._notify_message_subscribers(new_messages)
|
|
||||||
self._notify_message_next_handler(new_messages)
|
|
||||||
|
|
||||||
def process_new_edited_messages(self, edited_message):
|
def process_new_edited_messages(self, edited_message):
|
||||||
self._notify_command_handlers(self.edited_message_handlers, edited_message)
|
self._notify_command_handlers(self.edited_message_handlers, edited_message)
|
||||||
@ -912,8 +910,8 @@ class TeleBot:
|
|||||||
def send_invoice(self, chat_id, title, description, invoice_payload, provider_token, currency, prices,
|
def send_invoice(self, chat_id, title, description, invoice_payload, provider_token, currency, prices,
|
||||||
start_parameter, photo_url=None, photo_size=None, photo_width=None, photo_height=None,
|
start_parameter, photo_url=None, photo_size=None, photo_width=None, photo_height=None,
|
||||||
need_name=None, need_phone_number=None, need_email=None, need_shipping_address=None,
|
need_name=None, need_phone_number=None, need_email=None, need_shipping_address=None,
|
||||||
is_flexible=None,
|
is_flexible=None, disable_notification=None, reply_to_message_id=None, reply_markup=None,
|
||||||
disable_notification=None, reply_to_message_id=None, reply_markup=None, provider_data=None):
|
provider_data=None):
|
||||||
result = apihelper.send_invoice(self.token, chat_id, title, description, invoice_payload, provider_token,
|
result = apihelper.send_invoice(self.token, chat_id, title, description, invoice_payload, provider_token,
|
||||||
currency, prices, start_parameter, photo_url, photo_size, photo_width,
|
currency, prices, start_parameter, photo_url, photo_size, photo_width,
|
||||||
photo_height,
|
photo_height,
|
||||||
@ -1050,7 +1048,7 @@ class TeleBot:
|
|||||||
"""
|
"""
|
||||||
return apihelper.delete_sticker_from_set(self.token, sticker)
|
return apihelper.delete_sticker_from_set(self.token, sticker)
|
||||||
|
|
||||||
def register_for_reply(self, message, callback):
|
def register_for_reply(self, message, callback, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Registers a callback function to be notified when a reply to `message` arrives.
|
Registers a callback function to be notified when a reply to `message` arrives.
|
||||||
|
|
||||||
@ -1061,40 +1059,60 @@ class TeleBot:
|
|||||||
:param callback: The callback function to be called when a reply arrives. Must accept one `message`
|
:param callback: The callback function to be called when a reply arrives. Must accept one `message`
|
||||||
parameter, which will contain the replied message.
|
parameter, which will contain the replied message.
|
||||||
"""
|
"""
|
||||||
with self.message_subscribers_lock:
|
message_id = message.message_id
|
||||||
self.message_subscribers_messages.insert(0, message.message_id)
|
self.register_for_reply_by_message_id(message_id, callback, *args, **kwargs)
|
||||||
self.message_subscribers_callbacks.insert(0, callback)
|
|
||||||
if len(self.message_subscribers_messages) > 10000:
|
|
||||||
self.message_subscribers_messages.pop()
|
|
||||||
self.message_subscribers_callbacks.pop()
|
|
||||||
|
|
||||||
def _notify_message_subscribers(self, new_messages):
|
def register_for_reply_by_message_id(self, message_id, callback, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Registers a callback function to be notified when a reply to `message` arrives.
|
||||||
|
|
||||||
|
Warning: `message` must be sent with reply_markup=types.ForceReply(), otherwise TeleBot will not be able to see
|
||||||
|
the difference between a reply to `message` and an ordinary message.
|
||||||
|
|
||||||
|
:param message: The message for which we are awaiting a reply.
|
||||||
|
:param callback: The callback function to be called when a reply arrives. Must accept one `message`
|
||||||
|
parameter, which will contain the replied message.
|
||||||
|
"""
|
||||||
|
if message_id in self.reply_handlers.keys():
|
||||||
|
self.reply_handlers[message_id].append({"callback": callback, "args": args, "kwargs": kwargs})
|
||||||
|
else:
|
||||||
|
self.reply_handlers[message_id] = [{"callback": callback, "args": args, "kwargs": kwargs}]
|
||||||
|
|
||||||
|
def _notify_reply_handlers(self, new_messages):
|
||||||
for message in new_messages:
|
for message in new_messages:
|
||||||
if not message.reply_to_message:
|
if hasattr(message, "reply_to_message") and message.reply_to_message is not None:
|
||||||
continue
|
reply_msg_id = message.reply_to_message.message_id
|
||||||
|
if reply_msg_id in self.reply_handlers.keys():
|
||||||
|
handlers = self.reply_handlers[reply_msg_id]
|
||||||
|
for handler in handlers:
|
||||||
|
self._exec_task(handler["callback"], message, *handler["args"], **handler["kwargs"])
|
||||||
|
self.reply_handlers.pop(reply_msg_id)
|
||||||
|
|
||||||
reply_msg_id = message.reply_to_message.message_id
|
def register_next_step_handler(self, message, callback, *args, **kwargs):
|
||||||
if reply_msg_id in self.message_subscribers_messages:
|
|
||||||
index = self.message_subscribers_messages.index(reply_msg_id)
|
|
||||||
self.message_subscribers_callbacks[index](message)
|
|
||||||
|
|
||||||
with self.message_subscribers_lock:
|
|
||||||
index = self.message_subscribers_messages.index(reply_msg_id)
|
|
||||||
del self.message_subscribers_messages[index]
|
|
||||||
del self.message_subscribers_callbacks[index]
|
|
||||||
|
|
||||||
def register_next_step_handler(self, message, callback):
|
|
||||||
"""
|
"""
|
||||||
Registers a callback function to be notified when new message arrives after `message`.
|
Registers a callback function to be notified when new message arrives after `message`.
|
||||||
|
|
||||||
:param message: The message for which we want to handle new message after that in same chat.
|
:param message: The message for which we want to handle new message in the same chat.
|
||||||
:param callback: The callback function which next new message arrives.
|
:param callback: The callback function which next new message arrives.
|
||||||
|
:param args: Args to pass in callback func
|
||||||
|
:param kwargs: Args to pass in callback func
|
||||||
"""
|
"""
|
||||||
chat_id = message.chat.id
|
chat_id = message.chat.id
|
||||||
if chat_id in self.pre_message_subscribers_next_step:
|
self.register_next_step_handler_by_chat_id(chat_id, callback, *args, **kwargs)
|
||||||
self.pre_message_subscribers_next_step[chat_id].append(callback)
|
|
||||||
|
def register_next_step_handler_by_chat_id(self, chat_id, callback, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Registers a callback function to be notified when new message arrives after `message`.
|
||||||
|
|
||||||
|
:param chat_id: The chat for which we want to handle new message.
|
||||||
|
:param callback: The callback function which next new message arrives.
|
||||||
|
:param args: Args to pass in callback func
|
||||||
|
:param kwargs: Args to pass in callback func
|
||||||
|
"""
|
||||||
|
if chat_id in self.next_step_handlers.keys():
|
||||||
|
self.next_step_handlers[chat_id].append({"callback": callback, "args": args, "kwargs": kwargs})
|
||||||
else:
|
else:
|
||||||
self.pre_message_subscribers_next_step[chat_id] = [callback]
|
self.next_step_handlers[chat_id] = [{"callback": callback, "args": args, "kwargs": kwargs}]
|
||||||
|
|
||||||
def clear_step_handler(self, message):
|
def clear_step_handler(self, message):
|
||||||
"""
|
"""
|
||||||
@ -1103,26 +1121,48 @@ class TeleBot:
|
|||||||
:param message: The message for which we want to handle new message after that in same chat.
|
:param message: The message for which we want to handle new message after that in same chat.
|
||||||
"""
|
"""
|
||||||
chat_id = message.chat.id
|
chat_id = message.chat.id
|
||||||
self.pre_message_subscribers_next_step[chat_id] = []
|
self.clear_step_handler_by_chat_id(chat_id)
|
||||||
|
|
||||||
def _notify_message_next_handler(self, new_messages):
|
def clear_step_handler_by_chat_id(self, chat_id):
|
||||||
for message in new_messages:
|
"""
|
||||||
|
Clears all callback functions registered by register_next_step_handler().
|
||||||
|
|
||||||
|
:param chat_id: The chat for which we want to clear next step handlers
|
||||||
|
"""
|
||||||
|
self.next_step_handlers[chat_id] = []
|
||||||
|
|
||||||
|
def clear_reply_handlers(self, message):
|
||||||
|
"""
|
||||||
|
Clears all callback functions registered by register_for_reply() and register_for_reply_by_message_id().
|
||||||
|
|
||||||
|
:param message_id: The message for which we want to clear reply handlers
|
||||||
|
"""
|
||||||
|
message_id = message.message_id
|
||||||
|
self.clear_reply_handlers_by_message_id(message_id)
|
||||||
|
|
||||||
|
def clear_reply_handlers_by_message_id(self, message_id):
|
||||||
|
"""
|
||||||
|
Clears all callback functions registered by register_for_reply() and register_for_reply_by_message_id().
|
||||||
|
|
||||||
|
:param message_id: The message id for which we want to clear reply handlers
|
||||||
|
"""
|
||||||
|
self.reply_handlers[message_id] = []
|
||||||
|
|
||||||
|
def _notify_next_handlers(self, new_messages):
|
||||||
|
i = 0
|
||||||
|
while i < len(new_messages):
|
||||||
|
message = new_messages[i]
|
||||||
chat_id = message.chat.id
|
chat_id = message.chat.id
|
||||||
if chat_id in self.message_subscribers_next_step:
|
if chat_id in self.next_step_handlers.keys():
|
||||||
handlers = self.message_subscribers_next_step[chat_id]
|
handlers = self.next_step_handlers[chat_id]
|
||||||
for handler in handlers:
|
for handler in handlers:
|
||||||
self._exec_task(handler, message)
|
self._exec_task(handler["callback"], message, *handler["args"], **handler["kwargs"])
|
||||||
self.message_subscribers_next_step.pop(chat_id, None)
|
self.next_step_handlers.pop(chat_id, None)
|
||||||
|
new_messages.pop(i) # removing message that detects with next_step_handler
|
||||||
|
i += 1
|
||||||
|
|
||||||
def _append_pre_next_step_handler(self):
|
@staticmethod
|
||||||
for k in self.pre_message_subscribers_next_step.keys():
|
def _build_handler_dict(handler, **filters):
|
||||||
if k in self.message_subscribers_next_step:
|
|
||||||
self.message_subscribers_next_step[k].extend(self.pre_message_subscribers_next_step[k])
|
|
||||||
else:
|
|
||||||
self.message_subscribers_next_step[k] = self.pre_message_subscribers_next_step[k]
|
|
||||||
self.pre_message_subscribers_next_step = {}
|
|
||||||
|
|
||||||
def _build_handler_dict(self, handler, **filters):
|
|
||||||
return {
|
return {
|
||||||
'function': handler,
|
'function': handler,
|
||||||
'filters': filters
|
'filters': filters
|
||||||
@ -1300,9 +1340,6 @@ class TeleBot:
|
|||||||
|
|
||||||
def _notify_command_handlers(self, handlers, new_messages):
|
def _notify_command_handlers(self, handlers, new_messages):
|
||||||
for message in new_messages:
|
for message in new_messages:
|
||||||
# if message has next step handler, dont exec command handlers
|
|
||||||
if hasattr(message, 'chat') and message.chat and (message.chat.id in self.message_subscribers_next_step):
|
|
||||||
continue
|
|
||||||
for message_handler in handlers:
|
for message_handler in handlers:
|
||||||
if self._test_message_handler(message_handler, message):
|
if self._test_message_handler(message_handler, message):
|
||||||
self._exec_task(message_handler['function'], message)
|
self._exec_task(message_handler['function'], message)
|
@ -13,11 +13,11 @@ try:
|
|||||||
format_header_param = fields.format_header_param
|
format_header_param = fields.format_header_param
|
||||||
except ImportError:
|
except ImportError:
|
||||||
format_header_param = None
|
format_header_param = None
|
||||||
import telebot
|
import pytelegrambotapi
|
||||||
from telebot import types
|
from pytelegrambotapi import types
|
||||||
from telebot import util
|
from pytelegrambotapi import util
|
||||||
|
|
||||||
logger = telebot.logger
|
logger = pytelegrambotapi.logger
|
||||||
proxy = None
|
proxy = None
|
||||||
|
|
||||||
API_URL = "https://api.telegram.org/bot{0}/{1}"
|
API_URL = "https://api.telegram.org/bot{0}/{1}"
|
@ -7,7 +7,7 @@ except ImportError:
|
|||||||
|
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from telebot import util
|
from pytelegrambotapi import util
|
||||||
|
|
||||||
|
|
||||||
class JsonSerializable:
|
class JsonSerializable:
|
@ -7,9 +7,9 @@ import time
|
|||||||
import pytest
|
import pytest
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import telebot
|
import pytelegrambotapi
|
||||||
from telebot import types
|
from pytelegrambotapi import types
|
||||||
from telebot import util
|
from pytelegrambotapi import util
|
||||||
|
|
||||||
should_skip = 'TOKEN' and 'CHAT_ID' not in os.environ
|
should_skip = 'TOKEN' and 'CHAT_ID' not in os.environ
|
||||||
|
|
||||||
@ -29,11 +29,11 @@ class TestTeleBot:
|
|||||||
def listener(messages):
|
def listener(messages):
|
||||||
assert len(messages) == 100
|
assert len(messages) == 100
|
||||||
|
|
||||||
tb = telebot.TeleBot('')
|
tb = pytelegrambotapi.TeleBot('')
|
||||||
tb.set_update_listener(listener)
|
tb.set_update_listener(listener)
|
||||||
|
|
||||||
def test_message_handler(self):
|
def test_message_handler(self):
|
||||||
tb = telebot.TeleBot('')
|
tb = pytelegrambotapi.TeleBot('')
|
||||||
msg = self.create_text_message('/help')
|
msg = self.create_text_message('/help')
|
||||||
|
|
||||||
@tb.message_handler(commands=['help', 'start'])
|
@tb.message_handler(commands=['help', 'start'])
|
||||||
@ -45,7 +45,7 @@ class TestTeleBot:
|
|||||||
assert msg.text == 'got'
|
assert msg.text == 'got'
|
||||||
|
|
||||||
def test_message_handler_reg(self):
|
def test_message_handler_reg(self):
|
||||||
bot = telebot.TeleBot('')
|
bot = pytelegrambotapi.TeleBot('')
|
||||||
msg = self.create_text_message(r'https://web.telegram.org/')
|
msg = self.create_text_message(r'https://web.telegram.org/')
|
||||||
|
|
||||||
@bot.message_handler(regexp='((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)')
|
@bot.message_handler(regexp='((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)')
|
||||||
@ -57,7 +57,7 @@ class TestTeleBot:
|
|||||||
assert msg.text == 'got'
|
assert msg.text == 'got'
|
||||||
|
|
||||||
def test_message_handler_lambda(self):
|
def test_message_handler_lambda(self):
|
||||||
bot = telebot.TeleBot('')
|
bot = pytelegrambotapi.TeleBot('')
|
||||||
msg = self.create_text_message(r'lambda_text')
|
msg = self.create_text_message(r'lambda_text')
|
||||||
|
|
||||||
@bot.message_handler(func=lambda message: r'lambda' in message.text)
|
@bot.message_handler(func=lambda message: r'lambda' in message.text)
|
||||||
@ -69,7 +69,7 @@ class TestTeleBot:
|
|||||||
assert msg.text == 'got'
|
assert msg.text == 'got'
|
||||||
|
|
||||||
def test_message_handler_lambda_fail(self):
|
def test_message_handler_lambda_fail(self):
|
||||||
bot = telebot.TeleBot('')
|
bot = pytelegrambotapi.TeleBot('')
|
||||||
msg = self.create_text_message(r'text')
|
msg = self.create_text_message(r'text')
|
||||||
|
|
||||||
@bot.message_handler(func=lambda message: r'lambda' in message.text)
|
@bot.message_handler(func=lambda message: r'lambda' in message.text)
|
||||||
@ -81,7 +81,7 @@ class TestTeleBot:
|
|||||||
assert not msg.text == 'got'
|
assert not msg.text == 'got'
|
||||||
|
|
||||||
def test_message_handler_reg_fail(self):
|
def test_message_handler_reg_fail(self):
|
||||||
bot = telebot.TeleBot('')
|
bot = pytelegrambotapi.TeleBot('')
|
||||||
msg = self.create_text_message(r'web.telegram.org/')
|
msg = self.create_text_message(r'web.telegram.org/')
|
||||||
|
|
||||||
@bot.message_handler(regexp='((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)')
|
@bot.message_handler(regexp='((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)')
|
||||||
@ -93,7 +93,7 @@ class TestTeleBot:
|
|||||||
assert not msg.text == 'got'
|
assert not msg.text == 'got'
|
||||||
|
|
||||||
def test_send_message_with_markdown(self):
|
def test_send_message_with_markdown(self):
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
markdown = """
|
markdown = """
|
||||||
*bold text*
|
*bold text*
|
||||||
_italic text_
|
_italic text_
|
||||||
@ -103,7 +103,7 @@ class TestTeleBot:
|
|||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
def test_send_message_with_disable_notification(self):
|
def test_send_message_with_disable_notification(self):
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
markdown = """
|
markdown = """
|
||||||
*bold text*
|
*bold text*
|
||||||
_italic text_
|
_italic text_
|
||||||
@ -114,7 +114,7 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_send_file(self):
|
def test_send_file(self):
|
||||||
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_document(CHAT_ID, file_data)
|
ret_msg = tb.send_document(CHAT_ID, file_data)
|
||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
@ -123,7 +123,7 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_send_file_dis_noti(self):
|
def test_send_file_dis_noti(self):
|
||||||
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_document(CHAT_ID, file_data, disable_notification=True)
|
ret_msg = tb.send_document(CHAT_ID, file_data, disable_notification=True)
|
||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
@ -132,7 +132,7 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_send_file_caption(self):
|
def test_send_file_caption(self):
|
||||||
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_document(CHAT_ID, file_data, caption="Test")
|
ret_msg = tb.send_document(CHAT_ID, file_data, caption="Test")
|
||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
@ -141,30 +141,30 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_send_video(self):
|
def test_send_video(self):
|
||||||
file_data = open('./test_data/test_video.mp4', 'rb')
|
file_data = open('./test_data/test_video.mp4', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_video(CHAT_ID, file_data)
|
ret_msg = tb.send_video(CHAT_ID, file_data)
|
||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
def test_send_video_dis_noti(self):
|
def test_send_video_dis_noti(self):
|
||||||
file_data = open('./test_data/test_video.mp4', 'rb')
|
file_data = open('./test_data/test_video.mp4', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_video(CHAT_ID, file_data, disable_notification=True)
|
ret_msg = tb.send_video(CHAT_ID, file_data, disable_notification=True)
|
||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
def test_send_video_more_params(self):
|
def test_send_video_more_params(self):
|
||||||
file_data = open('./test_data/test_video.mp4', 'rb')
|
file_data = open('./test_data/test_video.mp4', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_video(CHAT_ID, file_data, 1)
|
ret_msg = tb.send_video(CHAT_ID, file_data, 1)
|
||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
def test_send_video_more_params_dis_noti(self):
|
def test_send_video_more_params_dis_noti(self):
|
||||||
file_data = open('./test_data/test_video.mp4', 'rb')
|
file_data = open('./test_data/test_video.mp4', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_video(CHAT_ID, file_data, 1, disable_notification=True)
|
ret_msg = tb.send_video(CHAT_ID, file_data, 1, disable_notification=True)
|
||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
def test_send_file_exception(self):
|
def test_send_file_exception(self):
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
try:
|
try:
|
||||||
tb.send_document(CHAT_ID, None)
|
tb.send_document(CHAT_ID, None)
|
||||||
assert False
|
assert False
|
||||||
@ -174,7 +174,7 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_send_photo(self):
|
def test_send_photo(self):
|
||||||
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_photo(CHAT_ID, file_data)
|
ret_msg = tb.send_photo(CHAT_ID, file_data)
|
||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
@ -183,7 +183,7 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_send_photo_dis_noti(self):
|
def test_send_photo_dis_noti(self):
|
||||||
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_photo(CHAT_ID, file_data)
|
ret_msg = tb.send_photo(CHAT_ID, file_data)
|
||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
@ -192,7 +192,7 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_send_audio(self):
|
def test_send_audio(self):
|
||||||
file_data = open('./test_data/record.mp3', 'rb')
|
file_data = open('./test_data/record.mp3', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_audio(CHAT_ID, file_data, 1, performer='eternnoir', title='pyTelegram')
|
ret_msg = tb.send_audio(CHAT_ID, file_data, 1, performer='eternnoir', title='pyTelegram')
|
||||||
assert ret_msg.content_type == 'audio'
|
assert ret_msg.content_type == 'audio'
|
||||||
assert ret_msg.audio.performer == 'eternnoir'
|
assert ret_msg.audio.performer == 'eternnoir'
|
||||||
@ -200,7 +200,7 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_send_audio_dis_noti(self):
|
def test_send_audio_dis_noti(self):
|
||||||
file_data = open('./test_data/record.mp3', 'rb')
|
file_data = open('./test_data/record.mp3', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_audio(CHAT_ID, file_data, 1, performer='eternnoir', title='pyTelegram',
|
ret_msg = tb.send_audio(CHAT_ID, file_data, 1, performer='eternnoir', title='pyTelegram',
|
||||||
disable_notification=True)
|
disable_notification=True)
|
||||||
assert ret_msg.content_type == 'audio'
|
assert ret_msg.content_type == 'audio'
|
||||||
@ -209,19 +209,19 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_send_voice(self):
|
def test_send_voice(self):
|
||||||
file_data = open('./test_data/record.ogg', 'rb')
|
file_data = open('./test_data/record.ogg', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_voice(CHAT_ID, file_data)
|
ret_msg = tb.send_voice(CHAT_ID, file_data)
|
||||||
assert ret_msg.voice.mime_type == 'audio/ogg'
|
assert ret_msg.voice.mime_type == 'audio/ogg'
|
||||||
|
|
||||||
def test_send_voice_dis_noti(self):
|
def test_send_voice_dis_noti(self):
|
||||||
file_data = open('./test_data/record.ogg', 'rb')
|
file_data = open('./test_data/record.ogg', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_voice(CHAT_ID, file_data, disable_notification=True)
|
ret_msg = tb.send_voice(CHAT_ID, file_data, disable_notification=True)
|
||||||
assert ret_msg.voice.mime_type == 'audio/ogg'
|
assert ret_msg.voice.mime_type == 'audio/ogg'
|
||||||
|
|
||||||
def test_get_file(self):
|
def test_get_file(self):
|
||||||
file_data = open('./test_data/record.ogg', 'rb')
|
file_data = open('./test_data/record.ogg', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_voice(CHAT_ID, file_data)
|
ret_msg = tb.send_voice(CHAT_ID, file_data)
|
||||||
file_id = ret_msg.voice.file_id
|
file_id = ret_msg.voice.file_id
|
||||||
file_info = tb.get_file(file_id)
|
file_info = tb.get_file(file_id)
|
||||||
@ -229,7 +229,7 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_get_file_dis_noti(self):
|
def test_get_file_dis_noti(self):
|
||||||
file_data = open('./test_data/record.ogg', 'rb')
|
file_data = open('./test_data/record.ogg', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_voice(CHAT_ID, file_data, disable_notification=True)
|
ret_msg = tb.send_voice(CHAT_ID, file_data, disable_notification=True)
|
||||||
file_id = ret_msg.voice.file_id
|
file_id = ret_msg.voice.file_id
|
||||||
file_info = tb.get_file(file_id)
|
file_info = tb.get_file(file_id)
|
||||||
@ -237,19 +237,19 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_send_message(self):
|
def test_send_message(self):
|
||||||
text = 'CI Test Message'
|
text = 'CI Test Message'
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_message(CHAT_ID, text)
|
ret_msg = tb.send_message(CHAT_ID, text)
|
||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
def test_send_message_dis_noti(self):
|
def test_send_message_dis_noti(self):
|
||||||
text = 'CI Test Message'
|
text = 'CI Test Message'
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_message(CHAT_ID, text, disable_notification=True)
|
ret_msg = tb.send_message(CHAT_ID, text, disable_notification=True)
|
||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
def test_send_message_with_markup(self):
|
def test_send_message_with_markup(self):
|
||||||
text = 'CI Test Message'
|
text = 'CI Test Message'
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
markup = types.ReplyKeyboardMarkup()
|
markup = types.ReplyKeyboardMarkup()
|
||||||
markup.add(types.KeyboardButton("1"))
|
markup.add(types.KeyboardButton("1"))
|
||||||
markup.add(types.KeyboardButton("2"))
|
markup.add(types.KeyboardButton("2"))
|
||||||
@ -258,7 +258,7 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_send_message_with_markup_use_string(self):
|
def test_send_message_with_markup_use_string(self):
|
||||||
text = 'CI Test Message'
|
text = 'CI Test Message'
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
markup = types.ReplyKeyboardMarkup()
|
markup = types.ReplyKeyboardMarkup()
|
||||||
markup.add("1")
|
markup.add("1")
|
||||||
markup.add("2")
|
markup.add("2")
|
||||||
@ -269,7 +269,7 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_send_message_with_inlinemarkup(self):
|
def test_send_message_with_inlinemarkup(self):
|
||||||
text = 'CI Test Message'
|
text = 'CI Test Message'
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
markup = types.InlineKeyboardMarkup()
|
markup = types.InlineKeyboardMarkup()
|
||||||
markup.add(types.InlineKeyboardButton("Google", url="http://www.google.com"))
|
markup.add(types.InlineKeyboardButton("Google", url="http://www.google.com"))
|
||||||
markup.add(types.InlineKeyboardButton("Yahoo", url="http://www.yahoo.com"))
|
markup.add(types.InlineKeyboardButton("Yahoo", url="http://www.yahoo.com"))
|
||||||
@ -278,28 +278,28 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_forward_message(self):
|
def test_forward_message(self):
|
||||||
text = 'CI forward_message Test Message'
|
text = 'CI forward_message Test Message'
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
msg = tb.send_message(CHAT_ID, text)
|
msg = tb.send_message(CHAT_ID, text)
|
||||||
ret_msg = tb.forward_message(CHAT_ID, CHAT_ID, msg.message_id)
|
ret_msg = tb.forward_message(CHAT_ID, CHAT_ID, msg.message_id)
|
||||||
assert ret_msg.forward_from
|
assert ret_msg.forward_from
|
||||||
|
|
||||||
def test_forward_message_dis_noti(self):
|
def test_forward_message_dis_noti(self):
|
||||||
text = 'CI forward_message Test Message'
|
text = 'CI forward_message Test Message'
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
msg = tb.send_message(CHAT_ID, text)
|
msg = tb.send_message(CHAT_ID, text)
|
||||||
ret_msg = tb.forward_message(CHAT_ID, CHAT_ID, msg.message_id, disable_notification=True)
|
ret_msg = tb.forward_message(CHAT_ID, CHAT_ID, msg.message_id, disable_notification=True)
|
||||||
assert ret_msg.forward_from
|
assert ret_msg.forward_from
|
||||||
|
|
||||||
def test_reply_to(self):
|
def test_reply_to(self):
|
||||||
text = 'CI reply_to Test Message'
|
text = 'CI reply_to Test Message'
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
msg = tb.send_message(CHAT_ID, text)
|
msg = tb.send_message(CHAT_ID, text)
|
||||||
ret_msg = tb.reply_to(msg, text + ' REPLY')
|
ret_msg = tb.reply_to(msg, text + ' REPLY')
|
||||||
assert ret_msg.reply_to_message.message_id == msg.message_id
|
assert ret_msg.reply_to_message.message_id == msg.message_id
|
||||||
|
|
||||||
def test_register_for_reply(self):
|
def test_register_for_reply(self):
|
||||||
text = 'CI reply_to Test Message'
|
text = 'CI reply_to Test Message'
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
msg = tb.send_message(CHAT_ID, text, reply_markup=types.ForceReply())
|
msg = tb.send_message(CHAT_ID, text, reply_markup=types.ForceReply())
|
||||||
reply_msg = tb.reply_to(msg, text + ' REPLY')
|
reply_msg = tb.reply_to(msg, text + ' REPLY')
|
||||||
|
|
||||||
@ -311,7 +311,7 @@ class TestTeleBot:
|
|||||||
tb.process_new_messages([reply_msg])
|
tb.process_new_messages([reply_msg])
|
||||||
|
|
||||||
def test_send_location(self):
|
def test_send_location(self):
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
lat = 26.3875591
|
lat = 26.3875591
|
||||||
lon = -161.2901042
|
lon = -161.2901042
|
||||||
ret_msg = tb.send_location(CHAT_ID, lat, lon)
|
ret_msg = tb.send_location(CHAT_ID, lat, lon)
|
||||||
@ -319,7 +319,7 @@ class TestTeleBot:
|
|||||||
assert int(ret_msg.location.latitude) == int(lat)
|
assert int(ret_msg.location.latitude) == int(lat)
|
||||||
|
|
||||||
def test_send_location_dis_noti(self):
|
def test_send_location_dis_noti(self):
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
lat = 26.3875591
|
lat = 26.3875591
|
||||||
lon = -161.2901042
|
lon = -161.2901042
|
||||||
ret_msg = tb.send_location(CHAT_ID, lat, lon, disable_notification=True)
|
ret_msg = tb.send_location(CHAT_ID, lat, lon, disable_notification=True)
|
||||||
@ -327,7 +327,7 @@ class TestTeleBot:
|
|||||||
assert int(ret_msg.location.latitude) == int(lat)
|
assert int(ret_msg.location.latitude) == int(lat)
|
||||||
|
|
||||||
def test_send_venue(self):
|
def test_send_venue(self):
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
lat = 26.3875591
|
lat = 26.3875591
|
||||||
lon = -161.2901042
|
lon = -161.2901042
|
||||||
ret_msg = tb.send_venue(CHAT_ID, lat, lon, "Test Venue", "1123 Test Venue address")
|
ret_msg = tb.send_venue(CHAT_ID, lat, lon, "Test Venue", "1123 Test Venue address")
|
||||||
@ -335,50 +335,50 @@ class TestTeleBot:
|
|||||||
assert int(lat) == int(ret_msg.venue.location.latitude)
|
assert int(lat) == int(ret_msg.venue.location.latitude)
|
||||||
|
|
||||||
def test_send_venue_dis_noti(self):
|
def test_send_venue_dis_noti(self):
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
lat = 26.3875591
|
lat = 26.3875591
|
||||||
lon = -161.2901042
|
lon = -161.2901042
|
||||||
ret_msg = tb.send_venue(CHAT_ID, lat, lon, "Test Venue", "1123 Test Venue address", disable_notification=True)
|
ret_msg = tb.send_venue(CHAT_ID, lat, lon, "Test Venue", "1123 Test Venue address", disable_notification=True)
|
||||||
assert ret_msg.venue.title == "Test Venue"
|
assert ret_msg.venue.title == "Test Venue"
|
||||||
|
|
||||||
def test_Chat(self):
|
def test_Chat(self):
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
me = tb.get_me()
|
me = tb.get_me()
|
||||||
msg = tb.send_message(CHAT_ID, 'Test')
|
msg = tb.send_message(CHAT_ID, 'Test')
|
||||||
assert me.id == msg.from_user.id
|
assert me.id == msg.from_user.id
|
||||||
assert msg.chat.id == int(CHAT_ID)
|
assert msg.chat.id == int(CHAT_ID)
|
||||||
|
|
||||||
def test_edit_message_text(self):
|
def test_edit_message_text(self):
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
msg = tb.send_message(CHAT_ID, 'Test')
|
msg = tb.send_message(CHAT_ID, 'Test')
|
||||||
new_msg = tb.edit_message_text('Edit test', chat_id=CHAT_ID, message_id=msg.message_id)
|
new_msg = tb.edit_message_text('Edit test', chat_id=CHAT_ID, message_id=msg.message_id)
|
||||||
assert new_msg.text == 'Edit test'
|
assert new_msg.text == 'Edit test'
|
||||||
|
|
||||||
def test_edit_message_caption(self):
|
def test_edit_message_caption(self):
|
||||||
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
msg = tb.send_document(CHAT_ID, file_data, caption="Test")
|
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)
|
new_msg = tb.edit_message_caption(caption='Edit test', chat_id=CHAT_ID, message_id=msg.message_id)
|
||||||
assert new_msg.caption == 'Edit test'
|
assert new_msg.caption == 'Edit test'
|
||||||
|
|
||||||
def test_get_chat(self):
|
def test_get_chat(self):
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ch = tb.get_chat(GROUP_ID)
|
ch = tb.get_chat(GROUP_ID)
|
||||||
assert str(ch.id) == GROUP_ID
|
assert str(ch.id) == GROUP_ID
|
||||||
|
|
||||||
def test_get_chat_administrators(self):
|
def test_get_chat_administrators(self):
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
cas = tb.get_chat_administrators(GROUP_ID)
|
cas = tb.get_chat_administrators(GROUP_ID)
|
||||||
assert len(cas) > 0
|
assert len(cas) > 0
|
||||||
|
|
||||||
def test_get_chat_members_count(self):
|
def test_get_chat_members_count(self):
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
cn = tb.get_chat_members_count(GROUP_ID)
|
cn = tb.get_chat_members_count(GROUP_ID)
|
||||||
assert cn > 1
|
assert cn > 1
|
||||||
|
|
||||||
def test_edit_markup(self):
|
def test_edit_markup(self):
|
||||||
text = 'CI Test Message'
|
text = 'CI Test Message'
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
markup = types.InlineKeyboardMarkup()
|
markup = types.InlineKeyboardMarkup()
|
||||||
markup.add(types.InlineKeyboardButton("Google", url="http://www.google.com"))
|
markup.add(types.InlineKeyboardButton("Google", url="http://www.google.com"))
|
||||||
markup.add(types.InlineKeyboardButton("Yahoo", url="http://www.yahoo.com"))
|
markup.add(types.InlineKeyboardButton("Yahoo", url="http://www.yahoo.com"))
|
||||||
@ -407,12 +407,12 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_send_video_note(self):
|
def test_send_video_note(self):
|
||||||
file_data = open('./test_data/test_video.mp4', 'rb')
|
file_data = open('./test_data/test_video.mp4', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_video_note(CHAT_ID, file_data)
|
ret_msg = tb.send_video_note(CHAT_ID, file_data)
|
||||||
assert ret_msg.message_id
|
assert ret_msg.message_id
|
||||||
|
|
||||||
def test_send_media_group(self):
|
def test_send_media_group(self):
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
img1 = 'https://i.imgur.com/CjXjcnU.png'
|
img1 = 'https://i.imgur.com/CjXjcnU.png'
|
||||||
img2 = 'https://i.imgur.com/CjXjcnU.png'
|
img2 = 'https://i.imgur.com/CjXjcnU.png'
|
||||||
medias = [types.InputMediaPhoto(img1, "View"), types.InputMediaPhoto(img2, "Dog")]
|
medias = [types.InputMediaPhoto(img1, "View"), types.InputMediaPhoto(img2, "Dog")]
|
||||||
@ -424,7 +424,7 @@ class TestTeleBot:
|
|||||||
def test_send_media_group_local_files(self):
|
def test_send_media_group_local_files(self):
|
||||||
photo = open('../examples/detailed_example/kitten.jpg', 'rb')
|
photo = open('../examples/detailed_example/kitten.jpg', 'rb')
|
||||||
video = open('./test_data/test_video.mp4', 'rb')
|
video = open('./test_data/test_video.mp4', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
medias = [types.InputMediaPhoto(photo, "View"),
|
medias = [types.InputMediaPhoto(photo, "View"),
|
||||||
types.InputMediaVideo(video)]
|
types.InputMediaVideo(video)]
|
||||||
result = tb.send_media_group(CHAT_ID, medias)
|
result = tb.send_media_group(CHAT_ID, medias)
|
||||||
@ -434,31 +434,31 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_send_photo_formating_caption(self):
|
def test_send_photo_formating_caption(self):
|
||||||
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_photo(CHAT_ID, file_data, caption='_italic_', parse_mode='Markdown')
|
ret_msg = tb.send_photo(CHAT_ID, file_data, caption='_italic_', parse_mode='Markdown')
|
||||||
assert ret_msg.caption_entities[0].type == 'italic'
|
assert ret_msg.caption_entities[0].type == 'italic'
|
||||||
|
|
||||||
def test_send_video_formatting_caption(self):
|
def test_send_video_formatting_caption(self):
|
||||||
file_data = open('./test_data/test_video.mp4', 'rb')
|
file_data = open('./test_data/test_video.mp4', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_video(CHAT_ID, file_data, caption='_italic_', parse_mode='Markdown')
|
ret_msg = tb.send_video(CHAT_ID, file_data, caption='_italic_', parse_mode='Markdown')
|
||||||
assert ret_msg.caption_entities[0].type == 'italic'
|
assert ret_msg.caption_entities[0].type == 'italic'
|
||||||
|
|
||||||
def test_send_audio_formatting_caption(self):
|
def test_send_audio_formatting_caption(self):
|
||||||
file_data = open('./test_data/record.mp3', 'rb')
|
file_data = open('./test_data/record.mp3', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_audio(CHAT_ID, file_data, caption='<b>bold</b>', parse_mode='HTML')
|
ret_msg = tb.send_audio(CHAT_ID, file_data, caption='<b>bold</b>', parse_mode='HTML')
|
||||||
assert ret_msg.caption_entities[0].type == 'bold'
|
assert ret_msg.caption_entities[0].type == 'bold'
|
||||||
|
|
||||||
def test_send_voice_formatting_caprion(self):
|
def test_send_voice_formatting_caprion(self):
|
||||||
file_data = open('./test_data/record.ogg', 'rb')
|
file_data = open('./test_data/record.ogg', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_voice(CHAT_ID, file_data, caption='<b>bold</b>', parse_mode='HTML')
|
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.caption_entities[0].type == 'bold'
|
||||||
assert ret_msg.voice.mime_type == 'audio/ogg'
|
assert ret_msg.voice.mime_type == 'audio/ogg'
|
||||||
|
|
||||||
def test_send_media_group_formatting_caption(self):
|
def test_send_media_group_formatting_caption(self):
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
img1 = 'https://i.imgur.com/CjXjcnU.png'
|
img1 = 'https://i.imgur.com/CjXjcnU.png'
|
||||||
img2 = 'https://i.imgur.com/CjXjcnU.png'
|
img2 = 'https://i.imgur.com/CjXjcnU.png'
|
||||||
medias = [types.InputMediaPhoto(img1, "*View*", parse_mode='Markdown'),
|
medias = [types.InputMediaPhoto(img1, "*View*", parse_mode='Markdown'),
|
||||||
@ -471,6 +471,6 @@ class TestTeleBot:
|
|||||||
|
|
||||||
def test_send_document_formating_caption(self):
|
def test_send_document_formating_caption(self):
|
||||||
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
|
||||||
tb = telebot.TeleBot(TOKEN)
|
tb = pytelegrambotapi.TeleBot(TOKEN)
|
||||||
ret_msg = tb.send_document(CHAT_ID, file_data, caption='_italic_', parse_mode='Markdown')
|
ret_msg = tb.send_document(CHAT_ID, file_data, caption='_italic_', parse_mode='Markdown')
|
||||||
assert ret_msg.caption_entities[0].type == 'italic'
|
assert ret_msg.caption_entities[0].type == 'italic'
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
sys.path.append('../')
|
sys.path.append('../')
|
||||||
from telebot import types
|
from pytelegrambotapi import types
|
||||||
|
|
||||||
|
|
||||||
def test_json_user():
|
def test_json_user():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user