Skip to content

Latest commit

 

History

History
69 lines (51 loc) · 2.07 KB

README.md

File metadata and controls

69 lines (51 loc) · 2.07 KB

Code style: black status workflow test status workflow build Downloads

Simple Telegram BOT library.

Example Usage

Example with poll method:

# -*-coding:utf8;-*-
from TelegramSDK import telegram as bot


# telegram token is automatic loaded from environment variable TELEGRAM_BOT_TOKEN but you can also set it manually
bot.set_token("your token")

# call this function if you run on old device
# bot.disable_ssl()


def handler(data):
    bot.update(data)
    download_photo = bot.download_file("/tmp", filter=(".jpeg", ".jpg", ".png"))
    if len(download_photo):
        bot.reply_message("Photo downloaded: {}".format(download_photo))
    else:
        try:
            # user not send any file
            bot.reply_message("OK: " + bot.data.message.text)
            """
            or  reply with file bot.reply_file("path/to/file/example.pdf", caption="example file!")
            """
        except BaseException as e:
            # handle user send file but not photo
            bot.reply_message("file not supported!")


bot.poll(handler, worker=5, debug=True)

Example with webhook (flask, bottle etc)

@post("/webhook")
def handler():
    bot.update(request.json)
    bot.reply_message("OK: " + bot.data.message.text)

TelegramSDK has built-in session function based zcache, for example:

if bot.get_session():
    count = bot.get_session() + 1
    bot.set_session(count, ttl=3600)
    bot.reply_message("count: %d" % count)
else:
    bot.set_session(1, ttl=3600)
    bot.reply_message("count: 1")

for more doc please read the source code.

License

MIT