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

Fixed some typos in the README

Fixed a bug where TeleBot would ignore KeyboardInterrupt events
This commit is contained in:
Pieter van den Ham 2015-09-08 17:38:44 +02:00
parent edf1694606
commit cc7ab58ed8
2 changed files with 16 additions and 10 deletions

View File

@ -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

View File

@ -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.')