2015-06-26 09:56:25 +03:00
|
|
|
# pyTelegramBotAPI
|
|
|
|
Python Telegram bot api.
|
2015-06-26 09:58:20 +03:00
|
|
|
|
2015-06-26 17:52:09 +03:00
|
|
|
## How to install
|
|
|
|
|
|
|
|
* Install from source
|
|
|
|
|
|
|
|
```
|
|
|
|
$ git clone https://github.com/eternnoir/pyTelegramBotAPI.git
|
|
|
|
$ cd pyTelegramBotAPI
|
|
|
|
$ python setup.py install
|
|
|
|
```
|
|
|
|
|
|
|
|
* Install by pip
|
|
|
|
|
|
|
|
```
|
|
|
|
$ pip install pyTelegramBotAPI
|
|
|
|
```
|
|
|
|
|
2015-06-26 09:58:20 +03:00
|
|
|
## Example
|
|
|
|
|
2015-06-26 13:05:23 +03:00
|
|
|
* Send Message
|
|
|
|
|
2015-06-26 09:58:20 +03:00
|
|
|
```python
|
|
|
|
import telebot
|
|
|
|
|
|
|
|
TOKEN = '<token string>'
|
|
|
|
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
# tb.send_message(chatid,message)
|
|
|
|
print tb.send_message(281281, 'gogo power ranger')
|
|
|
|
```
|
2015-06-26 13:05:23 +03:00
|
|
|
|
|
|
|
* Echo Bot
|
|
|
|
|
|
|
|
```python
|
2015-06-26 13:11:11 +03:00
|
|
|
import telebot
|
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
|
|
|
|
|
|
|
|
|
|
|
def listener(*messages):
|
2015-06-26 13:11:11 +03:00
|
|
|
"""
|
|
|
|
When new message get will call this function.
|
|
|
|
:param messages:
|
|
|
|
:return:
|
|
|
|
"""
|
2015-06-26 13:05:23 +03:00
|
|
|
for m in messages:
|
|
|
|
chatid = m.chat.id
|
2015-06-28 12:21:11 +03:00
|
|
|
if m.content_type == 'text'
|
|
|
|
text = m.text
|
|
|
|
tb.send_message(chatid, text)
|
2015-06-26 13:05:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
tb = telebot.TeleBot(TOKEN)
|
|
|
|
tb.get_update() # cache exist message
|
2015-06-26 13:11:11 +03:00
|
|
|
tb.set_update_listener(listener) #register listener
|
2015-06-26 13:05:23 +03:00
|
|
|
tb.polling(3)
|
|
|
|
while True:
|
|
|
|
time.sleep(20)
|
|
|
|
```
|
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>'
|
|
|
|
tb = telebot.TeleBot(TOKEN) #create new Telegram Bot object
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
# sendAudio
|
|
|
|
audio = open('/tmp/audio.ogg', 'rb')
|
|
|
|
tb.send_audio(chat_id, audio)
|
|
|
|
|
|
|
|
# sendDocument
|
|
|
|
doc = open('/tmp/file.txt', 'rb')
|
|
|
|
tb.send_document(chat_id, doc)
|
|
|
|
|
|
|
|
# sendSticker
|
|
|
|
sti = open('/tmp/sti.webp', 'rb')
|
|
|
|
tb.send_sticker(chat_id, sti)
|
|
|
|
|
|
|
|
# sendVideo
|
|
|
|
video = open('/tmp/video.mp4', 'rb')
|
|
|
|
tb.send_video(chat_id, video)
|
|
|
|
|
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
|
|
|
|
# action_string can be : typing,upload_photo,record_video,upload_video,record_audio,upload_audio,upload_document,
|
|
|
|
# find_location.
|
|
|
|
tb.send_chat_action(chat_id, action_string)
|
|
|
|
|
2015-06-27 05:25:22 +03:00
|
|
|
```
|
|
|
|
|
2015-06-28 12:35:47 +03:00
|
|
|
## Message notifier
|
|
|
|
|
|
|
|
* Define listener function
|
|
|
|
|
|
|
|
```python
|
|
|
|
def listener1(*messages):
|
|
|
|
for m in messages:
|
|
|
|
chatid = m.chat.id
|
|
|
|
if m.content_type == 'text'
|
|
|
|
text = m.text
|
|
|
|
tb.send_message(chatid, text)
|
|
|
|
```
|
|
|
|
|
|
|
|
* Use ***set_update_listener*** method to add listener function to telebot.
|
|
|
|
* Start polling or call get_update(). If get new updates, telebot will call listener and pass messages to listener.
|
|
|
|
* use Message's content_type attribute to check message type. Now Message support content_type:
|
|
|
|
* text
|
|
|
|
* audio
|
|
|
|
* document
|
|
|
|
* photo
|
|
|
|
* sticker
|
|
|
|
* video
|
|
|
|
* location
|
|
|
|
|
2015-06-26 16:56:49 +03:00
|
|
|
## TODO
|
|
|
|
|
|
|
|
- [x] getMe
|
2015-06-26 17:16:11 +03:00
|
|
|
- [x] sendMessage
|
2015-06-26 17:35:52 +03:00
|
|
|
- [x] forwardMessage
|
2015-06-26 20:53:07 +03:00
|
|
|
- [x] sendPhoto
|
|
|
|
- [x] sendAudio
|
|
|
|
- [x] sendDocument
|
|
|
|
- [x] sendSticker
|
|
|
|
- [x] sendVideo
|
2015-06-27 17:15:59 +03:00
|
|
|
- [x] sendLocation
|
2015-06-28 13:03:06 +03:00
|
|
|
- [x] sendChatAction
|
2015-06-26 16:56:49 +03:00
|
|
|
- [ ] getUserProfilePhotos
|
2015-06-28 13:07:27 +03:00
|
|
|
- [ ] getUpdat(contact and chat message not yet)
|