2015-06-26 09:56:25 +03:00
|
|
|
# pyTelegramBotAPI
|
2015-06-28 17:34:27 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
A Python implementation for the Telegram Bot API.
|
2015-06-28 17:34:27 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
See [https://core.telegram.org/bots/api](https://core.telegram.org/bots/api)
|
2015-06-26 09:58:20 +03:00
|
|
|
|
2015-07-25 22:41:19 +03:00
|
|
|
[![Build Status](https://travis-ci.org/eternnoir/pyTelegramBotAPI.svg?branch=master)](https://travis-ci.org/eternnoir/pyTelegramBotAPI)
|
|
|
|
|
2015-06-26 17:52:09 +03:00
|
|
|
## How to install
|
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
Python 2 or Python 3 is required.
|
|
|
|
|
2015-06-26 17:52:09 +03:00
|
|
|
* Install from source
|
|
|
|
|
|
|
|
```
|
|
|
|
$ git clone https://github.com/eternnoir/pyTelegramBotAPI.git
|
|
|
|
$ cd pyTelegramBotAPI
|
|
|
|
$ python setup.py install
|
|
|
|
```
|
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
* or install with pip
|
2015-06-26 17:52:09 +03:00
|
|
|
|
|
|
|
```
|
|
|
|
$ pip install pyTelegramBotAPI
|
|
|
|
```
|
|
|
|
|
2015-06-26 09:58:20 +03:00
|
|
|
## Example
|
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
* Sending a message.
|
2015-06-26 13:05:23 +03:00
|
|
|
|
2015-06-26 09:58:20 +03:00
|
|
|
```python
|
|
|
|
import telebot
|
|
|
|
|
|
|
|
TOKEN = '<token string>'
|
|
|
|
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
2015-07-03 00:17:48 +03:00
|
|
|
# tb.send_message(chatid, message)
|
|
|
|
tb.send_message(281281, 'gogo power ranger')
|
2015-06-26 09:58:20 +03:00
|
|
|
```
|
2015-06-26 13:05:23 +03:00
|
|
|
|
|
|
|
* Echo Bot
|
|
|
|
|
|
|
|
```python
|
2015-06-26 13:11:11 +03:00
|
|
|
import telebot
|
2015-07-03 01:09:58 +03:00
|
|
|
import time
|
2015-06-26 13:05:23 +03:00
|
|
|
|
2015-06-26 13:11:11 +03:00
|
|
|
TOKEN = '<token_string>'
|
2015-06-26 13:05:23 +03:00
|
|
|
|
|
|
|
|
2015-07-11 08:05:13 +03:00
|
|
|
def listener(messages):
|
2015-06-26 13:11:11 +03:00
|
|
|
"""
|
2015-07-03 00:17:48 +03:00
|
|
|
When new messages arrive TeleBot will call this function.
|
2015-06-26 13:11:11 +03:00
|
|
|
"""
|
2015-06-26 13:05:23 +03:00
|
|
|
for m in messages:
|
|
|
|
chatid = m.chat.id
|
2015-07-03 02:54:53 +03:00
|
|
|
if m.content_type == 'text':
|
2015-06-28 12:21:11 +03:00
|
|
|
text = m.text
|
|
|
|
tb.send_message(chatid, text)
|
2015-06-26 13:05:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
2015-06-26 13:11:11 +03:00
|
|
|
tb.set_update_listener(listener) #register listener
|
2015-07-03 00:17:48 +03:00
|
|
|
tb.polling()
|
2015-07-15 08:14:46 +03:00
|
|
|
#Use none_stop flag let polling will not stop when get new message occur error.
|
|
|
|
tb.polling(none_stop=True)
|
2015-07-15 08:09:24 +03:00
|
|
|
# Interval setup. Sleep 3 secs between request new message.
|
2015-07-15 08:14:46 +03:00
|
|
|
tb.polling(interval=3)
|
2015-07-03 00:17:48 +03:00
|
|
|
|
|
|
|
while True: # Don't let the main Thread end.
|
|
|
|
pass
|
2015-06-26 13:05:23 +03:00
|
|
|
```
|
2015-06-26 16:56:49 +03:00
|
|
|
|
2015-06-27 05:25:22 +03:00
|
|
|
## TeleBot API usage
|
|
|
|
|
|
|
|
```python
|
|
|
|
import telebot
|
|
|
|
import time
|
|
|
|
|
|
|
|
TOKEN = '<token_string>'
|
2015-07-03 00:17:48 +03:00
|
|
|
tb = telebot.TeleBot(TOKEN) #create a new Telegram Bot object
|
2015-06-27 05:25:22 +03:00
|
|
|
|
2015-07-07 06:51:11 +03:00
|
|
|
# TeleBot will not create thread for message listener. Default is True.
|
|
|
|
tb = telebot.TeleBot(TOKEN, False)
|
|
|
|
|
|
|
|
# 4 Thread worker for message listener.
|
|
|
|
tb = telebot.TeleBot(TOKEN, True, 4)
|
|
|
|
|
2015-07-24 04:22:02 +03:00
|
|
|
# Setup telebot handler to telebot logger. If you want to get some information from telebot.
|
|
|
|
# More information at Logging section
|
|
|
|
handler = logging.StreamHandler(sys.stdout)
|
|
|
|
telebot.logger.addHandler(handler)
|
|
|
|
telebot.logger.setLevel(logging.INFO)
|
|
|
|
|
2015-06-27 05:25:22 +03:00
|
|
|
# getMe
|
|
|
|
user = tb.get_me()
|
|
|
|
|
|
|
|
# sendMessage
|
|
|
|
tb.send_message(chatid, text)
|
|
|
|
|
|
|
|
# forwardMessage
|
|
|
|
# tb.forward_message(10894,926,3)
|
|
|
|
tb.forward_message(to_chat_id, from_chat_id, message_id)
|
|
|
|
|
|
|
|
# sendPhoto
|
|
|
|
photo = open('/tmp/photo.png', 'rb')
|
|
|
|
tb.send_photo(chat_id, photo)
|
2015-07-15 08:14:46 +03:00
|
|
|
file_id = 'AAAaaaZZZzzz'
|
|
|
|
tb.send_photo(chat_id, file_id)
|
2015-06-27 05:25:22 +03:00
|
|
|
|
|
|
|
# sendAudio
|
2015-08-21 18:09:35 +03:00
|
|
|
audio = open('/tmp/audio.mp3', 'rb')
|
2015-06-27 05:25:22 +03:00
|
|
|
tb.send_audio(chat_id, audio)
|
2015-08-21 18:09:35 +03:00
|
|
|
## sendAudio with duration, performer and title.
|
|
|
|
tb.send_audio(CHAT_ID, file_data, 1, 'eternnoir', 'pyTelegram')
|
2015-07-15 08:14:46 +03:00
|
|
|
file_id = 'AAAaaaZZZzzz'
|
|
|
|
tb.send_audio(chat_id, file_id)
|
2015-06-27 05:25:22 +03:00
|
|
|
|
2015-08-21 18:09:35 +03:00
|
|
|
# sendVoice
|
|
|
|
voice = open('/tmp/voice.ogg', 'rb')
|
|
|
|
tb.send_voice(chat_id, voice)
|
|
|
|
file_id = 'AAAaaaZZZzzz'
|
|
|
|
tb.send_voice(chat_id, file_id)
|
|
|
|
|
2015-06-27 05:25:22 +03:00
|
|
|
# sendDocument
|
|
|
|
doc = open('/tmp/file.txt', 'rb')
|
|
|
|
tb.send_document(chat_id, doc)
|
2015-07-15 08:14:46 +03:00
|
|
|
file_id = 'AAAaaaZZZzzz'
|
|
|
|
tb.send_document(chat_id, file_id)
|
2015-06-27 05:25:22 +03:00
|
|
|
|
|
|
|
# sendSticker
|
|
|
|
sti = open('/tmp/sti.webp', 'rb')
|
|
|
|
tb.send_sticker(chat_id, sti)
|
2015-07-15 08:14:46 +03:00
|
|
|
file_id = 'AAAaaaZZZzzz'
|
|
|
|
tb.send_sticker(chat_id, file_id)
|
2015-06-27 05:25:22 +03:00
|
|
|
|
|
|
|
# sendVideo
|
|
|
|
video = open('/tmp/video.mp4', 'rb')
|
|
|
|
tb.send_video(chat_id, video)
|
2015-07-15 08:14:46 +03:00
|
|
|
file_id = 'AAAaaaZZZzzz'
|
|
|
|
tb.send_video(chat_id, file_id)
|
2015-06-27 05:25:22 +03:00
|
|
|
|
2015-06-27 17:11:18 +03:00
|
|
|
# sendLocation
|
|
|
|
tb.send_location(chat_id, lat, lon)
|
|
|
|
|
2015-06-28 12:58:15 +03:00
|
|
|
# sendChatAction
|
2015-07-03 00:17:48 +03:00
|
|
|
# action_string can be one of the following strings: 'typing', 'upload_photo', 'record_video', 'upload_video',
|
|
|
|
# 'record_audio', 'upload_audio', 'upload_document' or 'find_location'.
|
2015-06-28 12:58:15 +03:00
|
|
|
tb.send_chat_action(chat_id, action_string)
|
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
# Use the ReplyKeyboardMarkup class.
|
2015-06-30 08:20:44 +03:00
|
|
|
# Thanks pevdh.
|
2015-07-01 04:55:25 +03:00
|
|
|
from telebot import types
|
|
|
|
|
2015-06-30 08:20:44 +03:00
|
|
|
markup = types.ReplyKeyboardMarkup()
|
|
|
|
markup.add('a', 'v', 'd')
|
2015-07-03 00:17:48 +03:00
|
|
|
tb.send_message(chat_id, message, reply_markup=markup)
|
|
|
|
|
|
|
|
# or add strings one row at a time:
|
2015-06-30 08:20:44 +03:00
|
|
|
markup = types.ReplyKeyboardMarkup()
|
|
|
|
markup.row('a', 'v')
|
|
|
|
markup.row('c', 'd', 'e')
|
2015-07-03 00:17:48 +03:00
|
|
|
tb.send_message(chat_id, message, reply_markup=markup)
|
2015-06-30 08:20:44 +03:00
|
|
|
|
2015-06-27 05:25:22 +03:00
|
|
|
```
|
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
## Creating a Telegram bot with the pyTelegramBotAPI
|
2015-08-01 16:03:40 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
There are two ways to define a Telegram Bot with the pyTelegramBotAPI.
|
|
|
|
### The listener mechanism
|
2015-08-01 16:03:40 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
* First, create a TeleBot instance.
|
2015-08-01 16:03:40 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
```python
|
|
|
|
import telebot
|
2015-06-28 12:35:47 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
TOKEN = '<token string>'
|
2015-06-28 12:35:47 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
bot = telebot.TeleBot(TOKEN)
|
|
|
|
```
|
2015-08-01 16:03:40 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
* Then, define a listener function.
|
2015-08-01 16:03:40 +03:00
|
|
|
|
2015-06-28 12:35:47 +03:00
|
|
|
```python
|
2015-07-03 00:17:48 +03:00
|
|
|
def echo_messages(*messages):
|
|
|
|
"""
|
|
|
|
Echoes all incoming messages of content_type 'text'.
|
|
|
|
"""
|
2015-06-28 12:35:47 +03:00
|
|
|
for m in messages:
|
|
|
|
chatid = m.chat.id
|
2015-07-03 02:54:53 +03:00
|
|
|
if m.content_type == 'text':
|
2015-06-28 12:35:47 +03:00
|
|
|
text = m.text
|
2015-07-03 00:17:48 +03:00
|
|
|
bot.send_message(chatid, text)
|
2015-06-28 12:35:47 +03:00
|
|
|
```
|
2015-08-01 16:03:40 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
* Now, register your listener with the TeleBot instance and call TeleBot#polling()
|
2015-08-01 16:03:40 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
```python
|
|
|
|
bot.set_update_listener(echo_messages)
|
|
|
|
bot.polling()
|
2015-06-28 12:35:47 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
while True: # Don't let the main Thread end.
|
|
|
|
pass
|
|
|
|
```
|
2015-08-01 16:03:40 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
* use Message's content_type attribute to check the type of Message. Now Message supports content types:
|
2015-06-28 12:35:47 +03:00
|
|
|
* text
|
|
|
|
* audio
|
|
|
|
* document
|
|
|
|
* photo
|
|
|
|
* sticker
|
|
|
|
* video
|
|
|
|
* location
|
2015-07-03 05:26:03 +03:00
|
|
|
* contact
|
|
|
|
* new_chat_participant
|
|
|
|
* left_chat_participant
|
|
|
|
* new_chat_title
|
|
|
|
* new_chat_photo
|
|
|
|
* delete_chat_photo
|
|
|
|
* group_chat_created
|
2015-07-03 00:17:48 +03:00
|
|
|
* That's it!
|
|
|
|
|
|
|
|
### The decorator mechanism
|
2015-08-01 16:03:40 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
* First, create a TeleBot instance.
|
2015-08-01 16:03:40 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
```python
|
|
|
|
import telebot
|
|
|
|
|
|
|
|
TOKEN = '<token string>'
|
|
|
|
|
|
|
|
bot = telebot.TeleBot(TOKEN)
|
|
|
|
```
|
2015-08-01 16:03:40 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
* Next, define all of your so-called message handlers and decorate them with @bot.message_handler
|
2015-08-01 16:03:40 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
```python
|
|
|
|
# Handle /start and /help
|
|
|
|
@bot.message_handler(commands=['start', 'help'])
|
|
|
|
def command_help(message):
|
2015-07-03 02:54:53 +03:00
|
|
|
bot.reply_to(message, "Hello, did someone call for help?")
|
2015-07-03 00:17:48 +03:00
|
|
|
|
|
|
|
# Handles all messages which text matches the regex regexp.
|
|
|
|
# See https://en.wikipedia.org/wiki/Regular_expression
|
|
|
|
# This regex matches all sent url's.
|
|
|
|
@bot.message_handler(regexp='((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)')
|
|
|
|
def command_url(message):
|
2015-07-03 02:54:53 +03:00
|
|
|
bot.reply_to(message, "I shouldn't open that url, should I?")
|
2015-07-03 00:17:48 +03:00
|
|
|
|
|
|
|
# Handle all sent documents of type 'text/plain'.
|
|
|
|
@bot.message_handler(func=lambda message: message.document.mime_type == 'text/plain', content_types=['document'])
|
|
|
|
def command_handle_document(message):
|
2015-07-03 02:54:53 +03:00
|
|
|
bot.reply_to(message, "Document received, sir!")
|
2015-07-03 00:17:48 +03:00
|
|
|
|
|
|
|
# Default command handler. A lambda expression which always returns True is used for this purpose.
|
|
|
|
@bot.message_handler(func=lambda message: True, content_types=['audio', 'video', 'document', 'text', 'location', 'contact', 'sticker'])
|
|
|
|
def default_command(message):
|
2015-07-03 02:54:53 +03:00
|
|
|
bot.reply_to(message, "This is the default command handler.")
|
2015-07-03 00:17:48 +03:00
|
|
|
```
|
2015-08-01 16:03:40 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
* And finally, call bot.polling()
|
2015-08-01 16:03:40 +03:00
|
|
|
|
2015-07-03 00:17:48 +03:00
|
|
|
```python
|
|
|
|
bot.polling()
|
|
|
|
|
|
|
|
while True: # Don't end the main thread.
|
|
|
|
pass
|
|
|
|
```
|
|
|
|
Use whichever mechanism fits your purpose! It is even possible to mix and match.
|
2015-06-28 12:35:47 +03:00
|
|
|
|
2015-07-20 05:38:33 +03:00
|
|
|
## Logging
|
|
|
|
|
|
|
|
Now you can use Telebot module logger to log some information in Telebot. Use `telebot.logger` to get
|
|
|
|
Telebot module logger.
|
|
|
|
|
|
|
|
```python
|
|
|
|
logger = telebot.logger
|
|
|
|
formatter = logging.Formatter('[%(asctime)s] %(thread)d {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
|
|
|
|
'%m-%d %H:%M:%S')
|
|
|
|
ch = logging.StreamHandler(sys.stdout)
|
|
|
|
logger.addHandler(ch)
|
|
|
|
logger.setLevel(logging.DEBUG) # or use logging.INFO
|
|
|
|
ch.setFormatter(formatter)
|
|
|
|
```
|
|
|
|
|
2015-07-17 16:40:33 +03:00
|
|
|
## Telegram Chat Group
|
|
|
|
|
|
|
|
Get help. Discuss. Chat.
|
|
|
|
|
|
|
|
Join [pyTelegramBotAPI Chat Group](https://telegram.me/joinchat/067e22c60035523fda8f6025ee87e30b).
|
|
|
|
|
2015-07-09 05:38:35 +03:00
|
|
|
## Examples
|
|
|
|
|
|
|
|
* [Echo Bot](https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/echo_bot.py)
|
|
|
|
* [Deep Linking](https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/deep_linking.py)
|
2015-08-01 05:20:41 +03:00
|
|
|
* [next_step_handler Example](https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/step_example.py)
|
2015-07-09 05:38:35 +03:00
|
|
|
|