pytg
pytg copied to clipboard
Wait until logged in - Docker
I am trying to dockerize a pytg bot. I encountered this problem:
Sample Code:
tg = Telegram(telegram = TG_CLI, pubkey_file = TG_PUBKEY)
tgSender = tg.sender
try:
tgSender.send_msg("user#123",'Bot started')
except:
print('ERROR sending start-msg')
The line tg = Telegram(telegram = TG_CLI, pubkey_file = TG_PUBKEY) will continue after the CLI has started, but before the CLI prompts first time user for phone number, which will result in an error in tgSender.send_msg("user#123",'Bot started') in the following lines.
My question is: how to start the CLI, wait until the user authentication is done, and then proceed to send or receive messages?
The registration with the phone number and the SMS code must be made manually. After that pytg should work.
I don't see any purpose on doing that setup automated.
@luckydonald The purpose of automating the process is just to make deployment on remote servers easier (by using a single command).
I later used the following shell code as a workaround.
function checkauth() {
if [ -f /root/.telegram-cli/auth ];
then
asize=$(wc -c < /root/.telegram-cli/auth)
if (($asize > 0)); then
ok=1
else
ok=0
fi
else
ok=0
fi
}
checkauth
while ((ok == 0)); do
echo "[RUN] Not authenticated. Running telegram-CLI"
echo ""
$TG_CLI -k $TG_PUBKEY
checkauth
done
echo ""
echo "[RUN] Authenticated. Starting pytg..."
pkill -f telegram-cli
python /app/p.py
echo ""
echo "[RUN] Stopping telegram-cli."
pkill -f telegram-cli
That works almost fine. The only problem is I need to press Ctrl-C to exit telegram-cli after the first login.
So if I can find a way to kill telegram-cli after /root/.telegram-cli/auth is created, the process will be more automated. :)
The only use-case I can see here, to do the setup automatic is, if you deploy a large scale of bots. And creating a large amount of bots just screams "spamming" to me.
Oh, wait, are you still letting the user do the input?
Then I kinda misunderstood your purpose.
Maybe some startup-parameter does the trick?
--exec/-e <commands> make commands end exit
Maybe append --exec safe_quit to your commands?
like $TG_CLI -k $TG_PUBKEY --exec safe_quit?
I assume this is resolved?
Actually a docker would be great. I completely read over that. Sorry. That happens when you do github at 2am.
Sorry for being away for a few days. Thank you for your kind help.
In short the need is to have a single entrypoint for both the initial run and later runs. On the first run it does authentication and then starts the bot; on later runs the bot will just work normally.
I don't quite understand what --exec safe_quit does.
--exec does execute a command in the cli and then exits, and the command run is safe_quit which waits for all queries to complete, and then quits. Maybe a normal quit is sufficient, too.
So my idea, though untested, is that this hopefully terminates the cli after a successful login.
have a single entrypoint for both the initial run and later runs.
If you are already logged in, it just should exit, so you can continue doing the rest of your routine.
Also, starting the cli from within python with
from pytg import Telegram
Telegram(cli_path, pubkey_path)
is how I would start it for use with the bot.
Benefits are to have the port settings and all that parameter stuff in one single place, and easily user customizable. As I understand, launching the cli in docker comes with hardcoding the --port= option.
But then, I never got Docker working, as all my tries only resulted in a VM_worker process which ate 100% cpu and did nothing ...
If you get something working, maybe you can add a pull request. As how I understand docker is something quite famous and useful?
@ztl8702:
I've been working on getting my bot running in Docker as well. It's not quite finished, but seems to work well enough for testing with the following configuration: https://gist.github.com/justingood/020e24222fa1653f5cd0
The docker-compose file also lets you run the test code in-place
I ended up keeping the .telegram-cli directory outside the container for a couple of reasons:
- Keeping secrets inside a container is generally a bad idea and Docker themselves recommends against it.
- If the container dies, or is updated with a new version, it can be started and have access to the configuration without having to re-register.
Would a configuration similar to this fit your use-case? If you're already running a tool to spin up the bot on each server, maybe it could also pre-populate the .telegram-cli contents?
@luckydonald: Docker is pretty cool. The example I linked to shows how you can have code on your machine and run it inside the container without needing Python installed locally. You could do the same for Ruby, Go, and PHP without cluttering your machine with development libraries. So in this type of setup, all you need is Docker and a text editor and you're good to go. For an actual deployment, you'd add the code into the container, and it will act similar to a single binary - run it, and it'll do its thing. Move it to another server, and it's ready to go. No extra configuration required.
I was just about to link you here, as I saw that in terribot
@ztl8702 I found your image at Docker Hub.
Care to share the Dockerfile?
_edit_: Probably not what I am looking for.

@justingood I curious about your justingood/tg docker, too
I tried to get something working, which resulted in luckydonald/docker-telegram-cli But I really can't remember if that even did run in the end.
I tried docker, but I couldn't get through it. Maybe someone else gets it working with all the login and stuff.