Slack is a popular team communication platform used by businesses to improve collaboration and productivity. Integrating ChatGPT, an AI-powered language model, with Slack can automate various tasks and improve workflow.
In this article, I will walk you through the steps of setting up a Slack app and integrating ChatGPT using Python requests in a PC environment and Micropython urequests on the W5100S-EVB-Pico board environment.
I have set the topic as studying English words
Prerequisites- OpenAI account and API key
- Slack account
- W5100S-EVB-Pico
- Thonny: Micropython environment
The first step in integrating ChatGPT with Slack is to create a new Slack app. Follow the below steps to create a new Slack app:
- Go to the Slack API homepage and sign in to your account.
- https://api.slack.com/apps
- Click on the "Create an App" button to start the app creation process.
- Name your app and select the workspace where you want to install it.
- After creating the app, you will be redirected to the "Basic Information" page. Here, you can find your "App Credentials" such as the "Client ID, " "Client Secret, " and "Verification Token." These credentials will be used to authenticate your app and interact with the Slack API.
- Next, click on the "OAuth & Permissions" tab in the left-hand menu. Here, you will find the "Bot Token" section. Click on the "Add a Bot User" button to add a bot to your app.
- Set a display name for your bot, and click on the "Add Bot User" button to save the changes.
- You will now see a new "Bot User OAuth Access Token" generated. This token will be used to authenticate your bot and interact with the Slack API.
Test if my Slack app is working properly by sending a message to a channel using the code below.
import requests
import time
# Slack API Token and URL
SLACK_API_TOKEN = "<Slack API Token>"
SLACK_API_URL = "https://slack.com/api/chat.postMessage"
# Message to send
message = {
"channel": "#general",
"text": "Hello, World!"
}
for i in range(0, 2):
# Send message using Slack API
response = requests.post(
SLACK_API_URL,
headers={
"Authorization": "Bearer " + SLACK_API_TOKEN,
"Content-type": "application/json"
},
json=message
)
# Print response
print(response.json())
# Wait for seconds before sending the message again
time.sleep(30)
ChatGPT prompt test using Python requests in a PC environmentModified based on the ChatGPT prompt used for email transmissionI previously used the ChatGPT API to send useful emails. I have modified it based on the prompt used at that time.
When generating content to send via email, I requested it in HTML format.
However, when I sent it to the channel as is, the tags were not applied as follows:
Upon checking, I found out that in order to post markdown formed messages using the Slack API, it requires the use of a format called Blocks.
Therefore, it needs to be converted into a suitable block format for the Slack API.
Convert message format to block format suitable for Slack APIThere are several ways to convert the existing HTML format to a block format.
You can do it directly or use a library, but you can also request ChatGPT to do this for you.
I added the following content to the prompt to request a block format response that can be used for Slack API requests.
# ChatGPT Prompt part
...
Please set the format as a 'blocks' value for Slack API
...
## RETURN ONLY THE MAIN RESPONSE. REMOVE PRE-TEXT AND POST-TEXT.
When requesting in this way, it is given in block format, but parsing is required by adding explanations or variable names before and after. Parsing can be implemented in the code as it is relatively simple.
Below is an example of a response.
You need to add the urequests library. There are several ways to install the library, but I have simply copied the library file from the link below.
Create a new file in Thonny and copy and paste the entire library code from the link above. Save it as urequests.py
.
To use the Slack and ChatGPT APIs in the W5100S-EVB-Pico board environment, you need to modify the Python code to be compatible with Micropython, a lightweight implementation of Python designed for microcontrollers.
You need to add an init function to use Ethernet on the W5100S-EVB-Pico board. Then, you can modify the Python code by changing requests to urequests and fixing any compatibility issues while running it.
The main issue was setting the header and body when using urequests.
Below is the basic operation code for urequests. Both json and ujson are available, and you can use the data parameter with a value of json dump.
from machine import Pin, SPI
import network
import utime
import urequests
import ujson
# Slack API Token and URL
SLACK_API_TOKEN = "<Slack API Token>"
SLACK_API_URL = "https://slack.com/api/chat.postMessage"
# W5x00 init
def init_ethernet():
spi = SPI(0, 2_000_000, mosi=Pin(19), miso=Pin(16), sck=Pin(18))
nic = network.WIZNET5K(spi, Pin(17), Pin(20)) # spi, cs, reset pin
# Using DHCP
nic.active(True)
while not nic.isconnected():
utime.sleep(1)
# print(nic.regs())
print('Connecting ethernet...')
print(f'Ethernet connected. IP: {nic.ifconfig()}')
def main():
init_ethernet()
# Message to send
message = {
"channel": "#general",
"text": "Hello, World!"
}
# Repeat message every 30 seconds
for i in range(0, 1):
# Send message using Slack API
response = urequests.post(
SLACK_API_URL,
headers={
"Authorization": "Bearer " + SLACK_API_TOKEN,
"Content-type": "application/json"
},
data=ujson.dumps(message)
)
# Print response
print(response.json())
# Wait for seconds before sending the message again
utime.sleep(30)
main()
Test and get resultsI have written a code to fetch the body to be sent to a Slack channel using ChatGPT API and post it to the Slack channel.
I have used the time library to set it up to send messages every hour.
The code has been shared on Github.
Here are the results posted on the Slack channel:
Initially posted on the #general
channel, but later created #daily-english
channel and continued posting there.
I write and execute code on Thonny.
Check the messages received on Slack web or PC app.
The format of the messages is not consistent. Prompt improvement is needed.
Integrating ChatGPT with Slack can automate various tasks and improve workflow.
In this article, I walked you through the steps of setting up a Slack app and integrating ChatGPT using Python requests in a PC environment and Micropython urequests on the W5100S-EVB-Pico board environment.
By following the steps outlined in this article, you can easily set up ChatGPT integration with Slack and streamline your workflow.
Comments
Please log in or sign up to comment.