BotsTelegram_Python/01-blasterbot/README.md

116 lines
3.1 KiB
Markdown
Raw Normal View History

2021-01-04 20:11:22 -03:00
## Bot Telegram
Funcionalidad:
- */huhuu* Responde con wav
- */allyb* Responde con png
- */caps* Responde con texto capitalizado, lo pasado como argumento
- */desconocido* Responde con frase aleateoria, a cualquier comando desconocido
- */echo* Devuelve el texto recibido
- */timer_minuto* Activa el trabajo alarma, para ser ejecutado en 1 minuto.
- */alarma* Envia msg a modo de alarma
- **func. ip_alert**: Tarea cada 30 minutos, avisa con MSG si la ip del servidor a cambiado
- Otros trabajos de ejemplo
Libreria [python-telegram-bot](https://pypi.org/project/python-telegram-bot/)
```
pip3 install python-telegram-bot
pip3 install fetchip
```
## Clases Principales, Updater, Dispatcher, Handler
*Extracto de [python-telegram-bot](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions-%E2%80%93-Your-first-Bot)*
```
telegram.ext.Updater
telegram.ext.Dispatcher
```
La clase **Updater** continuamente consulta y obtiene nuevas actualizaciones
desde telegram. Las pasa a la clase **Dispatcher**
Al crear un objeto Updater, este crea un objeto Dispatcher.
Estos son vinculados entre si en una **Queue**
Ahora se pueden Registrar **handlers** de diferentes tipos en el Dispatcher,
este último ordenará las actualizaciones obtenidas por Updater,
según los **handlers** registrados.
Estos handlers son entregados a las ***funciones callback*** definidas.
```
telegram.ext.Handler
```
Esta libreria ofrece ***manejadores*** para casi todos los casos de uso comúnes.
En caso de necesitar algo especifico, se puede crear una subclase Handler.
## Crear Bot en Telegram
*[telegram bot](https://core.telegram.org/bots)*
```
@BotFather
/newbot
/setname
/setdescription
/setabouttext
/setuserpic
```
## Estructura del directorio
```
📂️ .
├── 📂️ conf
│ ├── 📄️ config.cfg
│ └── 📄️ last_ipp
├── 📂️ media
│ ├── 📄️ allyb.png
│ └── 📄️ huhuu.wav
├── 📄️ blasterbot.py
└── 📄️ README.md
2021-01-04 20:11:22 -03:00
```
## Token desde config.cfg
Archivo de configuración ***/conf/config.cfg***
```
[creds]
token = <TOKEN>
user = <ID>
```
## User bajo el que correrá el servicio
User sin home ni login
```
sudo useradd --system --no-create-home --shell=/sbin/nologin my-user-service
```
### Conf de permisos comúnmente utilizados
```
sudo chown -R root:my-user-service /path/to/change
sudo chmod -R 775 /path/to/change
```
### Agregar usuario administrador al grupo del usuario bajo el que corre el servicio
```
usermod -a -G service-group-user admin-user
```
## Servicio Linux
**/etc/systemd/system/bot_telegram.service**
```
[Unit]
Description = Python Telegram Bot Service
Wants = network.target
After = network.target
[Service]
User = my-user-service
Grout = group-my-user-service
WorkingDirectory = /path/to/botelegram
ExecStart = /pat/to/.vitEnv/bin/python3 /path/to/botelegram/bot.py
Restart = always
[Install]
WantedBy = multi-user.target
```
### Probar y acticar servicio
```
sudo systemctl start bot_telegram
Si todo va bien 👌️ :
sudo systemctl enable bot_telegram
```