mehdichaouch / email-to-slack-py

A hack to read emails on a slack channel while staying in free plan

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reading emails on slack

If your team works on a Slack workspace and the team has to maintain a bunch of different email ids and wish to be notified of every email instantly on slack; there is a hack for you.

How does it work?

After setting up your slackbot chat as a forward emailing address of your email account (Steps expalined below) this bot watches for messages in the slackbot chat and whenever a message arrives with an attached email, It forwards it to a channel as a message.

How to make it work ?

  1. Set up a forwarding email. If you send any email to this address, it will appear as a file in your Direct Messaging channel with slackbot.
  2. Add this address as a forwarding address of your email account. (For gmail find forwarding options from gmail settings)
  3. Use Slack API to get notified about this.
  4. Use Slack API to post the email on a channel you want to.

Detailed steps

Step 1 and 2:

  • Navigate to Slack preferences -> Messages and Media -> Bring emails to slack. It will generate an email address for you. Any email sent to this address will appear in your DM with Slackbot.

  • Go to the email provider from where you want to forward the messages to slack. In the forwarding options, put the email generated by slack to forward email to.

  • In case of Gmail, you will have to provide a verification token which is sent to your slackbot.

For Step 3 and 4:

  • Create a bot account for this purpose. Add it to your workspace.
  • Go to api.slack.com/apps and create a new Slack app.
  • After creating the app, navigate to the app dashboard and find "Event Subscriptions" in the sidebar.
  • Subscribe to the event message.im which is the only event we need to be notified about.
  • Go to "OAuth and Permissions" and add files:read, im:history and incoming-webhook scopes.
  • Install it in your workspace
  • Activate incoming webhook for a channel. This will let you easily post to a channel.
  • Add the URL of your server (A heroku app which can be deployed from this repository).
  • The payload is then recieved and parsed.
    • Slackbot uploads the email as a file.
  • Refer to SLACK API FAQ for more help

Description of environment variables used by the repository

  • Set these enviroment variables in your heroku server which you hosted
    • You can easily change using Heroku Dashboard or using Heroku CLI.
    • For more detail see Heroku Config Var
Config Variable Description
APP_ID You get this when you create the app
INCOMING_WEBHOOK_URL You get this when you install the app on one channel
TEAM_ID ID of your slack workspace. See this
USLACKBOT_CHANNEL The ID of the direct messaging channel between you and @slackbot
VERIFICATION_TOKEN You get this when you create the app

Note that the Slack IDs (for channel, users, files) are alphanumeric uppercase string of 9 characters.

Notes:

  • Slack creates a beautiful file for an email. But I could not find way to change file permissions which is shared privately with just the user. Hence, I have to customize and post the email. If there is a way to change file permissions from private to be shared in a team, that would be easy and the best way.

About

A hack to read emails on a slack channel while staying in free plan


Languages

Language:Python 100.0%