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

View File

@ -1,11 +1,19 @@
from datetime import datetime
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:
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()
def insert(self, owner, submitter, text):

2
bot.py
View File

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