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

Update readme.

This commit is contained in:
eternnoir 2015-08-01 21:03:40 +08:00
parent 98f126d249
commit a12ea63858

View File

@ -156,9 +156,12 @@ tb.send_message(chat_id, message, reply_markup=markup)
``` ```
## Creating a Telegram bot with the pyTelegramBotAPI ## Creating a Telegram bot with the pyTelegramBotAPI
There are two ways to define a Telegram Bot with the pyTelegramBotAPI. There are two ways to define a Telegram Bot with the pyTelegramBotAPI.
### The listener mechanism ### The listener mechanism
* First, create a TeleBot instance. * First, create a TeleBot instance.
```python ```python
import telebot import telebot
@ -166,7 +169,9 @@ TOKEN = '<token string>'
bot = telebot.TeleBot(TOKEN) bot = telebot.TeleBot(TOKEN)
``` ```
* Then, define a listener function. * Then, define a listener function.
```python ```python
def echo_messages(*messages): def echo_messages(*messages):
""" """
@ -178,7 +183,9 @@ def echo_messages(*messages):
text = m.text text = m.text
bot.send_message(chatid, text) bot.send_message(chatid, text)
``` ```
* Now, register your listener with the TeleBot instance and call TeleBot#polling() * Now, register your listener with the TeleBot instance and call TeleBot#polling()
```python ```python
bot.set_update_listener(echo_messages) bot.set_update_listener(echo_messages)
bot.polling() bot.polling()
@ -186,6 +193,7 @@ bot.polling()
while True: # Don't let the main Thread end. while True: # Don't let the main Thread end.
pass pass
``` ```
* use Message's content_type attribute to check the type of Message. Now Message supports content types: * use Message's content_type attribute to check the type of Message. Now Message supports content types:
* text * text
* audio * audio
@ -204,7 +212,9 @@ while True: # Don't let the main Thread end.
* That's it! * That's it!
### The decorator mechanism ### The decorator mechanism
* First, create a TeleBot instance. * First, create a TeleBot instance.
```python ```python
import telebot import telebot
@ -212,7 +222,9 @@ TOKEN = '<token string>'
bot = telebot.TeleBot(TOKEN) bot = telebot.TeleBot(TOKEN)
``` ```
* Next, define all of your so-called message handlers and decorate them with @bot.message_handler * Next, define all of your so-called message handlers and decorate them with @bot.message_handler
```python ```python
# Handle /start and /help # Handle /start and /help
@bot.message_handler(commands=['start', 'help']) @bot.message_handler(commands=['start', 'help'])
@ -236,7 +248,9 @@ def command_handle_document(message):
def default_command(message): def default_command(message):
bot.reply_to(message, "This is the default command handler.") bot.reply_to(message, "This is the default command handler.")
``` ```
* And finally, call bot.polling() * And finally, call bot.polling()
```python ```python
bot.polling() bot.polling()