Skip to main content

Telegram Bot API with Tornado

Project description

Telegram Bot API with Tornado.

Implemented Functions

  • Getting Updates
    • [x] Update

    • [x] getUpdates

    • [x] setWebhook

    • [x] deleteWebhook

    • [x] getWebhookInfo

    • [x] WebhookInfo

  • Available Types
    • [x] User

    • [x] Chat

    • [x] Message

    • [x] MessageEntity

    • [x] PhotoSize

    • [x] Audio

    • [x] Document

    • [x] Video

    • [x] Voice

    • [x] VideoNote

    • [x] Contact

    • [x] Location

    • [x] Venue

    • [x] UserProfilePhotos

    • [x] ReplyKeyboardMarkup

    • [x] KeyboardButton

    • [x] ReplyKeyboardRemove

    • [x] InlineKeyboardMarkup

    • [x] InlineKeyboardButton

    • [x] CallbackQuery

    • [x] ForceReply

    • [ ] ChatPhoto

    • [x] ChatMember

    • [x] ResponseParameters

    • [x] InputFile

  • Available Methods
    • [x] getMe

    • [x] sendMessage

    • [x] forwardMessage

    • [x] sendPhoto

    • [x] sendAudio

    • [x] sendDocument

    • [x] sendVideo

    • [x] sendVoice

    • [x] sendVoiceNote

    • [x] sendLocation

    • [x] sendVenue

    • [x] sendContact

    • [x] sendChatAction

    • [x] getUserProfilePhotos

    • [x] getFile

    • [x] kickChatMember

    • [x] unbanChatMember

    • [ ] restrictChatMember

    • [ ] promoteChatMember

    • [ ] exportChatInviteLink

    • [ ] setChatPhoto

    • [ ] deleteChatPhoto

    • [ ] setChatTitle

    • [ ] setChatDescription

    • [ ] pinChatMessage

    • [ ] unpinChatMessage

    • [x] leaveChat

    • [x] getChat

    • [x] getChatAdministrators

    • [x] getChatMembersCount

    • [x] getChatMember

    • [x] answerCallbackQuery

  • Updating Messages
    • [x] editMessageText

    • [x] editMessageCaption

    • [x] editMessageReplyMarkup

    • [x] deleteMessage

  • Stickers
    • [x] Sticker

    • [x] StickerSet

    • [x] MaskPosition

    • [x] sendSticker

    • [x] getStickerSet

    • [x] uploadStickerSet

    • [x] createNewStickerSet

    • [x] addStickerToSet

    • [x] setStickerPositionInSet

    • [x] deleteStickerFromSet

  • Inline Mode
    • [x] InlineQuery

    • [x] answerInlineQuery

    • [x] InlineQueryResult

    • [x] InlineQueryResultArticle

    • [x] InlineQueryResultPhoto

    • [x] InlineQueryResultGif

    • [x] InlineQueryResultMpeg4Gif

    • [x] InlineQueryResultVideo

    • [x] InlineQueryResultAudio

    • [x] InlineQueryResultVoice

    • [x] InlineQueryResultDocument

    • [x] InlineQueryResultLocation

    • [x] InlineQueryResultVenue

    • [x] InlineQueryResultContact

    • [x] InlineQueryResultGame

    • [x] InlineQueryResultCachedPhoto

    • [x] InlineQueryResultCachedGif

    • [x] InlineQueryResultCachedMpeg4Gif

    • [x] InlineQueryResultCachedSticker

    • [x] InlineQueryResultCachedDocument

    • [x] InlineQueryResultCachedVideo

    • [x] InlineQueryResultCachedVoice

    • [x] InlineQueryResultCachedAudio

    • [x] InputMessageContent

    • [x] InputTextMessageContent

    • [x] InputLocationMessageContent

    • [x] InputVenueMessageContent

    • [x] InputContactMessageContent

    • [x] ChosenInlineResult

  • Payments
    • [ ] sendInvoice

    • [ ] answerShippingQuery

    • [ ] answerPreCheckoutQuery

    • [ ] LabeledPrice

    • [ ] Invoice

    • [ ] ShippingAddress

    • [ ] OrderInfo

    • [ ] ShippingOption

    • [ ] SuccessfulPayment

    • [ ] ShippingQuery

    • [ ] PreCheckoutQuery

  • Games
    • [x] sendGame

    • [x] Game

    • [x] Animation

    • [x] CallbackGame

    • [x] setGameScore

    • [x] getGameHighScores

    • [x] GameHighScore

High Level API Example

BotAgent is a high level undead, you can demand it to send requests:

from tornado import gen, ioloop
from wcpan.telegram import api, types


async def main():
    API_TOKEN = 'your_token'
    lich = api.BotAgent(API_TOKEN)
    talk_to = 42

    # getMe
    user = await lich.get_me()
    print(user)

    # getUpdates
    updates = await lich.get_updates()
    print(updates)

    # sendMessage
    message = await lich.send_message(talk_to, 'hello')
    print(message)

    # sendPhoto
    photo = types.InputFile('path_to_your_phoho.png')
    message = await lich.send_photo(talk_to, photo)
    print(message)

    # sendAudio
    audio = types.InputFile('path_to_your_audio.ogg')
    message = await lich.send_audio(talk_to, audio)
    print(message)

    # sendVideo
    video = types.InputFile('path_to_your_video.mp4')
    message = await lich.send_video(talk_to, video)
    print(message)

    # sendDocument
    document = types.InputFile('path_to_your_file.zip')
    message = await lich.send_document(talk_to, document)
    print(message)


main_loop = ioloop.IOLoop.instance()
main_loop.run_sync(main)

And let it handles updates:

from tornado import gen, ioloop
from wcpan.telegram import api


class KelThuzad(api.BotAgent):

    def __init__(self, api_token):
        super(KelThuzad, self).__init__(api_token)

    async def on_text(self, message):
        id_ = message.message_id
        chat = message.chat
        text = message.text
        # echo same text
        await self.send_message(chat.id_, text, reply_to_message_id=id_)

    async def on_video(self, message):
        chat = message.chat
        video = message.video
        # echo video without upload again
        await self.send_video(chat.id_, video.file_id, reply_to_message_id=id_)


async def forever():
    API_TOKEN = 'your_token'
    lich = api.KelThuzad(API_TOKEN)
    await lich.poll()


main_loop = ioloop.IOLoop.instance()
main_loop.run_sync(forever)

Or handles updates by webhook:

from tornado import gen, ioloop, web
from wcpan.telegram import api


class HookHandler(api.BotHookHandler):

    async def on_text(self, message):
        lich = self.application.settings['lich']
        id_ = message.message_id
        chat = message.chat
        text = message.text
        # echo same text
        await lich.send_message(chat.id_, text, reply_to_message_id=id_)


async def create_lich():
    API_TOKEN = 'your_token'
    lich = api.BotAgent(API_TOKEN)
    await lich.listen('https://your.host/hook')
    return lich


main_loop = ioloop.IOLoop.instance()

lich = main_loop.run_sync(create_lich)
application = web.Application([
    (r"/hook", HookHandler),
], lich=lich)
# please configure your own SSL proxy
application.listen(8000)

main_loop.start()

Low Level API Example

BotClient is also there, which provides simple and direct API mapping:

from tornado import gen, ioloop
from wcpan.telegram import api, types


async def main():
    API_TOKEN = 'your_token'
    ghoul = api.BotClient(API_TOKEN)
    talk_to = 42

    # getMe
    user = await ghoul.get_me()
    print(user)

    # getUpdates
    offset = 0
    updates = []
    while True:
        us = await ghoul.get_updates(offset)
        updates.extend(us)
        if not us:
            break
        offset = us[-1].update_id + 1
    print(updates)

    # sendMessage
    message = await ghoul.send_message(talk_to, 'hello')
    print(message)

    # sendDocument
    document = types.InputFile('path_to_your_file.zip')
    message = await lich.send_document(talk_to, document)
    print(message)


main_loop = ioloop.IOLoop.instance()
main_loop.run_sync(main)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wcpan.telegram-0.3.0.dev7.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wcpan.telegram-0.3.0.dev7-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file wcpan.telegram-0.3.0.dev7.tar.gz.

File metadata

  • Download URL: wcpan.telegram-0.3.0.dev7.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.0

File hashes

Hashes for wcpan.telegram-0.3.0.dev7.tar.gz
Algorithm Hash digest
SHA256 c9251f4bf0e98ec8aa25cb9676c52ce57e001a8d44b8d21544f0aa6754980388
MD5 51e7b42cbc8d1b2c4fa2512df58f6d3a
BLAKE2b-256 1d01bb2c87eb47f3872dc8266cd0b78e447e6984e5d4e9410d53c38717cdf7b9

See more details on using hashes here.

File details

Details for the file wcpan.telegram-0.3.0.dev7-py3-none-any.whl.

File metadata

File hashes

Hashes for wcpan.telegram-0.3.0.dev7-py3-none-any.whl
Algorithm Hash digest
SHA256 785a0979534105b65391259df3d919820a4ecbd90ee1e77722de38693efcbfff
MD5 fc7d2dbd9002e98ad61b8e8670fed350
BLAKE2b-256 36765c896580db15dc217604fea10eae87442bf4ae06a3de0f98a7416e045bee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page