1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Merge pull request #565 from uburuntu/analyzer-fixes

Some analyzer fixes
This commit is contained in:
FrankWang 2018-10-19 10:06:37 +08:00 committed by GitHub
commit e62eeb7ff2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 128 additions and 115 deletions

View File

@ -31,34 +31,38 @@
# 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 telebot
import time
bot = telebot.TeleBot('TOKEN') bot = telebot.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.
return text.split()[1] if len(text.split()) > 1 else None return text.split()[1] if len(text.split()) > 1 else None
def in_storage(unique_code): def in_storage(unique_code):
# (pseudo-code) Should check if a unique code exists in storage # (pseudo-code) Should check if a unique code exists in storage
return True return True
def get_username_from_storage(unique_code): def get_username_from_storage(unique_code):
# (pseudo-code) Does a query to the storage, retrieving the associated username # (pseudo-code) Does a query to the storage, retrieving the associated username
# Should be replaced by a real database-lookup. # Should be replaced by a real database-lookup.
return "ABC" if in_storage(unique_code) else None return "ABC" if in_storage(unique_code) else None
def save_chat_id(chat_id, username): def save_chat_id(chat_id, username):
# (pseudo-code) Save the chat_id->username to storage # (pseudo-code) Save the chat_id->username to storage
# Should be replaced by a real database query. # Should be replaced by a real database query.
pass pass
@bot.message_handler(commands=['start']) @bot.message_handler(commands=['start'])
def send_welcome(message): def send_welcome(message):
unique_code = extract_unique_code(message.text) 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) 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) save_chat_id(message.chat.id, username)
reply = "Hello {0}, how are you?".format(username) reply = "Hello {0}, how are you?".format(username)
else: else:
@ -67,4 +71,5 @@ def send_welcome(message):
reply = "Please visit me via a provided URL from the website." reply = "Please visit me via a provided URL from the website."
bot.reply_to(message, reply) bot.reply_to(message, reply)
bot.polling() bot.polling()

View File

@ -2,9 +2,10 @@
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 time
import telebot import telebot
from telebot import types from telebot import types
import time
TOKEN = '<token_string>' TOKEN = '<token_string>'
@ -12,10 +13,10 @@ knownUsers = [] # todo: save these in a file,
userStep = {} # so they won't reset every time the bot restarts userStep = {} # so they won't reset every time the bot restarts
commands = { # command description used in the "help" command commands = { # command description used in the "help" command
'start': 'Get used to the bot', 'start' : 'Get used to the bot',
'help': 'Gives you information about the available commands', 'help' : 'Gives you information about the available commands',
'sendLongText': 'A test using the \'send_chat_action\' command', 'sendLongText': 'A test using the \'send_chat_action\' command',
'getImage': 'A test using multi-stage messages, custom keyboard, and media sending' 'getImage' : 'A test using multi-stage messages, custom keyboard, and media sending'
} }
imageSelect = types.ReplyKeyboardMarkup(one_time_keyboard=True) # create the image selection keyboard imageSelect = types.ReplyKeyboardMarkup(one_time_keyboard=True) # create the image selection keyboard
@ -33,7 +34,7 @@ def get_user_step(uid):
else: else:
knownUsers.append(uid) knownUsers.append(uid)
userStep[uid] = 0 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 return 0
@ -45,7 +46,7 @@ def listener(messages):
for m in messages: for m in messages:
if m.content_type == 'text': if m.content_type == 'text':
# print the sent message to the console # 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) bot = telebot.TeleBot(TOKEN)
@ -128,4 +129,5 @@ def command_default(m):
# this is the standard reply to a normal message # 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.send_message(m.chat.id, "I don't understand \"" + m.text + "\"\nMaybe try the help page at /help")
bot.polling() bot.polling()

View File

@ -7,6 +7,7 @@ API_TOKEN = '<api_token>'
bot = telebot.TeleBot(API_TOKEN) bot = telebot.TeleBot(API_TOKEN)
# Handle '/start' and '/help' # Handle '/start' and '/help'
@bot.message_handler(commands=['help', 'start']) @bot.message_handler(commands=['help', 'start'])
def send_welcome(message): 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): def echo_message(message):
bot.reply_to(message, message.text) bot.reply_to(message, message.text)
bot.polling() bot.polling()

View File

@ -1,8 +1,9 @@
# This example show how to write an inline mode telegram bot use pyTelegramBotAPI. # This example show how to write an inline mode telegram bot use pyTelegramBotAPI.
import telebot
import time
import sys
import logging import logging
import sys
import time
import telebot
from telebot import types from telebot import types
API_TOKEN = '<TOKEN>' API_TOKEN = '<TOKEN>'
@ -69,5 +70,5 @@ if __name__ == '__main__':
try: try:
main_loop() main_loop()
except KeyboardInterrupt: except KeyboardInterrupt:
print >> sys.stderr, '\nExiting by user request.\n' print('\nExiting by user request.\n')
sys.exit(0) sys.exit(0)

View File

@ -1,6 +1,5 @@
import telebot import telebot
from telebot.types import LabeledPrice from telebot.types import LabeledPrice, ShippingOption
from telebot.types import ShippingOption
token = '1234567890:AAAABBBBCCCCDDDDeeeeFFFFgggGHHHH' token = '1234567890:AAAABBBBCCCCDDDDeeeeFFFFgggGHHHH'
provider_token = '1234567890:TEST:AAAABBBBCCCCDDDD' # @BotFather -> Bot Settings -> Payments provider_token = '1234567890:TEST:AAAABBBBCCCCDDDD' # @BotFather -> Bot Settings -> Payments

View File

@ -2,7 +2,6 @@
""" """
This Example will show you how to use register_next_step handler. This Example will show you how to use register_next_step handler.
""" """
import time
import telebot import telebot
from telebot import types 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! # WARNING It will work only if enable_save_next_step_handlers was called!
bot.load_next_step_handlers() bot.load_next_step_handlers()
bot.polling() bot.polling()

View File

@ -3,9 +3,10 @@
# 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 os import os
import telebot
text_messages = { text_messages = {
'welcome': 'welcome':
u'Please welcome {name}!\n\n' 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"]) bot = telebot.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):
return chat_id== GROUP_CHAT_ID return chat_id == GROUP_CHAT_ID
@bot.message_handler(func=lambda m: True, content_types=['new_chat_participant']) @bot.message_handler(func=lambda m: True, content_types=['new_chat_participant'])
def on_user_joins(message): 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.reply_to(message, text_messages['welcome'].format(name=name))
@bot.message_handler(commands=['info', 'help']) @bot.message_handler(commands=['info', 'help'])
def on_info(message): def on_info(message):
if not is_api_group(message.chat.id): if not is_api_group(message.chat.id):
@ -58,21 +62,23 @@ def on_info(message):
bot.reply_to(message, text_messages['info']) bot.reply_to(message, text_messages['info'])
@bot.message_handler(commands=["ping"]) @bot.message_handler(commands=["ping"])
def on_ping(message): def on_ping(message):
bot.reply_to(message, "Still alive and kicking!") bot.reply_to(message, "Still alive and kicking!")
@bot.message_handler(commands=['start']) @bot.message_handler(commands=['start'])
def on_start(message): def on_start(message):
if not is_api_group(message.chat.id): if not is_api_group(message.chat.id):
bot.reply_to(message, text_messages['wrong_chat']) bot.reply_to(message, text_messages['wrong_chat'])
return return
def listener(messages): def listener(messages):
for m in messages: for m in messages:
print str(m) print(str(m))
bot.set_update_listener(listener) bot.set_update_listener(listener)
bot.polling() bot.polling()

View File

@ -11,7 +11,6 @@ from aiohttp import web
import telebot import telebot
API_TOKEN = '<api_token>' API_TOKEN = '<api_token>'
WEBHOOK_HOST = '<ip/host where the bot is running>' WEBHOOK_HOST = '<ip/host where the bot is running>'
@ -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_BASE = "https://{}:{}".format(WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/{}/".format(API_TOKEN) WEBHOOK_URL_PATH = "/{}/".format(API_TOKEN)
logger = telebot.logger logger = telebot.logger
telebot.logger.setLevel(logging.INFO) telebot.logger.setLevel(logging.INFO)
@ -51,6 +49,7 @@ async def handle(request):
else: else:
return web.Response(status=403) return web.Response(status=403)
app.router.add_post('/{token}/', handle) app.router.add_post('/{token}/', handle)
@ -72,7 +71,7 @@ def echo_message(message):
bot.remove_webhook() bot.remove_webhook()
# Set 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')) certificate=open(WEBHOOK_SSL_CERT, 'r'))
# Build ssl context # Build ssl context

View File

@ -4,10 +4,11 @@
# This is a simple echo bot using decorators and webhook with CherryPy # 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. # It echoes any incoming text messages and does not use the polling method.
import cherrypy
import telebot
import logging import logging
import cherrypy
import telebot
API_TOKEN = '<api_token>' API_TOKEN = '<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_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN) WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)
logger = telebot.logger logger = telebot.logger
telebot.logger.setLevel(logging.INFO) telebot.logger.setLevel(logging.INFO)
@ -70,7 +70,7 @@ def echo_message(message):
bot.remove_webhook() bot.remove_webhook()
# Set 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')) certificate=open(WEBHOOK_SSL_CERT, 'r'))
# Disable CherryPy requests log # Disable CherryPy requests log
@ -80,9 +80,9 @@ for handler in tuple(access_log.handlers):
# Start cherrypy server # Start cherrypy server
cherrypy.config.update({ cherrypy.config.update({
'server.socket_host': WEBHOOK_LISTEN, 'server.socket_host' : WEBHOOK_LISTEN,
'server.socket_port': WEBHOOK_PORT, 'server.socket_port' : WEBHOOK_PORT,
'server.ssl_module': 'builtin', 'server.ssl_module' : 'builtin',
'server.ssl_certificate': WEBHOOK_SSL_CERT, 'server.ssl_certificate': WEBHOOK_SSL_CERT,
'server.ssl_private_key': WEBHOOK_SSL_PRIV 'server.ssl_private_key': WEBHOOK_SSL_PRIV
}) })

View File

@ -4,11 +4,17 @@
# This is a simple echo bot using decorators and webhook with BaseHTTPServer # 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. # It echoes any incoming text messages and does not use the polling method.
import BaseHTTPServer try:
import ssl # Python 2
import telebot from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import logging except ImportError:
# Python 3
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
import ssl
import telebot
API_TOKEN = '<api_token>' API_TOKEN = '<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_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN) WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)
logger = telebot.logger logger = telebot.logger
telebot.logger.setLevel(logging.INFO) telebot.logger.setLevel(logging.INFO)
@ -38,7 +43,7 @@ bot = telebot.TeleBot(API_TOKEN)
# WebhookHandler, process webhook calls # WebhookHandler, process webhook calls
class WebhookHandler(BaseHTTPServer.BaseHTTPRequestHandler): class WebhookHandler(BaseHTTPRequestHandler):
server_version = "WebhookHandler/1.0" server_version = "WebhookHandler/1.0"
def do_HEAD(self): def do_HEAD(self):
@ -84,12 +89,12 @@ def echo_message(message):
bot.remove_webhook() bot.remove_webhook()
# Set 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')) certificate=open(WEBHOOK_SSL_CERT, 'r'))
# Start server # Start server
httpd = BaseHTTPServer.HTTPServer((WEBHOOK_LISTEN, WEBHOOK_PORT), httpd = HTTPServer((WEBHOOK_LISTEN, WEBHOOK_PORT),
WebhookHandler) WebhookHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, httpd.socket = ssl.wrap_socket(httpd.socket,
certfile=WEBHOOK_SSL_CERT, certfile=WEBHOOK_SSL_CERT,

View File

@ -4,11 +4,12 @@
# This is a simple echo bot using decorators and webhook with flask # 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. # It echoes any incoming text messages and does not use the polling method.
import flask
import telebot
import logging import logging
import time import time
import flask
import telebot
API_TOKEN = '<api_token>' API_TOKEN = '<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_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN) WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)
logger = telebot.logger logger = telebot.logger
telebot.logger.setLevel(logging.INFO) telebot.logger.setLevel(logging.INFO)
@ -77,7 +77,7 @@ bot.remove_webhook()
time.sleep(0.1) time.sleep(0.1)
# Set 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')) certificate=open(WEBHOOK_SSL_CERT, 'r'))
# Start flask server # Start flask server

View File

@ -1,8 +1,9 @@
import os import os
import telebot
from flask import Flask, request from flask import Flask, request
import telebot
TOKEN = '<api_token>' TOKEN = '<api_token>'
bot = telebot.TeleBot(TOKEN) bot = telebot.TeleBot(TOKEN)
server = Flask(__name__) server = Flask(__name__)

View File

@ -4,13 +4,15 @@
# 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 tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
import signal import signal
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import telebot
API_TOKEN = '<api_token>' API_TOKEN = '<api_token>'
WEBHOOK_CERT = "./cert.pem" WEBHOOK_CERT = "./cert.pem"
WEBHOOK_PKEY = "./pkey.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) bot = telebot.TeleBot(API_TOKEN)
class Root(tornado.web.RequestHandler): class Root(tornado.web.RequestHandler):
def get(self): def get(self):
self.write("Hi! This is webhook example!") self.write("Hi! This is webhook example!")
self.finish() self.finish()
class webhook_serv(tornado.web.RequestHandler): class webhook_serv(tornado.web.RequestHandler):
def get(self): def get(self):
self.write("What are you doing here?") self.write("What are you doing here?")
self.finish() self.finish()
def post(self): def post(self):
if "Content-Length" in self.request.headers and \ if "Content-Length" in self.request.headers and \
"Content-Type" in self.request.headers and \ "Content-Type" in self.request.headers and \
@ -52,21 +57,26 @@ class webhook_serv(tornado.web.RequestHandler):
else: else:
self.write("What are you doing here?") self.write("What are you doing here?")
self.finish() self.finish()
tornado.options.define("port", default=WEBHOOK_PORT, help="run on the given port", type=int) tornado.options.define("port", default=WEBHOOK_PORT, help="run on the given port", type=int)
is_closing = False is_closing = False
def signal_handler(signum, frame): def signal_handler(signum, frame):
global is_closing global is_closing
print("Exiting...") print("Exiting...")
is_closing = True is_closing = True
def try_exit(): def try_exit():
global is_closing global is_closing
if is_closing: if is_closing:
# clean up here # clean up here
tornado.ioloop.IOLoop.instance().stop() tornado.ioloop.IOLoop.instance().stop()
print("Exit success!") print("Exit success!")
# Handle '/start' and '/help' # Handle '/start' and '/help'
@bot.message_handler(commands=['help', 'start']) @bot.message_handler(commands=['help', 'start'])
def send_welcome(message): def send_welcome(message):
@ -74,6 +84,7 @@ def send_welcome(message):
("Hi there, I am EchoBot.\n" ("Hi there, I am EchoBot.\n"
"I am here to echo your kind words back to you.")) "I am here to echo your kind words back to you."))
bot.remove_webhook() bot.remove_webhook()
bot.set_webhook(url=WEBHOOK_URL_BASE, bot.set_webhook(url=WEBHOOK_URL_BASE,
certificate=open(WEBHOOK_CERT, 'r')) certificate=open(WEBHOOK_CERT, 'r'))
@ -86,9 +97,9 @@ application = tornado.web.Application([
]) ])
http_server = tornado.httpserver.HTTPServer(application, ssl_options={ http_server = tornado.httpserver.HTTPServer(application, ssl_options={
"certfile": WEBHOOK_CERT, "certfile": WEBHOOK_CERT,
"keyfile": WEBHOOK_PKEY, "keyfile" : WEBHOOK_PKEY,
}) })
http_server.listen(tornado.options.options.port) http_server.listen(tornado.options.options.port)
tornado.ioloop.PeriodicCallback(try_exit, 100).start() tornado.ioloop.PeriodicCallback(try_exit, 100).start()
tornado.ioloop.IOLoop.instance().start() tornado.ioloop.IOLoop.instance().start()

View File

@ -1,16 +1,15 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import print_function from __future__ import print_function
import threading import logging
import time
import re
import sys
import six
import os import os
import pickle import pickle
import re
import sys
import threading
import time
import logging import six
logger = logging.getLogger('TeleBot') logger = logging.getLogger('TeleBot')
formatter = logging.Formatter( formatter = logging.Formatter(
@ -34,6 +33,7 @@ class Handler:
""" """
Class for (next step|reply) handlers Class for (next step|reply) handlers
""" """
def __init__(self, callback, *args, **kwargs): def __init__(self, callback, *args, **kwargs):
self.callback = callback self.callback = callback
self.args = args self.args = args
@ -47,6 +47,7 @@ class Saver:
""" """
Class for saving (next step|reply) handlers Class for saving (next step|reply) handlers
""" """
def __init__(self, handlers, filename, delay): def __init__(self, handlers, filename, delay):
self.handlers = handlers self.handlers = handlers
self.filename = filename self.filename = filename
@ -1294,22 +1295,21 @@ class TeleBot:
was_poped = False was_poped = False
if chat_id in self.next_step_handlers.keys(): if chat_id in self.next_step_handlers.keys():
handlers = self.next_step_handlers.pop(chat_id, None) handlers = self.next_step_handlers.pop(chat_id, None)
if (handlers): if handlers:
for handler in handlers: for handler in handlers:
self._exec_task(handler["callback"], message, *handler["args"], **handler["kwargs"]) self._exec_task(handler["callback"], message, *handler["args"], **handler["kwargs"])
new_messages.pop(i) # removing message that detects with next_step_handler new_messages.pop(i) # removing message that detects with next_step_handler
was_poped = True was_poped = True
if self.next_step_saver is not None: if self.next_step_saver is not None:
self.next_step_saver.start_save_timer() self.next_step_saver.start_save_timer()
if (not was_poped): if not was_poped:
i += 1 i += 1
@staticmethod @staticmethod
def _build_handler_dict(handler, **filters): def _build_handler_dict(handler, **filters):
return { return {
'function': handler, 'function': handler,
'filters': filters 'filters' : filters
} }
def message_handler(self, commands=None, regexp=None, func=None, content_types=['text'], **kwargs): 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) TeleBot.__init__(self, *args, **kwargs)
@util.async_dec() @util.async_dec()
def enable_save_next_step_handlers(self, delay=120, filename="./.handler-saves/step.save", del_file_after_loading=True): def enable_save_next_step_handlers(self, delay=120, filename="./.handler-saves/step.save"):
return TeleBot.enable_save_next_step_handlers(self, delay, filename, del_file_after_loading) return TeleBot.enable_save_next_step_handlers(self, delay, filename)
@util.async_dec() @util.async_dec()
def enable_save_reply_handlers(self, delay=120, filename="./.handler-saves/reply.save", del_file_after_loading=True): def enable_save_reply_handlers(self, delay=120, filename="./.handler-saves/reply.save"):
return TeleBot.enable_save_reply_handlers(self, delay, filename, del_file_after_loading) return TeleBot.enable_save_reply_handlers(self, delay, filename)
@util.async_dec() @util.async_dec()
def disable_save_next_step_handlers(self): def disable_save_next_step_handlers(self):
@ -1512,14 +1512,12 @@ class AsyncTeleBot(TeleBot):
return TeleBot.enable_save_reply_handlers(self) return TeleBot.enable_save_reply_handlers(self)
@util.async_dec() @util.async_dec()
def load_next_step_handlers(self, filename="./.handler-saves/step.save"): def load_next_step_handlers(self, filename="./.handler-saves/step.save", del_file_after_loading=True):
return TeleBot.load_next_step_handlers(self, filename) 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"):
return TeleBot.load_reply_handlers(self, filename)
@util.async_dec() @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() @util.async_dec()
def get_me(self): def get_me(self):

View File

@ -99,7 +99,6 @@ def get_file(token, file_id):
def get_file_url(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) 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 disable_web_page_preview:
:param reply_to_message_id: :param reply_to_message_id:
:param reply_markup: :param reply_markup:
:param parse_mode:
:param disable_notification:
:return: :return:
""" """
method_url = r'sendMessage' 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 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_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 :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' method_url = r'sendInvoice'
payload = {'chat_id': chat_id, 'title': title, 'description': description, 'payload': invoice_payload, payload = {'chat_id': chat_id, 'title': title, 'description': description, 'payload': invoice_payload,

View File

@ -457,11 +457,11 @@ class Message(JsonDeserializable):
if not entities: if not entities:
return text return text
_subs = { _subs = {
"bold": "<b>{text}</b>", "bold" : "<b>{text}</b>",
"italic": "<i>{text}</i>", "italic" : "<i>{text}</i>",
"pre": "<pre>{text}</pre>", "pre" : "<pre>{text}</pre>",
"code": "<code>{text}</code>", "code" : "<code>{text}</code>",
"url": "<a href=\"{url}\">{text}</a>", "url" : "<a href=\"{url}\">{text}</a>",
"text_link": "<a href=\"{url}\">{text}</a>" "text_link": "<a href=\"{url}\">{text}</a>"
} }
if hasattr(self, "custom_subs"): if hasattr(self, "custom_subs"):
@ -469,6 +469,7 @@ class Message(JsonDeserializable):
_subs[type] = self.custom_subs[type] _subs[type] = self.custom_subs[type]
utf16_text = text.encode("utf-16-le") utf16_text = text.encode("utf-16-le")
html_text = "" html_text = ""
def func(text, type=None, url=None, user=None): def func(text, type=None, url=None, user=None):
text = text.decode("utf-16-le") text = text.decode("utf-16-le")
if type == "text_mention": if type == "text_mention":
@ -501,6 +502,7 @@ class Message(JsonDeserializable):
def html_caption(self): def html_caption(self):
return self.__html_text(self.caption, self.caption_entities) return self.__html_text(self.caption, self.caption_entities)
class MessageEntity(JsonDeserializable): class MessageEntity(JsonDeserializable):
@classmethod @classmethod
def de_json(cls, json_string): def de_json(cls, json_string):
@ -598,29 +600,6 @@ class Document(JsonDeserializable):
self.file_size = file_size 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): class Video(JsonDeserializable):
@classmethod @classmethod
def de_json(cls, json_string): def de_json(cls, json_string):
@ -1092,7 +1071,7 @@ class InputVenueMessageContent(Dictionaryable):
def to_dic(self): def to_dic(self):
json_dic = {'latitude': self.latitude, 'longitude': self.longitude, 'title': self.title, json_dic = {'latitude': self.latitude, 'longitude': self.longitude, 'title': self.title,
'address': self.address} 'address' : self.address}
if self.foursquare_id: if self.foursquare_id:
json_dic['foursquare_id'] = self.foursquare_id json_dic['foursquare_id'] = self.foursquare_id
return json_dic return json_dic

View File

@ -1,10 +1,11 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import random import random
import re
import string import string
import sys
import threading import threading
import traceback import traceback
import re
import sys
import six import six
from six import string_types from six import string_types
@ -243,7 +244,7 @@ def extract_arguments(text):
:param text: String to extract the arguments from a command :param text: String to extract the arguments from a command
:return: the arguments if `text` is a command (according to is_command), else None. :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) result = regexp.match(text)
return result.group(2) if is_command(text) else None return result.group(2) if is_command(text) else None

View File

@ -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) 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 assert new_msg.message_id
def create_text_message(self, text): @staticmethod
def create_text_message(text):
params = {'text': text} params = {'text': text}
chat = types.User(11, False, 'test') chat = types.User(11, False, 'test')
return types.Message(1, None, None, chat, 'text', params, "") return types.Message(1, None, None, chat, 'text', params, "")

View File

@ -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}}' 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) msg = types.Message.de_json(json_string)
assert msg.sticker.height == 368 assert msg.sticker.height == 368
assert msg.sticker.thumb == None assert msg.sticker.thumb is None
assert msg.content_type == 'sticker' assert msg.content_type == 'sticker'
@ -113,6 +113,7 @@ def test_json_voice():
assert voice.duration == 0 assert voice.duration == 0
assert voice.file_size == 10481 assert voice.file_size == 10481
def test_json_update(): 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"}}' 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) update = types.Update.de_json(json_string)
@ -120,6 +121,7 @@ def test_json_update():
assert update.message.message_id == 241 assert update.message.message_id == 241
assert update.message.from_user.id == 9734 assert update.message.from_user.id == 9734
def test_json_chat(): def test_json_chat():
json_string = r'{"id": -111111,"title": "Test Title","type": "group"}' json_string = r'{"id": -111111,"title": "Test Title","type": "group"}'
chat = types.Chat.de_json(json_string) chat = types.Chat.de_json(json_string)
@ -127,6 +129,7 @@ def test_json_chat():
assert chat.type == 'group' assert chat.type == 'group'
assert chat.title == 'Test Title' assert chat.title == 'Test Title'
def test_InlineQueryResultCachedPhoto(): def test_InlineQueryResultCachedPhoto():
iq = types.InlineQueryResultCachedPhoto('aaa', 'Fileid') iq = types.InlineQueryResultCachedPhoto('aaa', 'Fileid')
json_str = iq.to_json() json_str = iq.to_json()
@ -143,6 +146,7 @@ def test_InlineQueryResultCachedPhoto_with_title():
assert 'Title' in json_str assert 'Title' in json_str
assert 'caption' not in json_str assert 'caption' not in json_str
def test_InlineQueryResultCachedPhoto_with_markup(): def test_InlineQueryResultCachedPhoto_with_markup():
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"))
@ -154,4 +158,3 @@ def test_InlineQueryResultCachedPhoto_with_markup():
assert 'Title' in json_str assert 'Title' in json_str
assert 'caption' not in json_str assert 'caption' not in json_str
assert 'reply_markup' in json_str assert 'reply_markup' in json_str