Skip to main content

Asynchronous Python API for building Telegram bots

Project description

Asynchronous Python API for building Telegram bots, featuring:

  • Easy and declarative API

  • Hassle-free setup - no need for SSL certificates or static IP

  • Built-in support for analytics via chatbase.com

  • Automatic handling of Telegram API throttling or timeouts

Install it with pip:

pip install aiotg

Then you can create a new bot in few lines:

from aiotg import Bot, Chat

bot = Bot(api_token="...")

@bot.command(r"/echo (.+)")
def echo(chat: Chat, match):
    return chat.reply(match.group(1))

bot.run()

Now run it with a proper API_TOKEN and it should reply to /echo commands.

The example above looks like a normal synchronous code but it actually returns a coroutine. If you want to make an external request (and that’s what bots usually do) just use aiohttp and async/await syntax:

import aiohttp
from aiotg import Bot, Chat

bot = Bot(api_token="...")

@bot.command("bitcoin")
async def bitcoin(chat: Chat, match):
    url = "https://apiv2.bitcoinaverage.com/indices/global/ticker/BTCUSD"
    async with aiohttp.ClientSession() as session:
        response = await session.get(url)
        info = await response.json()
        await chat.send_text(str(info["averages"]["day"]))

bot.run()

But what if you just want to write a quick integration and don’t need to provide a conversational interface? We’ve got you covered! You can send messages (or any other media) by constructing a Chat object with user_id or channel name. We even saved you some extra keystrokes by providing handy Channel constructors:

...
channel = bot.channel("@yourchannel")
private = bot.private("1111111")

async def greeter():
    await channel.send_text("Hello from channel!")
    await private.send_text("Why not greet personally?")
...

Examples

For a real world example, take a look at WhatisBot or Music Catalog Bot.

For more information on how to use the project, see the project’s documentation.

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

aiotg-2.0.0.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

aiotg-2.0.0-py2.py3-none-any.whl (13.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file aiotg-2.0.0.tar.gz.

File metadata

  • Download URL: aiotg-2.0.0.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for aiotg-2.0.0.tar.gz
Algorithm Hash digest
SHA256 9932f117d3faaf10ee6b36c580ffa033d4a3311b0d54dcbd261cb93e8bae3b1e
MD5 114c01df8a69d381cda0f91f0fc2b7fc
BLAKE2b-256 cfdeed3aaeae5956eb5e2ff98698d07cce0246009ed6654840e45bd376fc4266

See more details on using hashes here.

File details

Details for the file aiotg-2.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: aiotg-2.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for aiotg-2.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 05f7f7cccff9dc25e86d54efbfda862032c08302be41d2d207675a61802ac11e
MD5 ea181e275d641746922837727f8f4d8e
BLAKE2b-256 b483e183c14ac3aa8a5852fc48b56d7872cc904614040f002e023b68a4c960ea

See more details on using hashes here.

Supported by

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