Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/szastupov/aiotg
Asynchronous Python library for building Telegram bots
https://github.com/szastupov/aiotg
asyncio bot python telegram
Last synced: 11 days ago
JSON representation
Asynchronous Python library for building Telegram bots
- Host: GitHub
- URL: https://github.com/szastupov/aiotg
- Owner: szastupov
- License: mit
- Created: 2015-06-30T17:01:15.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-03-12T20:45:10.000Z (over 1 year ago)
- Last Synced: 2023-09-28T16:38:21.717Z (about 1 year ago)
- Topics: asyncio, bot, python, telegram
- Language: Python
- Homepage:
- Size: 498 KB
- Stars: 381
- Watchers: 24
- Forks: 42
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
- License: COPYING
Awesome Lists containing this project
README
aiotg
=====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 timeoutsInstall it with pip:
.. code:: sh
pip install aiotg
Then you can create a new bot in few lines:
.. code:: python
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.
.. note:: Type annotations are not required but will help your editor/IDE to provide code completion.
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:.. code:: python
import aiohttp
from aiotg import Bot, Chatbot = 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:.. code:: python
...
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
---------------- `Async IO `__
- `Send image `__
- `Post to channel `__
- `Webhooks mode `__
- `Sender id `__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 `__.