diff --git a/examples/deep_linking.py b/examples/deep_linking.py index f92680f..98df204 100644 --- a/examples/deep_linking.py +++ b/examples/deep_linking.py @@ -31,34 +31,38 @@ # steps are not shown here. Only steps 5 to 7 are illustrated, some in pseudo-code, with this example. import telebot -import time bot = telebot.TeleBot('TOKEN') + def extract_unique_code(text): # Extracts the unique_code from the sent /start command. return text.split()[1] if len(text.split()) > 1 else None + def in_storage(unique_code): # (pseudo-code) Should check if a unique code exists in storage return True + def get_username_from_storage(unique_code): # (pseudo-code) Does a query to the storage, retrieving the associated username # Should be replaced by a real database-lookup. return "ABC" if in_storage(unique_code) else None + def save_chat_id(chat_id, username): # (pseudo-code) Save the chat_id->username to storage # Should be replaced by a real database query. pass + @bot.message_handler(commands=['start']) def send_welcome(message): unique_code = extract_unique_code(message.text) - if unique_code: # if the '/start' command contains a unique_code + if unique_code: # if the '/start' command contains a unique_code username = get_username_from_storage(unique_code) - if username: # if the username exists in our database + if username: # if the username exists in our database save_chat_id(message.chat.id, username) reply = "Hello {0}, how are you?".format(username) else: @@ -67,4 +71,5 @@ def send_welcome(message): reply = "Please visit me via a provided URL from the website." bot.reply_to(message, reply) + bot.polling() diff --git a/examples/detailed_example/detailed_example.py b/examples/detailed_example/detailed_example.py index 8fad9af..f481832 100644 --- a/examples/detailed_example/detailed_example.py +++ b/examples/detailed_example/detailed_example.py @@ -2,9 +2,10 @@ This is a detailed example using almost every command of the API """ +import time + import telebot from telebot import types -import time TOKEN = '' @@ -12,10 +13,10 @@ knownUsers = [] # todo: save these in a file, userStep = {} # so they won't reset every time the bot restarts commands = { # command description used in the "help" command - 'start': 'Get used to the bot', - 'help': 'Gives you information about the available commands', - 'sendLongText': 'A test using the \'send_chat_action\' command', - 'getImage': 'A test using multi-stage messages, custom keyboard, and media sending' + 'start' : 'Get used to the bot', + 'help' : 'Gives you information about the available commands', + 'sendLongText': 'A test using the \'send_chat_action\' command', + 'getImage' : 'A test using multi-stage messages, custom keyboard, and media sending' } imageSelect = types.ReplyKeyboardMarkup(one_time_keyboard=True) # create the image selection keyboard @@ -33,7 +34,7 @@ def get_user_step(uid): else: knownUsers.append(uid) userStep[uid] = 0 - print "New user detected, who hasn't used \"/start\" yet" + print("New user detected, who hasn't used \"/start\" yet") return 0 @@ -45,7 +46,7 @@ def listener(messages): for m in messages: if m.content_type == 'text': # print the sent message to the console - 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) @@ -128,4 +129,5 @@ def command_default(m): # this is the standard reply to a normal message bot.send_message(m.chat.id, "I don't understand \"" + m.text + "\"\nMaybe try the help page at /help") + bot.polling() diff --git a/examples/echo_bot.py b/examples/echo_bot.py index f88d3bf..b20f09d 100644 --- a/examples/echo_bot.py +++ b/examples/echo_bot.py @@ -7,6 +7,7 @@ API_TOKEN = '' bot = telebot.TeleBot(API_TOKEN) + # Handle '/start' and '/help' @bot.message_handler(commands=['help', 'start']) def send_welcome(message): @@ -21,4 +22,5 @@ I am here to echo your kind words back to you. Just say anything nice and I'll s def echo_message(message): bot.reply_to(message, message.text) + bot.polling() diff --git a/examples/inline_example.py b/examples/inline_example.py index 5164eec..e36932d 100644 --- a/examples/inline_example.py +++ b/examples/inline_example.py @@ -1,8 +1,9 @@ # This example show how to write an inline mode telegram bot use pyTelegramBotAPI. -import telebot -import time -import sys import logging +import sys +import time + +import telebot from telebot import types API_TOKEN = '' @@ -69,5 +70,5 @@ if __name__ == '__main__': try: main_loop() except KeyboardInterrupt: - print >> sys.stderr, '\nExiting by user request.\n' + print('\nExiting by user request.\n') sys.exit(0) diff --git a/examples/payments_example.py b/examples/payments_example.py index 3e82955..d0f52d4 100644 --- a/examples/payments_example.py +++ b/examples/payments_example.py @@ -1,6 +1,5 @@ import telebot -from telebot.types import LabeledPrice -from telebot.types import ShippingOption +from telebot.types import LabeledPrice, ShippingOption token = '1234567890:AAAABBBBCCCCDDDDeeeeFFFFgggGHHHH' provider_token = '1234567890:TEST:AAAABBBBCCCCDDDD' # @BotFather -> Bot Settings -> Payments diff --git a/examples/step_example.py b/examples/step_example.py index 0fc0ef1..0fc17e5 100644 --- a/examples/step_example.py +++ b/examples/step_example.py @@ -2,7 +2,6 @@ """ This Example will show you how to use register_next_step handler. """ -import time import telebot from telebot import types @@ -84,5 +83,4 @@ bot.enable_save_next_step_handlers(delay=2) # WARNING It will work only if enable_save_next_step_handlers was called! bot.load_next_step_handlers() - bot.polling() diff --git a/examples/telebot_bot/telebot_bot.py b/examples/telebot_bot/telebot_bot.py index cd29276..ac6b63c 100644 --- a/examples/telebot_bot/telebot_bot.py +++ b/examples/telebot_bot/telebot_bot.py @@ -3,9 +3,10 @@ # and goes by the name 'TeleBot (@pyTeleBot)'. Join our group to talk to him! # WARNING: Tested with Python 2.7 -import telebot import os +import telebot + text_messages = { 'welcome': u'Please welcome {name}!\n\n' @@ -33,8 +34,10 @@ if "TELEBOT_BOT_TOKEN" not in os.environ or "GROUP_CHAT_ID" not in os.environ: bot = telebot.AsyncTeleBot(os.environ["TELEBOT_BOT_TOKEN"]) GROUP_CHAT_ID = int(os.environ["GROUP_CHAT_ID"]) + def is_api_group(chat_id): - return chat_id== GROUP_CHAT_ID + return chat_id == GROUP_CHAT_ID + @bot.message_handler(func=lambda m: True, content_types=['new_chat_participant']) def on_user_joins(message): @@ -50,6 +53,7 @@ def on_user_joins(message): bot.reply_to(message, text_messages['welcome'].format(name=name)) + @bot.message_handler(commands=['info', 'help']) def on_info(message): if not is_api_group(message.chat.id): @@ -58,21 +62,23 @@ def on_info(message): bot.reply_to(message, text_messages['info']) + @bot.message_handler(commands=["ping"]) def on_ping(message): bot.reply_to(message, "Still alive and kicking!") + @bot.message_handler(commands=['start']) def on_start(message): if not is_api_group(message.chat.id): bot.reply_to(message, text_messages['wrong_chat']) return + def listener(messages): for m in messages: - print str(m) + print(str(m)) + bot.set_update_listener(listener) bot.polling() - - diff --git a/examples/webhook_examples/webhook_aiohttp_echo_bot.py b/examples/webhook_examples/webhook_aiohttp_echo_bot.py index d92cff9..bbb7c6e 100644 --- a/examples/webhook_examples/webhook_aiohttp_echo_bot.py +++ b/examples/webhook_examples/webhook_aiohttp_echo_bot.py @@ -11,7 +11,6 @@ from aiohttp import web import telebot - API_TOKEN = '' WEBHOOK_HOST = '' @@ -32,7 +31,6 @@ WEBHOOK_SSL_PRIV = './webhook_pkey.pem' # Path to the ssl private key WEBHOOK_URL_BASE = "https://{}:{}".format(WEBHOOK_HOST, WEBHOOK_PORT) WEBHOOK_URL_PATH = "/{}/".format(API_TOKEN) - logger = telebot.logger telebot.logger.setLevel(logging.INFO) @@ -51,6 +49,7 @@ async def handle(request): else: return web.Response(status=403) + app.router.add_post('/{token}/', handle) @@ -72,7 +71,7 @@ def echo_message(message): bot.remove_webhook() # Set webhook -bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH, +bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH, certificate=open(WEBHOOK_SSL_CERT, 'r')) # Build ssl context diff --git a/examples/webhook_examples/webhook_cherrypy_echo_bot.py b/examples/webhook_examples/webhook_cherrypy_echo_bot.py index d0f3da0..7b46e78 100644 --- a/examples/webhook_examples/webhook_cherrypy_echo_bot.py +++ b/examples/webhook_examples/webhook_cherrypy_echo_bot.py @@ -4,10 +4,11 @@ # This is a simple echo bot using decorators and webhook with CherryPy # It echoes any incoming text messages and does not use the polling method. -import cherrypy -import telebot import logging +import cherrypy + +import telebot API_TOKEN = '' @@ -29,7 +30,6 @@ WEBHOOK_SSL_PRIV = './webhook_pkey.pem' # Path to the ssl private key WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT) WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN) - logger = telebot.logger telebot.logger.setLevel(logging.INFO) @@ -70,7 +70,7 @@ def echo_message(message): bot.remove_webhook() # Set webhook -bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH, +bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH, certificate=open(WEBHOOK_SSL_CERT, 'r')) # Disable CherryPy requests log @@ -80,9 +80,9 @@ for handler in tuple(access_log.handlers): # Start cherrypy server cherrypy.config.update({ - 'server.socket_host': WEBHOOK_LISTEN, - 'server.socket_port': WEBHOOK_PORT, - 'server.ssl_module': 'builtin', + 'server.socket_host' : WEBHOOK_LISTEN, + 'server.socket_port' : WEBHOOK_PORT, + 'server.ssl_module' : 'builtin', 'server.ssl_certificate': WEBHOOK_SSL_CERT, 'server.ssl_private_key': WEBHOOK_SSL_PRIV }) diff --git a/examples/webhook_examples/webhook_cpython_echo_bot.py b/examples/webhook_examples/webhook_cpython_echo_bot.py index 5c1dfc9..029f361 100644 --- a/examples/webhook_examples/webhook_cpython_echo_bot.py +++ b/examples/webhook_examples/webhook_cpython_echo_bot.py @@ -4,11 +4,17 @@ # This is a simple echo bot using decorators and webhook with BaseHTTPServer # It echoes any incoming text messages and does not use the polling method. -import BaseHTTPServer -import ssl -import telebot -import logging +try: + # Python 2 + from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer +except ImportError: + # Python 3 + from http.server import BaseHTTPRequestHandler, HTTPServer +import logging +import ssl + +import telebot API_TOKEN = '' @@ -30,7 +36,6 @@ WEBHOOK_SSL_PRIV = './webhook_pkey.pem' # Path to the ssl private key WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT) WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN) - logger = telebot.logger telebot.logger.setLevel(logging.INFO) @@ -38,7 +43,7 @@ bot = telebot.TeleBot(API_TOKEN) # WebhookHandler, process webhook calls -class WebhookHandler(BaseHTTPServer.BaseHTTPRequestHandler): +class WebhookHandler(BaseHTTPRequestHandler): server_version = "WebhookHandler/1.0" def do_HEAD(self): @@ -84,12 +89,12 @@ def echo_message(message): bot.remove_webhook() # Set webhook -bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH, +bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH, certificate=open(WEBHOOK_SSL_CERT, 'r')) # Start server -httpd = BaseHTTPServer.HTTPServer((WEBHOOK_LISTEN, WEBHOOK_PORT), - WebhookHandler) +httpd = HTTPServer((WEBHOOK_LISTEN, WEBHOOK_PORT), + WebhookHandler) httpd.socket = ssl.wrap_socket(httpd.socket, certfile=WEBHOOK_SSL_CERT, diff --git a/examples/webhook_examples/webhook_flask_echo_bot.py b/examples/webhook_examples/webhook_flask_echo_bot.py index d0327d7..daa3995 100644 --- a/examples/webhook_examples/webhook_flask_echo_bot.py +++ b/examples/webhook_examples/webhook_flask_echo_bot.py @@ -4,11 +4,12 @@ # This is a simple echo bot using decorators and webhook with flask # It echoes any incoming text messages and does not use the polling method. -import flask -import telebot import logging import time +import flask + +import telebot API_TOKEN = '' @@ -30,7 +31,6 @@ WEBHOOK_SSL_PRIV = './webhook_pkey.pem' # Path to the ssl private key WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT) WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN) - logger = telebot.logger telebot.logger.setLevel(logging.INFO) @@ -77,7 +77,7 @@ bot.remove_webhook() time.sleep(0.1) # Set webhook -bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH, +bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH, certificate=open(WEBHOOK_SSL_CERT, 'r')) # Start flask server diff --git a/examples/webhook_examples/webhook_flask_heroku_echo.py b/examples/webhook_examples/webhook_flask_heroku_echo.py index 62d0a90..7bbf2bf 100644 --- a/examples/webhook_examples/webhook_flask_heroku_echo.py +++ b/examples/webhook_examples/webhook_flask_heroku_echo.py @@ -1,8 +1,9 @@ import os -import telebot from flask import Flask, request +import telebot + TOKEN = '' bot = telebot.TeleBot(TOKEN) server = Flask(__name__) diff --git a/examples/webhook_examples/webhook_tornado_echo_bot.py b/examples/webhook_examples/webhook_tornado_echo_bot.py index 538b7b9..171d2d7 100644 --- a/examples/webhook_examples/webhook_tornado_echo_bot.py +++ b/examples/webhook_examples/webhook_tornado_echo_bot.py @@ -4,13 +4,15 @@ # This example shows webhook echo bot with Tornado web framework # Documenation to Tornado: http://tornadoweb.org -import telebot -import tornado.web -import tornado.ioloop -import tornado.httpserver -import tornado.options import signal +import tornado.httpserver +import tornado.ioloop +import tornado.options +import tornado.web + +import telebot + API_TOKEN = '' WEBHOOK_CERT = "./cert.pem" WEBHOOK_PKEY = "./pkey.pem" @@ -29,15 +31,18 @@ WEBHOOK_URL_BASE = "https://{0}:{1}/{2}".format(WEBHOOK_HOST, str(WEBHOOK_PORT), bot = telebot.TeleBot(API_TOKEN) + class Root(tornado.web.RequestHandler): def get(self): self.write("Hi! This is webhook example!") self.finish() + class webhook_serv(tornado.web.RequestHandler): def get(self): self.write("What are you doing here?") self.finish() + def post(self): if "Content-Length" in self.request.headers and \ "Content-Type" in self.request.headers and \ @@ -52,21 +57,26 @@ class webhook_serv(tornado.web.RequestHandler): else: self.write("What are you doing here?") self.finish() - + + tornado.options.define("port", default=WEBHOOK_PORT, help="run on the given port", type=int) is_closing = False + + def signal_handler(signum, frame): global is_closing print("Exiting...") is_closing = True + def try_exit(): global is_closing if is_closing: # clean up here tornado.ioloop.IOLoop.instance().stop() print("Exit success!") - + + # Handle '/start' and '/help' @bot.message_handler(commands=['help', 'start']) def send_welcome(message): @@ -74,6 +84,7 @@ def send_welcome(message): ("Hi there, I am EchoBot.\n" "I am here to echo your kind words back to you.")) + bot.remove_webhook() bot.set_webhook(url=WEBHOOK_URL_BASE, certificate=open(WEBHOOK_CERT, 'r')) @@ -86,9 +97,9 @@ application = tornado.web.Application([ ]) http_server = tornado.httpserver.HTTPServer(application, ssl_options={ - "certfile": WEBHOOK_CERT, - "keyfile": WEBHOOK_PKEY, - }) + "certfile": WEBHOOK_CERT, + "keyfile" : WEBHOOK_PKEY, +}) http_server.listen(tornado.options.options.port) tornado.ioloop.PeriodicCallback(try_exit, 100).start() tornado.ioloop.IOLoop.instance().start() diff --git a/telebot/__init__.py b/telebot/__init__.py index 41b65a4..52a4e5a 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -1,16 +1,15 @@ # -*- coding: utf-8 -*- from __future__ import print_function -import threading -import time -import re -import sys -import six - +import logging import os import pickle +import re +import sys +import threading +import time -import logging +import six logger = logging.getLogger('TeleBot') formatter = logging.Formatter( @@ -34,6 +33,7 @@ class Handler: """ Class for (next step|reply) handlers """ + def __init__(self, callback, *args, **kwargs): self.callback = callback self.args = args @@ -47,6 +47,7 @@ class Saver: """ Class for saving (next step|reply) handlers """ + def __init__(self, handlers, filename, delay): self.handlers = handlers self.filename = filename @@ -1294,22 +1295,21 @@ class TeleBot: was_poped = False if chat_id in self.next_step_handlers.keys(): handlers = self.next_step_handlers.pop(chat_id, None) - if (handlers): + if handlers: for handler in handlers: self._exec_task(handler["callback"], message, *handler["args"], **handler["kwargs"]) new_messages.pop(i) # removing message that detects with next_step_handler was_poped = True if self.next_step_saver is not None: self.next_step_saver.start_save_timer() - if (not was_poped): + if not was_poped: i += 1 - @staticmethod def _build_handler_dict(handler, **filters): return { 'function': handler, - 'filters': filters + 'filters' : filters } def message_handler(self, commands=None, regexp=None, func=None, content_types=['text'], **kwargs): @@ -1496,12 +1496,12 @@ class AsyncTeleBot(TeleBot): TeleBot.__init__(self, *args, **kwargs) @util.async_dec() - def enable_save_next_step_handlers(self, delay=120, filename="./.handler-saves/step.save", del_file_after_loading=True): - return TeleBot.enable_save_next_step_handlers(self, delay, filename, del_file_after_loading) + def enable_save_next_step_handlers(self, delay=120, filename="./.handler-saves/step.save"): + return TeleBot.enable_save_next_step_handlers(self, delay, filename) @util.async_dec() - def enable_save_reply_handlers(self, delay=120, filename="./.handler-saves/reply.save", del_file_after_loading=True): - return TeleBot.enable_save_reply_handlers(self, delay, filename, del_file_after_loading) + def enable_save_reply_handlers(self, delay=120, filename="./.handler-saves/reply.save"): + return TeleBot.enable_save_reply_handlers(self, delay, filename) @util.async_dec() def disable_save_next_step_handlers(self): @@ -1512,14 +1512,12 @@ class AsyncTeleBot(TeleBot): return TeleBot.enable_save_reply_handlers(self) @util.async_dec() - def load_next_step_handlers(self, filename="./.handler-saves/step.save"): - return TeleBot.load_next_step_handlers(self, filename) - - @util.async_dec() - def load_reply_handlers(self, filename="./.handler-saves/reply.save"): - return TeleBot.load_reply_handlers(self, filename) + def load_next_step_handlers(self, filename="./.handler-saves/step.save", del_file_after_loading=True): + return TeleBot.load_next_step_handlers(self, filename, del_file_after_loading) @util.async_dec() + def load_reply_handlers(self, filename="./.handler-saves/reply.save", del_file_after_loading=True): + return TeleBot.load_reply_handlers(self, filename, del_file_after_loading) @util.async_dec() def get_me(self): diff --git a/telebot/apihelper.py b/telebot/apihelper.py index a5e1df3..839754f 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -99,7 +99,6 @@ def get_file(token, file_id): def get_file_url(token, file_id): - method_url = r'getFile' return FILE_URL.format(token, get_file(token, file_id).file_path) @@ -123,6 +122,8 @@ def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_m :param disable_web_page_preview: :param reply_to_message_id: :param reply_markup: + :param parse_mode: + :param disable_notification: :return: """ method_url = r'sendMessage' @@ -769,7 +770,8 @@ def send_invoice(token, chat_id, title, description, invoice_payload, provider_t :param disable_notification: Sends the message silently. Users will receive a notification with no sound. :param reply_to_message_id: If the message is a reply, ID of the original message :param reply_markup: A JSON-serialized object for an inline keyboard. If empty, one 'Pay total price' button will be shown. If not empty, the first button must be a Pay button - :return: + :param provider_data: + :return: """ method_url = r'sendInvoice' payload = {'chat_id': chat_id, 'title': title, 'description': description, 'payload': invoice_payload, diff --git a/telebot/types.py b/telebot/types.py index 7340c10..06a1c44 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -457,11 +457,11 @@ class Message(JsonDeserializable): if not entities: return text _subs = { - "bold": "{text}", - "italic": "{text}", - "pre": "
{text}
", - "code": "{text}", - "url": "{text}", + "bold" : "{text}", + "italic" : "{text}", + "pre" : "
{text}
", + "code" : "{text}", + "url" : "{text}", "text_link": "{text}" } if hasattr(self, "custom_subs"): @@ -469,6 +469,7 @@ class Message(JsonDeserializable): _subs[type] = self.custom_subs[type] utf16_text = text.encode("utf-16-le") html_text = "" + def func(text, type=None, url=None, user=None): text = text.decode("utf-16-le") if type == "text_mention": @@ -501,6 +502,7 @@ class Message(JsonDeserializable): def html_caption(self): return self.__html_text(self.caption, self.caption_entities) + class MessageEntity(JsonDeserializable): @classmethod def de_json(cls, json_string): @@ -598,29 +600,6 @@ class Document(JsonDeserializable): self.file_size = file_size -class Sticker(JsonDeserializable): - @classmethod - def de_json(cls, json_string): - obj = cls.check_json(json_string) - file_id = obj['file_id'] - width = obj['width'] - height = obj['height'] - thumb = None - if 'thumb' in obj: - thumb = PhotoSize.de_json(obj['thumb']) - emoji = obj.get('emoji') - file_size = obj.get('file_size') - return cls(file_id, width, height, thumb, emoji, file_size) - - def __init__(self, file_id, width, height, thumb, emoji=None, file_size=None): - self.file_id = file_id - self.width = width - self.height = height - self.thumb = thumb - self.emoji = emoji - self.file_size = file_size - - class Video(JsonDeserializable): @classmethod def de_json(cls, json_string): @@ -1092,7 +1071,7 @@ class InputVenueMessageContent(Dictionaryable): def to_dic(self): json_dic = {'latitude': self.latitude, 'longitude': self.longitude, 'title': self.title, - 'address': self.address} + 'address' : self.address} if self.foursquare_id: json_dic['foursquare_id'] = self.foursquare_id return json_dic diff --git a/telebot/util.py b/telebot/util.py index e16ebb5..21826d0 100644 --- a/telebot/util.py +++ b/telebot/util.py @@ -1,10 +1,11 @@ # -*- coding: utf-8 -*- import random +import re import string +import sys import threading import traceback -import re -import sys + import six from six import string_types @@ -243,7 +244,7 @@ def extract_arguments(text): :param text: String to extract the arguments from a command :return: the arguments if `text` is a command (according to is_command), else None. """ - regexp = re.compile("\/\w*(@\w*)*\s*([\s\S]*)",re.IGNORECASE) + regexp = re.compile("/\w*(@\w*)*\s*([\s\S]*)",re.IGNORECASE) result = regexp.match(text) return result.group(2) if is_command(text) else None diff --git a/tests/test_telebot.py b/tests/test_telebot.py index b81485e..908b141 100644 --- a/tests/test_telebot.py +++ b/tests/test_telebot.py @@ -402,7 +402,8 @@ class TestTeleBot: new_msg = tb.edit_message_reply_markup(chat_id=CHAT_ID, message_id=ret_msg.message_id, reply_markup=markup) assert new_msg.message_id - def create_text_message(self, text): + @staticmethod + def create_text_message(text): params = {'text': text} chat = types.User(11, False, 'test') return types.Message(1, None, None, chat, 'text', params, "") diff --git a/tests/test_types.py b/tests/test_types.py index c174d42..c229bdf 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -59,7 +59,7 @@ def test_json_Message_Sticker_without_thumb(): json_string = r'{"message_id":98,"from":{"id":10734,"first_name":"Fd","last_name":"Wd","username":"dd","is_bot":true },"chat":{"id":10734,"first_name":"Fd","type":"private","last_name":"Wd","username":"dd"},"date":1435479551,"sticker":{"width":550,"height":368,"file_id":"BQADBQADNAIAAsYifgYdGJOa6bGAsQI","file_size":30320}}' msg = types.Message.de_json(json_string) assert msg.sticker.height == 368 - assert msg.sticker.thumb == None + assert msg.sticker.thumb is None assert msg.content_type == 'sticker' @@ -113,6 +113,7 @@ def test_json_voice(): assert voice.duration == 0 assert voice.file_size == 10481 + def test_json_update(): json_string = r'{"update_id":938203,"message":{"message_id":241,"from":{"is_bot":true,"id":9734,"first_name":"Fk","last_name":"Wg","username":"nir"},"chat":{"id":1111,"first_name":"Fk","type":"private","last_name":"Wg","username":"oir"},"date":1441447009,"text":"HIHI"}}' update = types.Update.de_json(json_string) @@ -120,6 +121,7 @@ def test_json_update(): assert update.message.message_id == 241 assert update.message.from_user.id == 9734 + def test_json_chat(): json_string = r'{"id": -111111,"title": "Test Title","type": "group"}' chat = types.Chat.de_json(json_string) @@ -127,6 +129,7 @@ def test_json_chat(): assert chat.type == 'group' assert chat.title == 'Test Title' + def test_InlineQueryResultCachedPhoto(): iq = types.InlineQueryResultCachedPhoto('aaa', 'Fileid') json_str = iq.to_json() @@ -143,6 +146,7 @@ def test_InlineQueryResultCachedPhoto_with_title(): assert 'Title' in json_str assert 'caption' not in json_str + def test_InlineQueryResultCachedPhoto_with_markup(): markup = types.InlineKeyboardMarkup() markup.add(types.InlineKeyboardButton("Google", url="http://www.google.com")) @@ -154,4 +158,3 @@ def test_InlineQueryResultCachedPhoto_with_markup(): assert 'Title' in json_str assert 'caption' not in json_str assert 'reply_markup' in json_str -