aio-telegram-bot
An asynchronous framework for building your own Telegram Bot over API.
Installation
aio-telegram-bot requires Python 3.5.3+ and is available on PyPI:
$ pip install aio-telegram-bot
*Compatible with PyPy3.5-6.0.0+
Examples
Polling example
import asyncio
import os
from aiotelegrambot import Bot, Client, Content, Message
from aiotelegrambot.rules import Contains
async def hi(message: Message):
await message.send_message("Hello!", True)
async def run(bot: Bot):
await bot.initialize()
while True:
await asyncio.sleep(1)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
client = Client(os.environ["TELEGRAM_BOT_TOKEN"])
bot = Bot(client)
bot.add_handler(hi, content_type=Content.TEXT, rule=Contains("hi"))
try:
loop.run_until_complete(run(bot))
except KeyboardInterrupt:
loop.run_until_complete(bot.close())
loop.run_until_complete(bot.client.close())
finally:
loop.close()
Running:
$ export TELEGRAM_BOT_TOKEN=12345678:replace-me-with-real-token
$ python3 polling.py
Webhook example
Example of how to generate ssl certificate:
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout domain_srv.key -out domain_srv.crt
import argparse
import json
import os
import ssl
from aiohttp import web
from async_generator import async_generator, yield_
from aiotelegrambot import Bot, Client, Content, Handlers, Message
from aiotelegrambot.rules import Contains
handlers = Handlers()
TOKEN = os.environ["TELEGRAM_BOT_TOKEN"]
HOST = os.environ["TELEGRAM_BOT_HOST"]
PORT = 8443
parser = argparse.ArgumentParser()
parser.add_argument("files", metavar="N", type=str, nargs="+")
SSL_PUBLIC_KEY, SSL_PRIVATE_KEY = parser.parse_args().files
@handlers.add(content_type=Content.TEXT, rule=Contains("hi"))
async def hi(message: Message):
await message.send_message("Hello!")
async def webhook_handle(request):
bot = request.app["bot"]
data = await request.text()
await bot.process_update(json.loads(data))
return web.Response()
@async_generator
async def init_bot(app: web.Application):
bot = Bot(Client(TOKEN), handlers)
await bot.initialize(webhook=True)
await bot.client.set_webhook("https://{}:{}/{}".format(HOST, PORT, TOKEN), certificate=SSL_PUBLIC_KEY)
app["bot"] = bot
await yield_()
await bot.client.delete_webhook()
await bot.close()
await bot.client.close()
app = web.Application()
app.router.add_post("/{}".format(TOKEN), webhook_handle)
app.cleanup_ctx.extend([init_bot])
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain(SSL_PUBLIC_KEY, SSL_PRIVATE_KEY)
web.run_app(app, host="0.0.0.0", port=PORT, ssl_context=context)
Running:
$ export TELEGRAM_BOT_TOKEN=12345678:replace-me-with-real-token
$ export TELEGRAM_BOT_HOST=real.host.com
$ python3 webhook.py domain_srv.crt domain_srv.key
License
aio-telegram-bot is offered under the MIT license.
Metadata
Release files for aio-telegram-bot 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| aio-telegram-bot-1.0.0.tar.gz | 10.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aio_telegram_bot-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.9 kB
Release files / aio-telegram-bot-1.0.0.tar.gz
| Download URL | aio-telegram-bot-1.0.0.tar.gz |
|---|---|
| Size | 10.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
472cbc7f2caf2cb695341e94eb3ee5af216a8ae52f92018558c41cf64567ad44
|
|
BLAKE2b-256 checksum How to use checksums |
82194f0413345c384e935eab18a5efac575b9c5978109d2a2a0ce5e5b3daea2b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 PyPy/7.0.0
|
Release files / aio_telegram_bot-1.0.0-py3-none-any.whl
| Download URL | aio_telegram_bot-1.0.0-py3-none-any.whl |
|---|---|
| Size | 11.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
56aa78be8cc3dfa574bdf86a5eed8dd486f001c4f6620549fe34432798fe50bb
|
|
BLAKE2b-256 checksum How to use checksums |
4fdc8a6b19eef74b089b9c839a07691aa5d4d2b4b261cb93071f81cb226d9ba7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 PyPy/7.0.0
|
Security for aio-telegram-bot 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.