signal-ctf-bot/ctf-signal.py
2024-11-24 18:27:11 +01:00

57 lines
No EOL
2.2 KiB
Python

#!/usr/bin/python3
import os
from signalbot import SignalBot, Command, Context
import logging
import json
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.DEBUG)
import requests
user_preferences={}
try:
with open("preferences.json") as ff:
user_preferences = json.load(ff)
logging.debug("Success opening preferences.json")
except Exception as ex:
print("Error opening user preferences.")
class MessageReceiveAndResponseCommand(Command):
async def handle(self, c: Context):
if c.message.text.lower().startswith(user_preferences["listen_command"]):
param = c.message.text.lower().split(" ")
if len(param) >= 3 and param[1] in user_preferences["gh_groups"]:
result = gh_groupadd(param[1], param[2])
if result == True:
await c.send("OK. Added "+param[2]+" to "+param[1]+". \n Access the repo at https://github.com/nik-soc/"+param[1])
if c.message.text == user_preferences["listen_command"]:
await c.send("""CTF Signal bot is up!\n
Available commands: \n
- evosoft25 <GitHub-username> - Adds you to the evosoft25 group to grant access to the evosoft25 repo \n
Powered by TheAdam (https://repo.theadam.eu/theadam/signal-ctf-bot)""")
def gh_groupadd(group, user):
global user_preferences
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': 'Bearer '+user_preferences["gh_token"],
'X-GitHub-Api-Version': '2022-11-28',
'Content-Type': 'application/x-www-form-urlencoded',
}
data = '{"role":"member"}'
response = requests.put('https://api.github.com/orgs/'+user_preferences["gh_org"]+'/teams/'+group+'/memberships/'+user, headers=headers, data=data)
if response.status_code == 200:
return True
else:
return False
if __name__ == "__main__":
bot = SignalBot({
"signal_service": user_preferences["signal_service"],
"phone_number": user_preferences["phone_number"]
})
bot.register(MessageReceiveAndResponseCommand(), contacts=False, groups=user_preferences["groups"])
bot.start()