Examples¶
The scripts in the examples/ folder, smallest bot first. Each starts with a comment that
says how to run it, and they read the token from PGPCHAT_BOT_TOKEN (and the server from
PGPCHAT_API): none has a secret written in it. All of them were run against a real server,
with a person writing to the bot from the app.
File |
What it shows |
|---|---|
|
the smallest bot: one handler, |
|
text, media and messages without text; edited messages |
|
commands with arguments, the command menu published at start-up, a catch-all for unknown commands |
|
a bot that works in private chats only, and says so in groups |
|
being added to a group, mentions, the member list, an admin-only command, deleted messages |
|
a counter with buttons, links, |
|
a poll and a menu that changes in place; regex patterns on button data; late answers |
|
polling by hand with |
|
a webhook inside FastAPI, with |
|
an end-to-end encrypted bot: publishing the key, signatures required |
|
making and saving the bot’s key with a passphrase |
|
one message from a script, encrypted when the chat is; the same by hand |
|
your own filters, a filter that hands values to the handler, |
|
error handlers by type, |
|
a broadcast to many chats in the background, people who blocked the bot, a custom |
|
the bot inside your own asyncio program, next to a periodic task |
|
no asyncio: the blocking |
|
|
|
most options, with a word on each, and secrets from files |
|
a to-do list per chat: routers, buttons, middleware, admin commands, hooks, a clean |
Running them¶
pip install PGPCbot fastapi uvicorn # fastapi and uvicorn only for 09
export PGPCHAT_BOT_TOKEN="pgpbot_..."
python examples/01_hello_world.py
Then write to your bot from the app. Ctrl+C stops it.
A few need more:
10_encrypted_bot.pyneeds a key: run11_generate_pgp_keys.pyfirst. Publishing the key is one-way: try it on a test bot.09_fastapi_webhook.pyneeds a public HTTPS address that reaches the app, and a secret.14,15and20have admin commands: setADMIN_IDto your own user ID.