Skip to main content

Asynchronous Python API for building Telegram bots

Project description

https://travis-ci.org/szastupov/aiotg.svg?branch=master

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 botan.io

  • 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

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

@bot.command(r"/echo (.+)")
def echo(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

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

@bot.command("bitcoin")
async def bitcoin(chat, match):
    url = "https://api.bitcoinaverage.com/ticker/global/USD/"
    async with aiohttp.get(url) as s:
        info = await s.json()
        await chat.send_text(info["24h_avg"])

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. Have a question? Ask it on project’s Telegram chat.

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-0.7.20.tar.gz (8.6 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: aiotg-0.7.20.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for aiotg-0.7.20.tar.gz
Algorithm Hash digest
SHA256 ae37421c41a2d1db30797a56fba39c176890ea36f88148c0bd4e3977ac695192
MD5 a6f33929270288ca96ac3cd25b3f8e05
BLAKE2b-256 5b615cfc9f7f6af9e2dfb151eadd6e66a37ab38be080df85c376f92a3fa0ab8c

See more details on using hashes here.

Provenance

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