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
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) 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) 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.