Skip to content
This repository was archived by the owner on Jun 28, 2024. It is now read-only.

Commit bca5cf8

Browse files
committed
inital commit
0 parents  commit bca5cf8

File tree

8 files changed

+1228
-0
lines changed

8 files changed

+1228
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
run.sh
2+
faqbot.service
3+
session.txt

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Matrix FAQ Bot
2+
3+
## Install
4+
5+
## Usage
6+
7+
- Edit/add faq in `faq.json`

faq.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"nightly": "You can download Gradience nightly builds from <a href='https://github.com/GradienceTeam/Gradience/actions/workflows/build.yml'>here</a>."
3+
}

faqbot/__init__.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import simplematrixbotlib as botlib
2+
import os
3+
import subprocess
4+
import json
5+
import sys
6+
7+
PREFIX = "!"
8+
9+
USERNAME = os.environ.get("MATRIX_USERNAME", "oxmrtt")
10+
SERVER = os.environ.get("MATRIX_SERVER", "https://mtrx.vern.cc")
11+
PASSWORD = os.environ.get("MATRIX_PASSWORD", None)
12+
ALIASES = os.environ.get("ALIASES", "faq, f").split(", ")
13+
FAQ_CONFIG = os.environ.get("FAQ_CONFIG", "faq.json")
14+
SYSTEMD_SERVICE_NAME = os.environ.get("SYSTEMD_SERVICE_NAME", "faqbot")
15+
16+
HELP = f"""
17+
Help Message:
18+
prefix: {PREFIX}
19+
commands:
20+
faq:
21+
command: {", ".join(ALIASES)}
22+
description: display faq for an argument
23+
example: {PREFIX}faq nightly
24+
25+
help:
26+
command: {", ".join(ALIASES)} help, {", ".join(ALIASES)} ?, {", ".join(ALIASES)} h
27+
description: display help command
28+
29+
faq all:
30+
command: {", ".join(ALIASES)} all
31+
description: display all faq
32+
33+
"""
34+
35+
36+
def run():
37+
if not USERNAME or not SERVER or not PASSWORD:
38+
print(
39+
"Please set the environment variables MATRIX_USERNAME(optional), MATRIX_SERVER(optional), and MATRIX_PASSWORD(required)"
40+
)
41+
return
42+
43+
creds = botlib.Creds(SERVER, USERNAME, PASSWORD)
44+
bot = botlib.Bot(creds)
45+
46+
FAQ = {}
47+
48+
@bot.listener.on_message_event
49+
async def faq(room, message):
50+
match = botlib.MessageMatch(room, message, bot, PREFIX)
51+
52+
if match.is_not_from_this_bot() and match.prefix() and ALIASES:
53+
for alias in ALIASES:
54+
if match.command(alias):
55+
break
56+
else:
57+
return
58+
59+
try:
60+
prompt = " ".join(arg for arg in match.args())
61+
62+
global FAQ
63+
if not FAQ:
64+
with open(FAQ_CONFIG) as f:
65+
FAQ = json.load(f)
66+
67+
if prompt == "help" or prompt == "h" or prompt == "?":
68+
await bot.api.send_markdown_message(room.room_id, HELP)
69+
return
70+
71+
elif prompt == "all":
72+
for key, value in FAQ.items():
73+
await bot.api.send_markdown_message(
74+
room.room_id, f"> {key}\n\n{value}"
75+
)
76+
else:
77+
if prompt in FAQ:
78+
response = FAQ[prompt]
79+
else:
80+
response = "I don't know the answer to that question."
81+
82+
await bot.api.send_markdown_message(room.room_id, f"{response}")
83+
except Exception as e:
84+
print(e)
85+
await bot.api.send_markdown_message(room.room_id, f"> {prompt}\n\n{e}")
86+
87+
try:
88+
bot.run()
89+
except Exception as e:
90+
subprocess.run(["systemctl", "restart", "--user", SYSTEMD_SERVICE_NAME])
91+
print("Restarting bot due to {e}")
92+
return 1
93+
94+
95+
if __name__ == "__main__":
96+
sys.exit(run())

poetry.lock

Lines changed: 1098 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[tool.poetry]
2+
name = "faqbot"
3+
version = "0.1.0"
4+
description = ""
5+
authors = ["0xMRTT <[email protected]>"]
6+
readme = "README.md"
7+
8+
[tool.poetry.dependencies]
9+
python = "^3.10"
10+
simplematrixbotlib = "^2.8.0"
11+
12+
13+
[build-system]
14+
requires = ["poetry-core"]
15+
build-backend = "poetry.core.masonry.api"

run.sh.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cd /home/0xmrtt/faqbot
2+
3+
export MATRIX_PASSWORD="secret password"
4+
5+
python faqbot/__init__.py
6+

tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)