From cc7ab58ed8851a341e900b7d3b738fa6558af33f Mon Sep 17 00:00:00 2001 From: Pieter van den Ham Date: Tue, 8 Sep 2015 17:38:44 +0200 Subject: [PATCH] Fixed some typos in the README Fixed a bug where TeleBot would ignore KeyboardInterrupt events --- README.md | 10 +++++----- telebot/__init__.py | 16 +++++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index cf303f6..28bb895 100644 --- a/README.md +++ b/README.md @@ -130,15 +130,16 @@ All [API methods](https://core.telegram.org/bots/api#available-methods) are loca Outlined below are some general use cases of the API. #### Message handlers -A message handler is a function that is decorated with the `message_handler` decorator of a TeleBot instance. The following examples illustrate the possibilities of message handlers. Message handlers consists of one or multiple filters. -A message handler is declared in the following way (provided `bot` is an instance of TeleBot): +A message handler is a function that is decorated with the `message_handler` decorator of a TeleBot instance. Message handlers consist of one or multiple filters. +Each filter much return True for a certain message in order for a message handler to become eligible to handle that message. A message handler is declared in the following way (provided `bot` is an instance of TeleBot): ```python @bot.message_handler(filters) def function_name(message): bot.reply_to(message, "This is a message handler") ``` `function_name` is not bound to any restrictions. Any function name is permitted with message handlers. The function must accept at most one argument, which will be the message that the function must handle. -`filters` is a list of keyword arguments. Each filter must return True for a certain message in order for the message handler to become eligible to handle that message. +`filters` is a list of keyword arguments. +A filter is declared in the following manner: `name=argument`. One handler may have multiple filters. TeleBot supports the following filters: |name|argument(s)|Condition| @@ -146,9 +147,8 @@ TeleBot supports the following filters: |content_types|list of strings (default `['text']`)|`True` if message.content_type is in the list of strings.| |regexp|a regular expression as a string|`True` if `re.search(regexp_arg)` returns `True` and `message.content_type == 'text'`| |commands|list of strings|`True` if `message.content_type == 'text'` and `message.text` starts with a command that is in the list of strings.| -|func|a function (lambda or function reference)|True if the function or lambda reference returns True +|func|a function (lambda or function reference)|`True` if the lambda or function reference returns `True` -A filter is declared in the following manner: `name=argument`. Here are some examples of using the filters and message handlers: ```python import telebot diff --git a/telebot/__init__.py b/telebot/__init__.py index cbe5a51..53f29c4 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -3,13 +3,12 @@ from __future__ import print_function import threading import time +import re +from telebot import apihelper, types, util import logging logging.basicConfig() logger = logging.getLogger('Telebot') -import re - -from telebot import apihelper, types, util """ Module : telebot @@ -65,7 +64,7 @@ class TeleBot: Registered listeners and applicable message handlers will be notified when a new message arrives. :raises ApiException when a call has failed. """ - updates = apihelper.get_updates(self.token, offset=(self.last_update_id + 1), timeout=20) + updates = apihelper.get_updates(self.token, offset=(self.last_update_id + 1), timeout=3) new_messages = [] for update in updates: if update['update_id'] > self.last_update_id: @@ -109,7 +108,14 @@ class TeleBot: self.polling_thread.start() if block: - self.__stop_polling.wait() + while self.polling_thread.is_alive: + try: + time.sleep(.1) + except KeyboardInterrupt: + logger.info("TeleBot: Received KeyboardInterrupt: Stopping") + self.stop_polling() + self.polling_thread.join() + break def __polling(self, none_stop, interval): logger.info('TeleBot: Started polling.')