Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple channels, Attachments and Docker #9

Open
wants to merge 25 commits into
base: master
Choose a base branch
from

Conversation

psyciknz
Copy link

Added a few features.

  • Changed from a single webhook to a discord bot. And uses a token. With this it can post to mulitple channels via the channelid.
  • Added a config file where you can set rules for which channel gets what content. Filtered by from, to or subject. At the moment, mutually exclusive.
  • Added a dockerfile and github actions for building. Does require a couple of secrets in the repository though. DOCKER_HUB_ACCESS_TOKEN and DOCKER_HUB_USERNAME

[default]
# Create a discord channels, and copy the channel ID to this default section.
# if this section is missing it will fail to start
# for subsequent sections, is no channel ID is specified it will default to this ID.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# for subsequent sections, is no channel ID is specified it will default to this ID.
# for subsequent sections, if no channel ID is specified it will default to this ID.

@@ -69,6 +153,10 @@ def discord_field(self, name, value="", inline=False):
"inline": inline
}
return r
#def discord_field(self, name, value="", inline=False):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the discord_field function isn't actually used anymore, so it should be deleted.

Comment on lines +92 to +114
self.client.channelid = channelid
self.client.subject = subject
self.client.embeds = discord.Embed(title=subject,description="desc")
self.client.embeds.add_field(name="To",value=to_addr,inline=False)
print(f'Email To: "{to_addr}"')
self.client.embeds.add_field(name="From",value=from_addr)
print(f'Email From: "{from_addr}"')
self.client.embeds.add_field(name="Subject",value=subject)
if body is not None:
chunklength = 1000
chunks = [body[i:i+chunklength] for i in range(0,len(body),chunklength)]
print('Email Body length: %s = %s chunks' % (len(body),len(chunks)))
chunknum = 1
for chunk in chunks:
self.client.embeds.add_field(name="Body-%s" %chunknum,value=chunk)
chunknum += 1

print('Adding Attachments: %s' % len(attachments) )
self.client.files = attachments

print('Sending email')
#reset flag
self.client.msg_sent = False
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one's a bit of a bigger comment, but I don't really like how this tells the discord bot to send the message. It looks like this is setting some fields in the bot object, then turning on the msg_sent flag which the bot looks for in a loop. But I suspect that will have concurrency issues (like if multiple messages come in at once).

I think a better way would be to send an event, passing all of the information as parameters. It looks relatively easy to send custom events:
https://stackoverflow.com/questions/64810905/how-do-i-emit-custom-discord-py-events

Then instead of a timer, you could simply register that event and send the message when it fires.

I'm envisioning something along the lines of

embeds  = discord.Embed(title=subject,description="desc")
embeds.add_field(name="To",value=to_addr,inline=False)
...
print('Sending email')
await client.emit('send_email', channelid, subject, embeds, attachments)

Then in the bot, instead of

    @tasks.loop(seconds=1)
    async def timer(self):
        try:
            if not self.msg_sent:
                print("Email content passed to bot for processing.")
                ...

it would be like:

async def on_send_email(channelid, subject, embeds, attachments):
    print("Email content passed to bot for processing.")
    # all the stuff for sending the message to the channel
    ...

Is this something you'd want to try doing? If not, I can accept this as-is and give it a shot.

@@ -0,0 +1,15 @@
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, the repo shouldn't include IDE configuration files. I'd delete this file and add .vscode to the gitignore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants