Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #17 from soluna-studios/support_discord
Browse files Browse the repository at this point in the history
feat(discord): added built-in connector for discord
  • Loading branch information
jaredhendrickson13 authored Aug 13, 2022
2 parents d1b1111 + 748e05c commit b6767c8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
13 changes: 12 additions & 1 deletion docs/DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ configuration. This name will be used to assign this connector to mappings in yo
**module**

- _Required_: Yes
- _Options_: [`void`, `smtp`, `slack`, `google_chat`, `microsoft_teams`]
- _Options_: [`void`, `smtp`, `slack`, `discord`, `google_chat`, `microsoft_teams`]
- _Description_: The module this connector will use. Multiple connectors can use the same underlying module.

**config**
Expand Down Expand Up @@ -687,6 +687,17 @@ created for your channel beforehand. Available options for this module are:
- _Description_: The full Slack webhook URL. For help with creating a webhook, refer to
https://api.slack.com/messaging/webhooks

## ```discord```
The `discord` module allows SMTP messages to be redirected to a Discord channel using a webhook. A webhook must be
created for your channel beforehand. Available options for this module are:

**webhook_url**

- _Required_: Yes
- _Description_: The full Discord webhook URL. For help with creating a webhook, refer to
https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks


## ```google_chat```
The `google_chat` module allows SMTP messages to be redirected to a Google Chat space using an app webhook. A webhook
must be created for your space beforehand. Available options for this module are:
Expand Down
1 change: 1 addition & 0 deletions mail2beyond/connectors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
from . import void
from . import smtp
from . import slack
from . import discord
from . import google_chat
from . import microsoft_teams
35 changes: 35 additions & 0 deletions mail2beyond/connectors/discord.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Creates the built-in 'discord' connector that can be used to translate SMTP messages into Discord messages.
"""

import requests

from mail2beyond import framework


class Connector(framework.BaseConnector):
"""Defines a connector that allows mail2beyond to integrate with Discord."""
name = "discord"

def submit(self, parser):
"""Overwrites the submit() method to send a Discord message."""
# Submit this message to Discord via webhook URL
try:
resp = requests.post(
url=self.config["webhook_url"],
headers={'Content-Type': 'application/json'},
json={"content": f"*{parser.subject}*\n\n{parser.content}"}
)
self.log.debug(f"connector '{self}' responded with {resp.status_code} status {resp.text}")
except Exception as discord_req_err:
self.log.error(f"connector '{self}' failed '{discord_req_err}'")
raise framework.Error(f"connector '{self}' : {discord_req_err}")

def pre_submit(self, parser):
"""Overwrites the pre_submit() method to ensure required config values are set."""
# Ensure ths connector has a config value for 'webhook_url'
if "webhook_url" not in self.config:
raise framework.Error(f"connector '{self}' requires config value 'webhook_url'")
# Ensure the webhook_url config value is a string
if not isinstance(self.config["webhook_url"], str):
raise framework.Error(f"connector '{self}' config value 'webhook_url' must be type 'str'")

0 comments on commit b6767c8

Please sign in to comment.