First commit for bot. v1.0.
This commit is contained in:
commit
b63659af34
3 changed files with 159 additions and 0 deletions
32
README.md
Normal file
32
README.md
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
bot buenosdias
|
||||||
|
==============
|
||||||
|
|
||||||
|
Este es un bot para Mastodon que saluda en nombre de un usuario
|
||||||
|
uniendo sujetos y predicados o tomando frases y uniéndolos
|
||||||
|
en un mensaje final.
|
||||||
|
|
||||||
|
La prioridad es frases sobre sujetos y predicados.
|
||||||
|
|
||||||
|
## Configuración
|
||||||
|
|
||||||
|
Este bot requiere de un archivo de configuración en formato toml.
|
||||||
|
Está pensado para ser simple de usar.
|
||||||
|
Mira el archivo de configuración de ejemplo.
|
||||||
|
|
||||||
|
### Tablas
|
||||||
|
|
||||||
|
Las tablas representan los distintos nombre de días de la semana.
|
||||||
|
Monday, Tuesday, Wednesday...
|
||||||
|
En las tablas se ponen los arrays *phrases* o *subjects* y *predicates*.
|
||||||
|
Si quieres poner arrays por defecto, si no existe un día,
|
||||||
|
existe la tabla especial *default*.
|
||||||
|
|
||||||
|
## Uso
|
||||||
|
|
||||||
|
Para usarlo es mejor llamar al script con un archivo de configuración como
|
||||||
|
parámetro en un planificador como cron.
|
||||||
|
En teoría cualquier planificador valdría.
|
||||||
|
|
||||||
|
## Licencia
|
||||||
|
|
||||||
|
[AGPL-3.0](https://www.gnu.org/licenses/agpl-3.0.md)
|
||||||
109
bot-buenosdias.py
Normal file
109
bot-buenosdias.py
Normal file
|
|
@ -0,0 +1,109 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
# Oscar Triano García
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify it under
|
||||||
|
# the terms of the GNU General Public License as published
|
||||||
|
# by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import datetime
|
||||||
|
import random
|
||||||
|
import requests
|
||||||
|
import tomllib
|
||||||
|
|
||||||
|
VERSION = "1.0"
|
||||||
|
|
||||||
|
mensaje = "BIP BOP"
|
||||||
|
|
||||||
|
hoy = datetime.datetime.now()
|
||||||
|
dia = hoy.isoweekday()
|
||||||
|
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print("Ningun argumento para configuración.")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
config_toml = sys.argv.pop()
|
||||||
|
archivo_config = open(config_toml,"rb")
|
||||||
|
config = tomllib.load(archivo_config)
|
||||||
|
|
||||||
|
token = ""
|
||||||
|
URL = ""
|
||||||
|
try:
|
||||||
|
token = config["token"]
|
||||||
|
except:
|
||||||
|
print ("'token' no found in config.");
|
||||||
|
sys.exit(1)
|
||||||
|
try:
|
||||||
|
URL = config["URL"]
|
||||||
|
except:
|
||||||
|
print ("'URL' no found in config.")
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
|
URL_status = URL + "/api/v1/statuses"
|
||||||
|
|
||||||
|
dayweek_name = {
|
||||||
|
1: "Monday",
|
||||||
|
2: "Tuesday",
|
||||||
|
3: "Wednesday",
|
||||||
|
4: "Thursday",
|
||||||
|
5: "Friday",
|
||||||
|
6: "Saturday",
|
||||||
|
7: "Sunday",
|
||||||
|
}.get(dia,"default")
|
||||||
|
|
||||||
|
def send_message(status, token):
|
||||||
|
data = {
|
||||||
|
"status": status,
|
||||||
|
"media_ids[]": [],
|
||||||
|
"poll[options]": [],
|
||||||
|
"poll[expires_in]": []
|
||||||
|
}
|
||||||
|
headers = {"Authorization": f"Bearer {token}"}
|
||||||
|
return requests.post(URL_status,headers=headers,data=data)
|
||||||
|
|
||||||
|
def build_random_message(config, dayweek_name):
|
||||||
|
"""
|
||||||
|
Priority: phrases > subjects + predicates
|
||||||
|
"""
|
||||||
|
message = config.get("base", "")
|
||||||
|
|
||||||
|
dia_config = None
|
||||||
|
|
||||||
|
if dayweek_name in config:
|
||||||
|
dia_config = config[dayweek_name]
|
||||||
|
elif "default" in config:
|
||||||
|
dia_config = config["default"]
|
||||||
|
|
||||||
|
if "phrases" in dia_config and len(dia_config["phrases"]) > 0:
|
||||||
|
phrase = dia_config["phrases"][random.randint(0, len(dia_config["phrases"])-1)]
|
||||||
|
message += f"{phrase}."
|
||||||
|
else: # Continue here
|
||||||
|
subjects = dia_config.get("subjects", [])
|
||||||
|
predicates = dia_config.get("predicates", [])
|
||||||
|
subject = ""
|
||||||
|
predicate = ""
|
||||||
|
if subjects:
|
||||||
|
subject = subjects[random.randint(0, len(subjects)-1)]
|
||||||
|
if predicates:
|
||||||
|
predicate = predicates[random.randint(0, len(predicates)-1)]
|
||||||
|
|
||||||
|
message += f"{subject} {predicate}."
|
||||||
|
|
||||||
|
return message
|
||||||
|
|
||||||
|
message = build_random_message (config, dayweek_name)
|
||||||
|
|
||||||
|
print(message)
|
||||||
|
|
||||||
|
res = send_message(message, token)
|
||||||
|
print(res)
|
||||||
18
config-bot-buenosdias.toml
Normal file
18
config-bot-buenosdias.toml
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
|
||||||
|
base = "Good morning, " #Change the base here
|
||||||
|
URL = "https://mastodon.xyz" # Change here by your instance.
|
||||||
|
token = "<replace this by your Mastodon API token.>"
|
||||||
|
|
||||||
|
[default]
|
||||||
|
subjects = ["replace","this",""]
|
||||||
|
predicates = ["please", "your code here."]
|
||||||
|
|
||||||
|
[Saturday]
|
||||||
|
phrases = ["little people of Earth"]
|
||||||
|
|
||||||
|
#[Friday]
|
||||||
|
#phrases = ["for Friday", "13"]
|
||||||
|
|
||||||
|
[Monday]
|
||||||
|
subjects = ["world",""]
|
||||||
|
predicates = ["Monday", "longing for coffe."]
|
||||||
Loading…
Add table
Add a link
Reference in a new issue