# Coding a personal assistant on Slack A while back Google remove the option to use their email adresses on low security settings. Which I was using with one of my dummy emails as a way to send me emails when a remote job is done. I thought about looking for an alternative and wondered how difficult it would be to transfer this messaging task to a Slack Bot. Turns out it's not that difficult: 1. Install the Slack Developer Kit for Python: a. Open a terminal window and run the command: `pip install slack-sdk`. 2. Get the bot token: a. In the Slack API dashboard, go to the "Install App" section. b. Click "Install App to Workspace" and follow the prompts. c. Allow Bot token. I used the followin ones:`channels:join`, `channels:read` and `channels:write` d. Copy the "Bot User OAuth Access Token" - this will be used to authenticate your bot. 3. Bot code - Auth Set the bot token as environment variable ```bash export SLACK_BOT_TOKEN="THE_TOKEN" ``` - To join a channel: ```python= import os from slack_sdk import WebClient from slack_sdk.errors import SlackApiError client = WebClient(token=os.environ['SLACK_BOT_TOKEN']) channel_id = 'YOUR_CHANNEL_ID' try: response = client.conversations_join(channel=channel_id) print("Bot has joined the channel: ", response['channel']['name']) except SlackApiError as e: print("Error joining channel: {}".format(e)) try: response = client.chat_postMessage( channel=channel_id, text="Hello, World! I am Wilfred your personal assistant." ) print("Message sent: ", response['message']['text']) except SlackApiError as e: print("Error sending message: {}".format(e)) ``` - To post a message This code when used as `python wilfred_message.py "My message"` wil post "My message' in the channel joined above. ```python= import os import sys from slack_sdk import WebClient from slack_sdk.errors import SlackApiError client = WebClient(token=os.environ['SLACK_BOT_TOKEN']) channel_id = 'CU6LRKTPT' message = " ".join(sys.argv[1:]) try: response = client.chat_postMessage( channel=channel_id, text=message ) print("Message sent: ", response['message']['text']) except SlackApiError as e: print("Error sending message: {}".format(e)) ``` And voila! ![](https://hackmd.io/_uploads/HyUAkfO43.png) ###### tags: `Bot`, `Slack`, `API`, `Python`