-
Notifications
You must be signed in to change notification settings - Fork 2
Telegram Bot
This is a telegram bot which uses the processor
service. You can send an image to the bot (from your gallery or camera) and the bot will reply with the object_string
:
This uses the library python-telegram-bot (supports v12
) You should check their Wiki for more info, and probably familiarise yourself with the rate limits.
To make this work you'll need the Telegram IM app working, which requires a smartphone. You can then also install the Desktop Client for different operating systems and do the following steps through that:
- Send a
/start
command to@BotFather
- Send a
/newbot
command - Give the bot a name, and a username.
- You should get a confirmation that the bot has been created.
- Note the API key. Keep this very secret.
When the python application connects to telegram, by default anyone on telegram can message the bot. My script implements support to lock this down to one or several users. This is done through the @restricted
decorator.
Every user on telegram has a unique user id. When an "update" reaches one of the callback functions in the code, this ID is available in the variable update.effective_user.id
. For single person chats (that's when you message the bot directly, as oppose to in a group chat) update.chat_id
is equal to update.effective_user.id
.
My script also contains an echo function which isn't restricted, allowing you to find out your user id by messaging the bot.
-
Create a file called
.telegram-env
and place the API key in here as the following environment variable:TELEGRAM_API_KEY=1235:AAAfffsecretshit
Be careful not to put spaces on either side of the
=
symbol. -
Launch just this container with:
docker-compose -f tg-docker-compose.yml up --build
You should see something like:
telegram_1 | Admin list: [] telegram_1 | Serving echo handler telegram_1 | About to poll...
-
Find your bot by searching in the telegram app for
@username_bot
which you set earlier, and issue the/echo
command. The number which comes back is your effective user_id. -
Take this ID and append a new environment variable
LIST_OF_ADMINS
in the.telegram-env
file you created earlier. This should now look something like:TELEGRAM_API_KEY=1235:AAAfffsecretshit` LIST_OF_ADMINS=1987654
LIST_OF_ADMINS=1987654,145723
is also a valid format for multiple users (probably not required for now) -
You can re-test the container by launching with the same docker-compose command as before.
docker-compose -f tg-docker-compose.yml up --build
This time you should see something like:
telegram_1 | Admin list: [1987654] telegram_1 | Serving photo handler telegram_1 | About to poll...
-
This is no good on its own, as the telegram container will use the
proccesing
service's API to do image processing. So kill that compose command, and now launch the service fully with:docker-compose -f docker-compose.yml -f tg-docker-compose.yml up
Finally, back on the telegram app you should be able to share a photo with the bot, and then get object_string
relayed back through the chat. This is quite convenient, as you can actually take a photo within the app: