Bot now posts to correct chat url

This commit is contained in:
Siina Mashek 2022-04-19 13:59:19 +03:00
parent ac113944cd
commit 6503f66114
3 changed files with 34 additions and 22 deletions

View File

@ -3,17 +3,23 @@ import json
import random import random
import requests import requests
from requests.structures import CaseInsensitiveDict from requests.structures import CaseInsensitiveDict
from ameliabot.init_logging import init_logging
from ameliabot.logger import logging from ameliabot.logger import logging
from ameliabot.quote import Quote from ameliabot.quote import Quote
from flask import request from flask import request
init_logging()
bot_version = "0.0.1" bot_version = "0.0.1"
# Get basic owncast bot config # Get basic owncast bot config
with open("config/owncast.json", "r") as f: try:
config = json.load(f) with open("config/owncast.json", "r") as f:
logging.info("Configuration loaded") config = json.load(f)
logging.info("Configuration loaded")
except FileNotFoundError:
logging.error("Configuration file not found.")
logging.error("Please see README.md for more information.")
raise SystemExit
with open("commands.json", "r") as f: with open("commands.json", "r") as f:
commands = json.load(f) commands = json.load(f)
@ -43,18 +49,20 @@ def parse_webhook():
data = process_chat(data) data = process_chat(data)
if data: if data:
logging.debug("Processed: {}".format(data)) send_message(data)
data = '{"body": "%s"}' % data
resp = requests.post(
config["owncast_server"],
headers=headers,
data=data.encode('utf-8'))
if resp.status_code == 200: def send_message(data):
logging.debug("RESP: {}".format(data)) data = '{"body": "%s"}' % data
else: resp = requests.post(
logging.error("Status code {} returned".format( config["owncast_server"],
str(resp.status_code))) headers=headers,
data=data.encode('utf-8'))
if resp.status_code == 200:
logging.debug("RESP: {}".format(resp.__dict__))
else:
logging.error("Status code {} returned".format(
str(resp.status_code)))
def get_command(text): def get_command(text):
@ -78,13 +86,11 @@ def get_quote(num):
def process_chat(data): def process_chat(data):
timestamp = "{} {}".format(
data["timestamp"][0:10], data["timestamp"][11:19])
sender = data["user"]["displayName"] sender = data["user"]["displayName"]
text = data["body"] text = data["body"]
command_reply = get_command(text) command_reply = get_command(text)
logging.info("{} <{}> {}".format(timestamp, sender, text)) logging.info("<{}> {}".format(sender, text))
if command_reply: if command_reply:
try: try:

View File

@ -1,11 +1,19 @@
from datetime import datetime from datetime import datetime
import random import random
import psycopg2 from ameliabot.logger import logging
try:
import psycopg2
except ModuleNotFoundError:
logging.error("Please install psycopg2.")
logging.error("Please see README.md for more information.")
raise SystemExit
class Quote: class Quote:
def __init__(self, database, user, password): def __init__(self, database, user, password):
self.conn = psycopg2.connect(database, user, password) self.conn = psycopg2.connect(database=database, user=user, password=password)
self.num_quotes = self._get_num_quotes() self.num_quotes = self._get_num_quotes()
def insert(self, owner, submitter, text): def insert(self, owner, submitter, text):

2
bot.py
View File

@ -1,11 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
from flask import Flask, Response from flask import Flask, Response
from ameliabot import owncast from ameliabot import owncast
from ameliabot.init_logging import init_logging
from ameliabot.logger import logging from ameliabot.logger import logging
init_logging()
logging.info("Loaded ameliabot v%s" % owncast.bot_version) logging.info("Loaded ameliabot v%s" % owncast.bot_version)
# the url of the Owncast API for bot posts # the url of the Owncast API for bot posts