Slack news bot based on ChatGPT using W5500-EVB-Pico
Slack news bot based on ChatGPT using W5500-EVB-Pico with Micropython.

Software Apps and online services
Overview
This is the second version of the previous project. In the last project, I carried out sending a specific message by linking the W5100S-EVB-Pico, Slack, and ChatGPT APIs.
In the previous version, requests were used to focus on request-response, but this time, I tried to create more practical and rich messages using sensor data and additional service APIs.
Also, this time, W5500-EVB-Pico was used, and a development kit called Picobricks was additionally used.
Component
Hardware
- W5500-EVB-Pico
- Picobricks
Platform
- Slack
- NewsAPI
- OpenAI Chat API
Development environment
- Thonny (Micropython)
The setting to use Slack and OpenAI API is not written separately because it was done in the previous project. If necessary, relevant content can be found in existing projects.
- English study bot using ChatGPT with Slack
- Boost Your Day with Helpful Emails: W5100S-EVB-Pico and ChatGPT API in Action
Picobricks
Picobricks is an open-source platform that makes it easy to build projects using modularized components.
It is convenient because you can easily use sensors by controlling designated pins by simply plugging in the board without the trouble of connecting jumpers using a breadboard. Separate modules can also be connected using Grove cables.
This time, only the temperature and humidity sensor and the illuminance sensor were used.
More details can be found in the link.
This time, only the temperature and humidity sensor and the illuminance sensor were used.
Process
Firmware
The firmware used the official W5500-EVB-Pico firmware provided by Micropython. The version used is below. This is the latest version as of now.
v1.20.0 (2023-04-26) .uf2 [Release notes]
https://micropython.org/download/W5500_EVB_PICO/
Sensor
DHT11 Test
When using the library provided by Picobricks, the operation may or may not work, but when it does not work, the following error occurs.
When I searched, it seemed that many people had the same problem.
It seems to be a problem with the use of the sensor itself, not with Picobricks. After some searching, I found that the latest release of Micropython firmware contains the driver.
You can use it simply by importing it like so:
import dht
import machine
import utime
dht11 = dht.DHT11(machine.Pin(11))
for i in range(0, 5):
dht11.measure()
print(f'Temperature: {dht11.temperature()}, Humidity: {dht11.humidity()}')
utime.sleep(1)
In the case of the light sensor, the value can be easily obtained by using the ADC and specifying only the Pin number.
This is the result of sending a simple message by adding only the sensor measurement value in the existing project.
News API

I thought about news when I was wondering if I could give you some real useful information. Sometimes, if you just ask ChatGPT, it will be made(?), but it often gives an empty response, so I thought it would be nice to receive actual news.
I found out that there is an API that is supported for free. See ChatGPT answer.
You can sign up through the link above, and the sign up process and use are very simple!
It is free for development and non-commercial use. Instead, requests are limited to 100 per day. Same as Sendgrid.
You can check how much of the free 100 you have used on the My Account page of newsapi.


Upon signing up, an API Key is issued immediately.
This value is also kindly sent by e-mail, so you can refer to it on the page without copying it separately.
You can get the value using Get Started in the document, and if you are logged in, the API key for your account automatically enters and it was very convenient to just copy-paste it.
Thanks to this, I was able to quickly test and apply.
I tested it with Postman and it works fine. Various information is included in the return value.
I also tested it with the Python code provided by ChatGPT.
There are two options, kr and us, and the number of pages is set to 5. And the category was set to the field of interest, technology. You can set each one to your liking.
Below are the results of requests in Korean and English, respectively.
Korean(kr)
English(us)
Since the API operation was checked with Python, the test was conducted by modifying the code with Micropython.
Added the ethernet configuration code and included it directly in the URL as urequests does not support the param option.
The following is the Micropython test code of the News API.
from machine import Pin, SPI, ADC
import network
import urequests
import json
import utime
NEWS_API_KEY = <API Key>
# 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 get_news_list(api_key):
url = f"https://newsapi.org/v2/top-headlines?country=us&apiKey={api_key}&category=technology&pageSize=5"
print(f'url: {url}')
response = urequests.get(url)
print(f'response: {response}')
if response.status_code == 200:
data = json.loads(response.text)
if data["status"] == "ok":
news_articles = data["articles"]
news_list = [f"News {i + 1}) [{article['title']}]({article['url']})" for i, article in enumerate(news_articles)]
print(f'news_list: {news_list}')
return news_list
else:
print("News request failed.")
return None
else:
print(f'Error: {response.status_code}')
return None
init_ethernet()
news_list = get_news_list(NEWS_API_KEY)
if news_list:
news_str = ', '.join(news_list)
print(f'news_str: {news_str}')
else:
print("Failed to get news summary.")
The response includes various information about the news article, and ChatGPT was used to use the title and link and convert it to markdown format.
The remaining task is to put this into a Slack block message.
After the sensor measurement value of the user input part, the news list was converted into a String type and added.
Memo: Using prompts as strings instead of dictionaries
At first, the data was configured as a dictionary, but even after converting using json.dumps()
, the value was not viewed in the correct JSON format in the OpenAI API. If you do it on the web JSON validation site, it says that it is a valid JSON value, but there seems to be a filtering part.
So, I changed all of them to string type.
# Get news list
news_list = get_news_list(NEWS_API_KEY)
news_str = ', '.join(news_list)
dht11.measure()
user_text = f'Temperature: {dht11.temperature()}, Humidity: {dht11.humidity()}, Light: {ldr.read_u16()}, {news_str}'
The output that worked fine is: It feels happy when I receive a completed message after several attempts.😊
It contains all the information I requested.
- sensor data
- News of the day
- Word of the day (study)
- Quote of the day
When I tried to receive the block code for Slack, the code for processing strings and handling exceptions for responses became a bit long, which is a part that needs improvement.
The output (ChatGPT's answer) is not constant, so there are parts to refine. That part isn't easy.
Still, I was able to get better results than before by adjusting the system and user input according to the guide document of Open AI.
If you use the ChatGPT API, I recommend that you refer to it.
https://platform.openai.com/docs/guides/gpt-best-practices)
Conclusion
Depending on which API or service is used, the results can be output in various ways, so I wonder if I can do more interesting things.
Although there are limitations, since there are so many APIs that you can try for free, you can implement functions such as receiving real-time weather information or receiving financial information, and take action based on the data.
Recently, it seems that there are many APIs related to AI.
Below is a list of APIs that ChatGPT notified.
I couldn't check if all the links were working, but the Cat Facts API caught my eye, so I went in and there was an API that tells the history and information about cats. I only thought of development, but this kind of API is also fresh and fun.
Next time, I want to add functions such as remote control and use new APIs to create more useful and fun projects in real life.
It would be good if each of us composed a message that could be usefully used in our own environment.