Skip to main content

A Telegram Bot API Python Client

Project description

Telegram Bot API Client

A telegram bot API client is written in python 3.5+ and currently compatible with Telegram Bot API 5.3 and later. The reason for writing this bot utility is that I wish to run multi telegram bots which could have same or different business logic (route policy) in one process . I reckon it is lightweight, fast, full implement and only urllib3 dependent.

Update 5.3.5.3

Fix bugs

Update 5.3.5.2

Fix bugs and add router.remove_handler. see example.dynamic_handler

Update 5.3.5.1

  1. Refacted and faster than before.

  2. Provide a UIHelper in ui for buttons.

Update 5.3.5

A large update. I do not write too many details because no one is using it except myself.

Update 5.3.4

Add: add get_file_url function in bot

Update 5.3.3

Fix bugs and update: support multi emojis for UI buttons

Update 5.3.2

Fix bugs and update: add a UI stack for be back to ahead UI, see example.ui_stack.py

Update 5.3.1

Change: delete multi keys in session using delete function

Update 5.3

Add BotCommandScope Support

Update 5.2.5.1

Fix bugs... correct get_file_bytes in TelegramBotAPI

Update 5.2.5

Optimize: add a context manager on session implement

Update 5.2.4.1

Fix bugs... correct TelegramBotAPIException

Update 5.2.4

Optimize: make define your local API host easy, and your API host can use 'http://'

Update 5.2.3

Optimize: make all bots call same one TelegramBotAPI instance

Update 5.2.2

Add a confirm in ui

Quick to go

This is a simple echo bot.

from telegrambotclient import bot_client
from telegrambotclient.base import MessageField, ParseMode

# define a unnamed router
router = bot_client.router()

# decorate a handler callback on incoming message updates that have a text field
@router.message_handler(fields=MessageField.TEXT)
def on_echo_text(bot, message):
    # receive and reply
    sent_message = bot.send_message(
	        chat_id=message.chat.id,
	        text="I receive: <strong>{0}</strong>".format(message.text),
	        parse_mode=ParseMode.HTML,
	    )
    # pin the sent message
    bot.pin_chat_message(chat_id=message.chat.id, message_id=sent_message.message_id)

# define a bot with the router
bot = bot_client.create_bot(token=<BOT_TOKEN>, router=router)
# delete webhook if did or not
bot.delete_webhook(drop_pending_updates=True)
# run polling to fetch updates in every 10s
bot.run_polling(timeout=10)

Call telegram bot APIs

telegrambotclient has same parameter signatures with official Telegram Bot APIs. Please see official Telegram Bot API document when calling telegram bot APIs.

Quick to reply

For send_message api, it provides a shortcut.

sent_message = bot.reply_message(
        message,
        text="I receive: <strong>{0}</strong>".format(message.text),
        parse_mode=ParseMode.HTML,
    )

Multi bots through webhook

In my case, I use fastapi and uvicron to provide a HTTP interface to receive updates from the official Telegram Bot Server. For development and testing, ngrok give a HTTPs URL on my localhost server with a real-time HTTP traffic tunnel.

# run in terminal and get a https tunnel on port 8000 in Austrlia
ngrok http 8000 --region=au

source code:

from fastapi import FastAPI, Request, status
from telegrambotclient import bot_client
from telegrambotclient.base import MessageField, ParseMode
	# from ngrok's https url, replace it with yours
WEBHOOK_URL = "https://5f9d0f13b9fb.au.ngrok.io/bot/{0}"

	# define a default routers
router = bot_client.router()
# two bots have same router
bot1 = bot_client.create_bot(token=<BOT1_TOKEN>, router=router)
bot2 = bot_client.create_bot(token=<BOT2_TOKEN>, router=router)
bot1.setup_webhook(WEBHOOK_URL.format(<BOT1_TOKEN>))
bot2.setup_webhook(WEBHOOK_URL.format(<BOT2_TOKEN>))

@router.message_handler(fields=MessageField.TEXT)
def on_echo_text(bot, message):
    bot.reply_message(message, text="I receive: <strong>{0}</strong>".format(message.text), parse_mode=ParseMode.HTML)
    return bot.stop_call

app = FastAPI()

# waiting for incoming updates and dispatch them
@app.post("/bot/{bot_token}", status_code=status.HTTP_200_OK)
async def process_telegram_update(bot_token: str, request: Request):
    await bot_client.dispatch(bot_token, await request.json())
    return "OK"

Multi bots and routers play around

from fastapi import FastAPI, Request, status
from telegrambotclient import bot_client
from telegrambotclient.base import Message, MessageField, ParseMode
# from ngrok's https url, replace it with yours
WEBHOOK_URL = "https://5f9d0f13b9fb.au.ngrok.io/bot/{0}"

router1 = bot_client.router("router1")
router2 = bot_client.router("router2")
bot1 = bot_client.create_bot(token=<BOT1_TOKEN>, router=router1)
bot2 = bot_client.create_bot(token=<BOT2_TOKEN>, router=router2)
bot1.setup_webhook(WEBHOOK_URL.format(<BOT1_TOKEN>))
bot2.setup_webhook(WEBHOOK_URL.format(<BOT2_TOKEN>))

# bind a handler on router1
@router1.message_handler(fields=MessageField.TEXT)
def on_router1_echo(bot, message):
    bot.reply_message(
        message,
        text="I receive: <strong>{0}</strong> from router1".format(message.text),
        parse_mode=ParseMode.HTML,
    )

# bind a handler on router2
@router2.message_handler(fields=MessageField.TEXT)
def on_router2_echo(bot, message):
    bot.reply_message(
        message,
        text="I receive: <strong>{0}</strong> from router2".format(message.text),
        parse_mode=ParseMode.HTML,
    )

app = FastAPI()

# waiting for incoming updates and dispatch them
@app.post("/bot/{bot_token}", status_code=status.HTTP_200_OK)
async def process_telegram_update(bot_token: str, request: Request):
    await bot_client.dispatch(bot_token, await request.json())
    return "OK"

Register handlers

decorator

@router.message_handler(fields=MessageField.TEXT)
def on_message(bot, message):
	pass

function

good way to register one callback on multi routers

def on_message(bot, message):
    pass

router1.register_message_handler(callback=on_message, fields=MessageField.TEXT)
router2.register_message_handler(callback=on_message, fields=MessageField.TEXT)

route for multi message fields

@router.message_handler(fields=MessageField.TEXT | MessageField.LOCATION)
def on_any_message_fields(bot, message: Message):
    # call when a message includes 'text' OR 'location' fields
    pass

@router.message_handler(fields=MessageField.ANIMATION & MessageField.DOCUMENT)
def on_animation(bot, message: Message):
    # call when a message includes 'animation' AND 'document' fields
    pass

Please try examples for more detail

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

py-telegram-bot-client-5.3.5.3.tar.gz (24.4 kB view details)

Uploaded Source

Built Distribution

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

py_telegram_bot_client-5.3.5.3-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

Details for the file py-telegram-bot-client-5.3.5.3.tar.gz.

File metadata

  • Download URL: py-telegram-bot-client-5.3.5.3.tar.gz
  • Upload date:
  • Size: 24.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

File hashes

Hashes for py-telegram-bot-client-5.3.5.3.tar.gz
Algorithm Hash digest
SHA256 a3f37e0c890094cf9d97c2d52f7ab75e3d82cabfec7686da92fbcbc03a952f34
MD5 9f242245a43c381adaacd82b3c03ee93
BLAKE2b-256 ace42a5820bcb9b77f2e585411b0b7250ee20f7202dcb63ddb8cc34445f93db9

See more details on using hashes here.

File details

Details for the file py_telegram_bot_client-5.3.5.3-py3-none-any.whl.

File metadata

  • Download URL: py_telegram_bot_client-5.3.5.3-py3-none-any.whl
  • Upload date:
  • Size: 27.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

File hashes

Hashes for py_telegram_bot_client-5.3.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 feb615e4677e5f901b83fbd1f4cbc4111b19c6422846a6c3c7f026f1aa67d363
MD5 e0a47f248723bff16a309775899fe034
BLAKE2b-256 151df04545e2e2a83b211332fe655840f245a8004d71c821ec69157fc9250828

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