I created a Discord server for an alumni’s mailing list since many people asked “Do we have a Slack group or a Discord server?” and there wouldn’t ...
For further actions, you may consider blocking this person and/or reporting abuse
The code is already in this post. (Line 4)
Thanks for this, it is really useful and lightweight.
I've got a company account which only rarely receives emails, and I am always forgetting to check it. I have rejigged this code slightly so that it only checks for unread messages and forwards those to the specified discord channel (I've made a private channel only accessible to me on the server). Finally, it marks these messages as read so that you don't get repeat messages. Moving my email notification system to discord 🙂
Does this work? How will the script find the email without a search Query?
It does indeed work, it has been running since this post in fact.
The script doesn't need to search for emails, as it just grabs the all the unread emails, forwards them to discord and finally marks them as read, so you don't get repeat notifications.
It figures out what emails are unread with this line:
However, there is a bug in the code... can you see it?
postMsg
is defined as aconst
but is reassigned if the email message is longer than 2000. That is the only change I have had to make to the scriptDoes this script still work? I've been trying to get it to work but nothing is happening. I believe I've entered in the correct webhook/token as I'm not getting any error messages when running the script. I'm seeing Execution started and Execution completed.
I've only changed two things in the the script. The channel name and the searchQuery, but it yields nothing. I also double checked that the webhook has permission to post in the channel. Any ideas to what I'm doing wrong?
Nvm, I figured out what I was doing wrong. I wasn't pushing the correct command. Thanks again for the script as it works well!
Nice, glad to hear that!
I'm thinking of writing a new post about this since I've received some comments.
There seems to be an issue or it does not work anymore 😥 I followed the instructions (great guide btw) and when I run the script, I get the following error. Any ideas on what I'm doing wrong?
*Error Exception: *
DNS error: 1079268134118248508
postDiscord @ Code.gs:21
sendMailsToDiscord @ Code.gs:60
Here's the modification I made to the script:
The script itself still works as expected on my end.
Are you sure that you set the webhook url and token properly and pass the proper params?
Yeah I double checked. The 'classic' version of the properties pane does not seem to be available anymore. Here is what I have on my end when I go to the left-hand menu bar (Project Settings --> Script Properties):
For the script properties, am I supposed to provide a URL or how is it supposed to be laid out? I ask as I followed on how to dissect the Discord webhook via its ID and TOKEN.
EDIT: I was able to get it to work "somewhat" by changing the script properties to the following:
WEBHOOKS || discord.com/api/webhooks/100000009...
TOKEN || hg_SMWJNfoo
Now when I click execute, it appears to fully execute. However, I am not receiving any messages in that Discord channel itself. Any ideas? The triggers are setup correctly. Unless it does not work when you manually execute it and just have to give it some time? Is there anything else I need to do once it's all setup? Here is what I have for the search criteria:
from: emailaddress@gmail.com
Also, see below for the script execution output:
That sounds your discord access right issue.
Are you sure that you allowed the bot/account to post a message to the channel?
I am getting 401 unauthorized errors. You only see them when you set
'muteHttpExceptions': false
My variables are set as folows:
const webhooks = 'discord.com/api/webhooks/00002345140112979';
const token = 'nottherealtokenki56MxTtZW2opjtoWxuSICbOnuO';
which are the left and right part of the url from copy token in discord.
Is there a way to test if the webhook actually works?
another thing: with messages longer than 2000 I got a value assigned to const error, so I removed const from postMsg
If on the off chance you still need this, webhooks needs to include the https:// and include the token at the end: discord.com/api/webhooks/yourid/yourtoken.
To test the script, you need to run SendMailsToDiscord() and if any emails were recieved within the last 30mins (or your specified interval) it should send a discord message.
I was getting 405s for hours, turns out you need the token on the end of the url as well as a variable. If you run PostDiscord(postMsg) you should get a 400 error as the content is undefined.
Hope this helps if you still need it