This is the third in a series that creates automated English study content by integrating the ChatGPT API.
I'm trying to integrate various platforms. The first was SendGrid (email), and the second was Slack.
APIs allow you to automate functions in the form of requests/responses, even on low-resource devices.
I used the Raspberry Pi's RP2040 and WIZnet's iEthernet chip based devices below.
For reference, the existing project can be referenced at the following link.
- Integrating ChatGPT with Slack API on W5100S-EVB-Pico: Making English Study Bot
- Boost Your Day with Helpful Emails: W5100S-EVB-Pico and ChatGPT API in Action
In this article, we will explore how the ChatGPT API can be integrated with the Tistory platform to create automated English learning blogs.
By combining the power of ChatGPT, a state-of-the-art language model, with Tistory, a popular blogging platform, learners can access personalized English study prompts and enhance their language proficiency.
Prerequisite- Tistory Account: Blog Platform
- W5100S-EVB-Pico or W5500-EVB-Pico
- Thonny: Micropython environment
To begin our journey of creating automated English learning blogs, we first need to set up the Tistory app. This involves the following steps:
a. Create the App
Creating the Tistory app is the initial step towards integrating the Tistory API with ChatGPT. By registering the app in the Tistory Developer Center and providing the necessary information, such as the app name, description, and website URL, we can obtain the credentials required to access the Tistory API.
Official documentation of Tistory API can be found at the link below.
To obtain an access token for API calls, you must go through the following process.
- Authentication request and Authentication code issuance
- Obtain Access Token
This process is described in the link below.
https://tistory.github.io/document-tistory-apis/auth/authorization_code.html
When you create an app, you can check your App ID and Secret Key. This value is required for authentication requests.
Authentication request and authentication code issuance
https://www.tistory.com/oauth/authorize?
client_id={client-id}
&redirect_uri={redirect-uri}
&response_type=code
&state={state-param}
Obtain Access Token
Just fill in the value and paste it into the address bar of your browser. To check the result right away, press F12 to open the developer tools window. (Chrome browser)
GET https://www.tistory.com/oauth/access_token?
client_id={client-id}
&client_secret={client-secret}
&redirect_uri={redirect-uri}
&code={code}
&grant_type=authorization_code
When accessing with the address, an access permission window appears, and here, click the Grant button to allow permission.
Through this process, an Tistory access token can be issued.
2. Test the Tistory APIOnce we have set up the Tistory app and obtained the access token, it is essential to test the functionality of the Tistory API. In this step, we will focus on developing and testing a simple post code using the popular Python library, requests
.
a. Develop and Test a Simple Post Code using 'requests'
Using the requests
library, we can write a code snippet that sends a POST request to the Tistory API's post.write
endpoint. This code will include the necessary parameters, such as the access token, blog name, title, and content of the post. By testing this code, we can verify that the API call is successful and that the created post appears correctly on our Tistory blog.
Use the code below to test whether the issued access token works well.
Fill the TISTORY_ACCESS_TOKEN
, TISTORY_BLOG_NAME
variables with values.
import requests
import json
# set Tistory API endpoint URL and access token
TISTORY_API_URL = "https://www.tistory.com/apis/post/write"
TISTORY_ACCESS_TOKEN = "<Access token>"
# set Tistory blog name and post data
TISTORY_BLOG_NAME = "<Blog name>"
TISTORY_POST_TITLE = "First Post"
TISTORY_POST_CONTENT = "This is Post Content"
# create Tistory API request headers
headers = {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
}
# create Tistory API request data
data = {
"access_token": TISTORY_ACCESS_TOKEN,
"output": "json",
"blogName": TISTORY_BLOG_NAME,
"title": TISTORY_POST_TITLE,
"content": TISTORY_POST_CONTENT
}
# send Tistory API request using requests
response = requests.post(TISTORY_API_URL, headers=headers, data=data)
print(f'response: {response.content.decode("utf-8")}')
# parse response data and print status
response_data = json.loads(response.content.decode("utf-8"))
print(response_data["status"])
If the request is successful, you can receive status, postId, and url as a response as follows.
Below is the first post published using the API.
The next step is to write the main code that will generate the ChatGPT English study prompt and integrate the ChatGPT API with the Tistory API.
a. Generate a ChatGPT English Study Prompt
To enhance the learning experience on our automated English learning blogs, we can leverage the power of ChatGPT to generate personalized English study prompts. This step involves modifying the return format for Tistory posts to include study questions and answers.
b. Modify the Return Format for Tistory Post
The link above is a Slack interlocking project that was previously carried out. I created updated prompts based on the prompts I used at this time.
The first important condition is to generate the response format in HTML format. After that, since this is a blog post, I wanted more explanation.
Added the following phrases: First, I drafted a prompt using ChatGPT and added and subtracted the content to create the prompt.
The Title: The title of content have to contain the featured 2 intermediate-level English words with HTML format.
Other Content: Inscribe the remaining sections of the blog post, such as introductions, conclusions, and supplementary information, in Korean while maintaining a formal tone. Integrate relevant examples, anecdotes, or personal insights in Korean to captivate the readers and establish a deeper connection with the content.
Practical Applications: Incorporate practical, real-life examples or scenarios in Korean that vividly demonstrate the application of the discussed language skills in everyday situations. This approach enables learners to grasp the significance and effectiveness of their learning journey.
The Title
And, since the blog post needs a title, I wrote additional code to extract the title from the HTML code. Use regular expression patterns.
def get_title(content):
title = None
try:
# Define the regex pattern to match the title tag
pattern = r"<title>(.*?)</title>"
# Find the match using regex
match = re.search(pattern, content)
# Extract the title if a match is found
if match:
title = match.group(1)
print(title)
except Exception as e:
print(e)
return title
c. Integrate ChatGPT API with Tistory API
To fully harness the potential of both the ChatGPT API and the Tistory API,
we can integrate the two to create a seamless and efficient learning experience for English learners. This integration involves modifying the code to make it compatible with Micropython using the urequests
library.
W5500-EVB-Pico is a microcontroller evaluation board based on the Raspberry Pi RP2040 and fully hardwired TCP/IP controller.
In this step, we will upload the firmware and update the application code
The first step is to upload the firmware to the W5500-EVB-Pico.
The board can be connected like this: Use an Ethernet network by supplying power with a 5V micro USB and connecting a LAN cable.
a. Upload Firmware
The first step is to upload the firmware to the W5500-EVB-Pico. This can be done using the firmware upload tool provided by the manufacturer.
The firmware can be downloaded from the link below.
As of the current date, the latest firmware version is: v1.20.0 (2023-04-26) .uf2 [Release notes] (latest)
If you press the RUN
button while pressing the BOOTSEL
button on the board, the RPI-RP2
drive will appear on the PC. Copy the .uf2 file you downloaded here.
The firmware is uploaded and the drive is automatically unrecognized.
b. Update Application Code using urequests for Micropython
To integrate the ChatGPT API with the Tistory API in a resource-constrained environment, we can modify the code using the urequests library, which is suitable for Micropython-based devices. This modification enables English learners to access ChatGPT-generated study prompts on devices with limited computational capabilities.
c. Error handling
In the process of updating and testing the Micropython code, the following error occurred.
There was no problem when testing on PC Python, but the problem occurred because the way to use Micropython's urequests was slightly different.
In conclusion, it was a problem that could be easily solved by setting the header encoding format, but I had a little trouble.
The test code that was previously integrated with Slack was helpful.
The header was modified as follows.
# Previous: {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}
# Modified
headers["Content-Type"] = "application/json; charset=utf-8"
Here is an example of a successful log.
After integrating the ChatGPT API with the Tistory API and generating English study prompts, it is essential to review the created posts on the Tistory blog. This step ensures that the automated process functions as expected and that the generated content is accurate and visually appealing.
It's not easy to get a consistent response form. At first, I got more raw responses, but as I corrected the prompts, I got more consistent answers.
The automatically created English study blog post reads:
Still have the following problems:
- Redundant English words: separate database required
- If you give an off-topic response
- Giving responses that do not contain enough information
In conclusion, the integration of the ChatGPT API with the Tistory platform opens up exciting possibilities for creating automated English learning blogs.
Comments
Please log in or sign up to comment.