1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00
Go to file
2015-07-02 11:19:38 +08:00
telebot Message support contact. 2015-07-02 11:19:38 +08:00
tests Message support contact. 2015-07-02 11:19:38 +08:00
.gitignore First Init. 2015-06-26 14:55:13 +08:00
.travis.yml add python3 to travis test, add python3 to readme 2015-07-01 10:17:45 +07:00
LICENSE Initial commit 2015-06-26 14:56:25 +08:00
README.md Update TODO 2015-07-01 19:44:49 +02:00
requirements.txt Add requirements. 2015-06-26 18:14:51 +08:00
setup.py Support ReplyKeyboardMarkup object. 2015-06-30 13:20:44 +08:00

pyTelegramBotAPI

Python Telegram Bot API.

https://core.telegram.org/bots/api

How to install

  • Need python2 or python3
  • Install from source
$ git clone https://github.com/eternnoir/pyTelegramBotAPI.git
$ cd pyTelegramBotAPI
$ python setup.py install
  • or install by pip
$ pip install pyTelegramBotAPI

Example

  • Send Message
import telebot

TOKEN = '<token string>'

tb = telebot.TeleBot(TOKEN)
# tb.send_message(chatid,message)
print tb.send_message(281281, 'gogo power ranger')
  • Echo Bot
import telebot

TOKEN = '<token_string>'


def listener(*messages):
    """
    When new message get will call this function.
    :param messages:
    :return:
    """
    for m in messages:
        chatid = m.chat.id
        if m.content_type == 'text'
            text = m.text
            tb.send_message(chatid, text)


tb = telebot.TeleBot(TOKEN)
tb.get_update()  # cache exist message
tb.set_update_listener(listener) #register listener
tb.polling(3)
while True:
    time.sleep(20)

TeleBot API usage

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)

# sendLocation
tb.send_location(chat_id, lat, lon)

# 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)

# ReplyKeyboardMarkup.
# Use ReplyKeyboardMarkup class.
# Thanks pevdh.

from telebot import types

markup = types.ReplyKeyboardMarkup()
markup.add('a', 'v', 'd')
tb.send_message(chat_id, message, None, None, markup)
# or use row method
markup = types.ReplyKeyboardMarkup()
markup.row('a', 'v')
markup.row('c', 'd', 'e')
tb.send_message(chat_id, message, None, None, markup)

Message notifier

  • Define listener function
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

TODO

  • getMe
  • sendMessage
  • forwardMessage
  • sendPhoto
  • sendAudio
  • sendDocument
  • sendSticker
  • sendVideo
  • sendLocation
  • sendChatAction
  • getUserProfilePhotos
  • getUpdat(contact and chat message not yet)