Skip to content

Instantly share code, notes, and snippets.

@harej
Created March 26, 2023 18:03
Show Gist options
  • Save harej/599a03795616c3e6810639cea2afc263 to your computer and use it in GitHub Desktop.
Save harej/599a03795616c3e6810639cea2afc263 to your computer and use it in GitHub Desktop.
Phabricator task creation bot for IABot IRC channel
import irc.client
import requests
import re
from credentials import (
IRC_SERVER, IRC_CHANNEL, IRC_NICKNAME,
PHABRICATOR_API_KEY, PHID
)
authorized_hostnames = [
"user/hare",
"wikipedia/Cyberpower678",
]
authorized_telegram_users = [
"<Cyberpower678>",
"<harej>",
]
def is_authorized(user):
return any(user.host.endswith(hostname) for hostname in authorized_hostnames)
def is_telegram_authorized(message):
return any(message.startswith(f"[telegram] {user}")
for user in authorized_telegram_users)
def create_task(url, message):
api_url = "https://phabricator.wikimedia.org/api/maniphest.createtask"
params = {
"api.token": PHABRICATOR_API_KEY,
"title": message,
"description": f'Reported via IRC/Telegram.\n\nReported URL: {url}',
"projectPHIDs[]": PHID
}
response = requests.post(api_url, data=params)
return response.json()
class IRCBot(irc.client.SimpleIRCClient):
def on_welcome(self, connection, event):
connection.join(IRC_CHANNEL)
def on_pubmsg(self, connection, event):
message = event.arguments[0]
match = re.match(r"!task (https?://\S+) (.+)", message)
if (match and (is_authorized(event.source)
or (event.source.nick == "wm-bb"
and is_telegram_authorized(message)))):
url, task_message = match.groups()
response = create_task(url, task_message)
if "result" in response:
task_url = f'https://phabricator.wikimedia.org/T{response["result"]["id"]}'
connection.privmsg(IRC_CHANNEL, task_url)
else:
error_msg = f'Error creating task: {response.get("error_info")}'
connection.privmsg(IRC_CHANNEL, error_msg)
if __name__ == "__main__":
bot = IRCBot()
bot.connect(IRC_SERVER, 6667, IRC_NICKNAME)
bot.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment