Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

LINE Messaging API SDK for Go

License

NotificationsYou must be signed in to change notification settings

line/line-bot-sdk-go

Repository files navigation

Build StatuscodecovGo ReferenceGo Report Card

Introduction

The LINE Messaging API SDK for Go makes it easy to develop bots using LINE Messaging API, and you can create a sample bot within minutes.

Documentation

See the official API documentation for more information.

Requirements

This library requires Go 1.22 or later.

Installation

$ go get -u github.com/line/line-bot-sdk-go/v8/linebot

Import all packages in your code

import ("github.com/line/line-bot-sdk-go/v8/linebot""github.com/line/line-bot-sdk-go/v8/linebot/channel_access_token""github.com/line/line-bot-sdk-go/v8/linebot/insight""github.com/line/line-bot-sdk-go/v8/linebot/liff""github.com/line/line-bot-sdk-go/v8/linebot/manage_audience""github.com/line/line-bot-sdk-go/v8/linebot/messaging_api""github.com/line/line-bot-sdk-go/v8/linebot/module""github.com/line/line-bot-sdk-go/v8/linebot/module_attach""github.com/line/line-bot-sdk-go/v8/linebot/shop""github.com/line/line-bot-sdk-go/v8/linebot/webhook")

Configuration

import ("github.com/line/line-bot-sdk-go/v8/linebot/messaging_api")funcmain() {bot,err:=messaging_api.NewMessagingApiAPI(os.Getenv("LINE_CHANNEL_TOKEN"),)...}

Configuration with http.Client

Every client application allows configuration with WithHTTPClient and WithEndpoint.(For Blob client, configurations WithBlobHTTPClient and WithBlobEndpoint are also available.)

client:=&http.Client{}bot,err:=messaging_api.NewMessagingApiAPI(os.Getenv("LINE_CHANNEL_TOKEN"),messaging_api.WithHTTPClient(client),)...

Getting Started

The LINE Messaging API primarily utilizes the JSON data format. To parse the incoming HTTP requests, thewebhook.ParseRequest() method is provided. This method reads the*http.Request content and returns a slice of pointers to Event Objects.

import ("github.com/line/line-bot-sdk-go/v8/linebot/webhook")cb,err:=webhook.ParseRequest(os.Getenv("LINE_CHANNEL_SECRET"),req)iferr!=nil {// Handle any errors that occur.}

The LINE Messaging API is capable of handling various event types. The Messaging API SDK automatically unmarshals these events into respective classes likewebhook.MessageEvent,webhook.FollowEvent, and so on. You can easily check the type of the event and respond accordingly using a switch statement as shown below:

for_,event:=rangecb.Events {switche:=event.(type) {case webhook.MessageEvent:// Do Something...case webhook.StickerMessageContent:// Do Something...}}

We provide codeexamples.

Receiver

To send a message to a user, group, or room, you need either an ID

userID:=event.Source.UserIdgroupID:=event.Source.GroupIdRoomID:=event.Source.RoomId

or a reply token.

replyToken:=event.ReplyToken

Create message

The LINE Messaging API provides various types of message.

bot.ReplyMessage(&messaging_api.ReplyMessageRequest{ReplyToken:e.ReplyToken,Messages: []messaging_api.MessageInterface{messaging_api.TextMessage{Text:replyMessage,},},},)

Send message

With an ID, you can send message usingPushMessage()

bot.PushMessage(&messaging_api.PushMessageRequest{To:"U.......",Messages: []messaging_api.MessageInterface{messaging_api.TextMessage{Text:pushMessage,},},},"",// x-line-retry-key)

With a reply token, you can reply to messages usingReplyMessage()

bot.ReplyMessage(&messaging_api.ReplyMessageRequest{ReplyToken:e.ReplyToken,Messages: []messaging_api.MessageInterface{messaging_api.TextMessage{Text:replyMessage,},},},)

How to get response header and error message

You may need to store thex-line-request-id header obtained as a response from several APIs. In this case, please use~WithHttpInfo. You can get headers and status codes. Thex-line-accepted-request-id orcontent-type header can also be obtained in the same way.

resp,_,_:=app.bot.ReplyMessageWithHttpInfo(&messaging_api.ReplyMessageRequest{ReplyToken:replyToken,Messages: []messaging_api.MessageInterface{messaging_api.TextMessage{Text:"Hello, world",},},}, )log.Printf("status code: (%v), x-line-request-id: (%v)",resp.StatusCode,resp.Header.Get("x-line-request-id"))

Similarly, you can get specific error messages by using~WithHttpInfo.

resp,_,err:=app.bot.ReplyMessageWithHttpInfo(&messaging_api.ReplyMessageRequest{ReplyToken:replyToken+"invalid",Messages: []messaging_api.MessageInterface{            messaging_api.TextMessage{Text:"Hello, world",            },        },    },)iferr!=nil&&resp.StatusCode>=400&&resp.StatusCode<500 {decoder:=json.NewDecoder(resp.Body)errorResponse:=&messaging_api.ErrorResponse{}iferr:=decoder.Decode(&errorResponse);err!=nil {log.Fatal("failed to decode JSON: %w",err)    }log.Printf("status code: (%v), x-line-request-id: (%v), error response: (%v)",resp.StatusCode,resp.Header.Get("x-line-request-id"),errorResponse)}

Help and media

FAQ:https://developers.line.biz/en/faq/

News:https://developers.line.biz/en/news/

Versioning

This project respects semantic versioning.

Seehttp://semver.org/

Contributing

Please checkCONTRIBUTING before making a contribution.

License

Copyright (C) 2016 LINE Corp.Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at   http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.

[8]ページ先頭

©2009-2025 Movatter.jp