First commit for bot. v1.0.
This commit is contained in:
commit
b63659af34
3 changed files with 159 additions and 0 deletions
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue