title: [Golang][LINE Bot SDK] How to Update Golang LINE Bot SDK v8 OpenAPI(Swagger)
published: false
date: 2024-01-04 00:00:00 UTC
tags:
canonical_url: http://www.evanlin.com/linebot-sdk-v8/
---

# Preface:
In 2023, [LINE Bot SDK actively promoted the OpenAPI](https://github.com/line/line-openapi) (a.k.a. swagger) standard interface. Through integration with OpenAPI, LINE Bot SDK has many advantages (supplemented in the second half of the article). This article will briefly explain the advantages after the introduction of OpenAPI, and guide everyone to upgrade from the v7 version to the v8 version for those who use [LINE Bot Go SDK](https://github.com/line/line-bot-sdk-go).
##### The code for this time will use the example mentioned in the previous articles: [https://github.com/kkdai/linebot-gemini-pro](https://github.com/kkdai/linebot-gemini-pro).
## Benefits of Supporting OpenAPI
[LINE Bot Go SDK](https://github.com/line/line-bot-sdk-go) also recently updated its version number to v8 in [November 2023](https://github.com/line/line-bot-sdk-go/releases/tag/v8.0.0), and officially supports [OpenAPI](https://github.com/line/line-openapi/). So, simply put, what are the benefits of an SDK package supporting OpenAPI?
#### 1. Standardized API Design
- The way many API calls are made becomes more intuitive. It's also easier to understand. This will be mentioned later.
#### 2. Code Generation

- In the past, when developing a new API Entry, you had to go through issue -> send PR -> review -> publish before it could be sent to the developers.
- However, after the introduction, the LINE Bot SDK uses Github Action to automatically fetch the latest [LINE OPENAPI Repo](https://github.com/line/line-openapi), and then generates the corresponding code based on the new changes. (Refer to the image above) (Refer to [Code Generator generated commit](https://github.com/line/line-bot-sdk-go/commit/5ab525b2ec4c673f4242f32f71f85468bdea3bb0))
More benefits:
- **Automated document generation:** There are many related documents that can help automate the generation of OpenAPI documents.
- **Client and server-side validation:** Through the introduction of OpenAPI, automated testing and Consumer-Driven Contract validation can be achieved.
- **Integration of API ecosystem tools, etc.**
## Start Migrating LINE Bot Go SDK from v7 to v8
### First, modify the package version v7 -> v8
First, add the following three packages, which will be explained later:
"github.com/line/line-bot-sdk-go/v8/linebot"
"github.com/line/line-bot-sdk-go/v8/linebot/messaging_api"
"github.com/line/line-bot-sdk-go/v8/linebot/webhook"
- `linebot`: Primarily handles v7 related content, try not to use it. It will be gradually deprecated.
- `messaging_api`: Used for sending messages.
- `webhook`: Used for receiving messages and processing related events.
Next, we will gradually integrate and start explaining the parts used by the relevant packages.
### Start integration and explain the related changes:
#### “github.com/line/line-bot-sdk-go/v8/linebot/webhook”
- Responsible for receiving Webhook related data, which includes two major series:
- `MessageEvent` to handle related message Webhooks including:
- `TextMessageContent`: Text messages.
- `StickerMessageContent`: Sticker messages...
- `ImageMessageContent`: Image messages, photos or image messages uploaded by users.
- Other related Event processing, including:
- `FollowEvent`: There are new friends.
- `PostbackEvent`: This is more commonly used, which is to use postback as user input.
- `BeaconEvent`: You can use this message to receive messages from registered Beacon HWID.
- Webhook processing methods are also slightly different, write the details:
// Need to parse the request through a different Channel Secret.
cb, err := webhook.ParseRequest(os.Getenv("ChannelSecret"), r)
if err != nil {
if err == linebot.ErrInvalidSignature {
w.WriteHeader(400)
} else {
w.WriteHeader(500)
}
return
}
// This is similar to the previous method, using the callback event to switch individual event types.
for _, event := range cb.Events {
log.Printf("Got event %v", event)
switch e := event.(type) {
case webhook.MessageEvent:
switch message := e.Message.(type) {
// Handle only on text message
case webhook.TextMessageContent:
.......
- About how to get UserId and RoomId:
#### “github.com/line/line-bot-sdk-go/v8/linebot/messaging\_api”
- This is responsible for sending related messages or Events to users. That is, you must go through this package to send any messages. This package has separated the data format:
## Summary:
This time, in addition to upgrading to the OpenAPI version, the Golang LINE Bot SDK package has also made a comprehensive 정리 of many calling methods and variable handling methods. In handling many messages, it becomes more intuitive and changes. Although there may be more changes, and some APIs are still in the process of migration. However, since the package also maintains the related packages of v7, you can continue to use the legacy API, so everyone is welcome to start integrating into the v8 version as soon as possible. This way, you can see that many updated APIs will be released at the first time.
# References:
- [LINE OPENAPI Repo](https://github.com/line/line-openapi)
- [LINE Messaging API SDK for Go Repo](https://github.com/line/line-bot-sdk-go)
Top comments (0)