Telegram Pi Bot
About
This is my personal Telegram Bot to run on my RaspberryPi 2 at home. It's supposed to be a proxy and an easy interface for all the home automation paraphernalia that we might end up getting in the future.
Features
- Answers to only pre-configured chat rooms
- Webhooks configured via a JSON config file
- Configures an endpoint to call for a given command
- Works great with IFTTT Maker channel
- Basic multilingual support
-
en
pt-br
-
- Runs on Python 3
Setup
Hardware requirements
- RaspberryPi
- Power cable
- MicroSD card
- Internet connection
Installing things
This is a guide for the complete setup on ArchLinux, if you're just intested in running the script you can jump
Base system setup
- Install archlinux
- Add new user and change default passowrds
- Setup ssh
- Setup sudo
- Setup iptables, easier guide here (make sure to open ports for ssh and supervisor)
systemctl enable iptables
- Configure static ip address
Python setup
- Install Python:
sudo pacman -S python
- Install pip:
sudo pacman -S python-pip
Process management setup
sudo pacman -S gcc
sudo pacman -S python-setuptools
- Get circus for process management
sudo pip install circus
- Start circus on boot
Finally, bot setup
sudo pip install telegram-pi-bot
- Configure a
~/circus.ini
file
[circus]
[watcher:telegram-pi-bot]
cmd = telegram-pi-bot
numprocesses = 1
[env]
TELEGRAM_PI_BOT_CONFIG = /home/username/config.json
- Setup the json config file
Maintenance
-
sudo pacman -Syu
Updates the system
Running the bot
Config file
Telegram Pi Bot is configured through a json
file, in which you can set your bot token and configure webhooks/commands to invoke from your bot.
Here is an example of what a config.json
file looks like:
{
"bot_token": "<insert bot token>",
"chat_ids": [
"<insert chat id in which the bot should respond to>"
],
"language": "<bot language>",
"webhooks": [
{
"command_name": "dosomething",
"url": "https://example.com/dosomething",
"method": "POST",
"headers": {
"Content-Type": "application/json"
},
"data": { "value1": "%s" }
}
]
}
Locate config file
The config file will try to be loaded from:
- A location specified using the
TELEGRAM_PI_BOT_CONFIG
environment variable
or
- A
config.json
file placed in the same folder as themain.py
script
Using Circus
For improved process handling:
systemctl start circus
systemctl stop circus
systemctl reload circus
Autostart on device boot:
systemctl enable circus
Script
Clone this repo, then cd
into its folder.
To just simply run as a Python script, just make sure your config.json
file is accessible as previously explained, then just run:
python telegram_pi_bot
License
MIT © Ruy Adorno