1

I would like to use my Raspberrry Pi (Raspbian Stretch) for running a Telegram bot.

I already wrote the script and it works perfectly, when run in the 'Thonny Python IDE', because there it doesn't terminate after running it.

When I edit my /etc/rc.local via

sudo nano /etc/rc.local

to say

sudo python3 /home/pi/Desktop/bot.py &
exit 0

I'm not quite sure, if the script even executes correctly, because I do not get any response in Telegram.

My script looks like this:

import telepot
from telepot.loop import MessageLoop
import sys

# Logic
def handle(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)

    if str(chat_id) not in open('/home/pi/Desktop/blacklist.txt').read():
        command = msg['text']
        if str(chat_id) in open('/home/pi/Desktop/whitelist.txt').read():            
            print("user: '%d'" % chat_id + ", command: '%s'" % command)

            if command == 'test':
                bot.sendMessage(chat_id, 'Test successful.')
            elif '/check' in command:
                check_id = command.split()
                if str(check_id[1]) in open('/home/pi/Desktop/blacklist.txt').read():
                    bot.sendMessage(chat_id, "Unauthorized user detected.")
                else:
                    bot.sendMessage(chat_id, "No database entry for user '%s'." % check_id[1])
            elif command == '/start':
                bot.sendMessage(chat_id, 'Welcome back, <master_name>.')

        else:
            bot.sendMessage(<master_id>, "Unauthorized access by user '%d'." % chat_id)
            print("user: '%d'" % chat_id + ", command: '%s'" % command)

            file = open('/home/pi/Desktop/blacklist.txt', 'a')
            print(chat_id, file=file)
            file.close()


# Setup
bot = telepot.Bot('<bot_token>')
MessageLoop(bot, handle).run_as_thread()

As you can see, I'm using the Telepot API and MessageLoop, which at least in Thonny, correctly loops my script. Also, this is just the skeleton for Telegram bot as of now.

I searched for solutions via Google, StackOverflow, and the Raspberry Pi StackExchange, but couldn't find a solution to my problem.

3

Using sudo in /etc/rc.local is not needed. You should avoid it. But in general you should not use rc.local anymore. rc.local is only emulated by systemd and known to have problems. Use a systemd unit to manage your script. I don't know what environment and edge conditions it needs and if it has any output. For my example I assume it needs network. Here is a simple example that can be improved to fit your needs. Create a service with:

rpi ~$ sudo systemctl edit --force --full telegram_bot.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Descrition=Telegram bot
Wants=network.target
After=network.target

[Service]
ExecStart=/usr/bin/python3 /home/pi/Desktop/bot.py

[Install]
WantedBy=multi-user.target

Enable the new service:

rpi ~$ sudo systemctl enable telegram_bot.service

Reboot and look what's going with:

rpi ~$ systemctl status telegram_bot.service
rpi ~$ journalctl -b -e
  • This does not work for me unless I switch the flag --full. This does work sudo systemctl edit --force --full telegram_bot.service – skd Oct 11 at 12:05
  • 1
    @skd Yes, you are right, my mistake. I have corrected it. Thanks for the feedback. – Ingo Oct 11 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy