Skip to content

Latest commit

 

History

History
220 lines (168 loc) · 5.54 KB

README.md

File metadata and controls

220 lines (168 loc) · 5.54 KB

The LINE bot SDK for Go (Golang)

Start using it

  • Download and install it:
$ go get github.com/dongri/line-bot-sdk-go-trial
  • Import it in your code:
$ import "github.com/dongri/line-bot-sdk-go-trial/linebot"

Examples

package main

import (
	"fmt"
	"log"
	"net/http"
	"net/url"
	"os"

	"github.com/dongri/line-bot-sdk-go-trial/linebot"
)

var botClient *linebot.Client

func main() {
	channelID := os.Getenv("LINE_CHANNEL_ID")
	channelSecret := os.Getenv("LINE_CHANNEL_SECRET")
	mid := os.Getenv("LINE_MID")
	proxyURL := getProxyURL() // can set nil if not need

	botClient = linebot.NewClient(channelID, channelSecret, mid, proxyURL)

	// EventHandler
	var myEvent linebot.EventHandler = NewEventHandler()
	botClient.SetEventHandler(myEvent)

	http.HandleFunc("/callback", linebot.Middleware(http.HandlerFunc(callbackHandler)))
	port := os.Getenv("PORT")
	addr := fmt.Sprintf(":%s", port)
	http.ListenAndServe(addr, nil)
}

func callbackHandler(w http.ResponseWriter, r *http.Request) {
	log.Print("=== callback ===")
}

func getProxyURL() *url.URL {
	proxyURL, err := url.Parse(os.Getenv("PROXY_URL"))
	if err != nil {
		log.Fatal(err)
	}
	return proxyURL
}

// BotEventHandler ...
type BotEventHandler struct {
}

// NewEventHandler ...
func NewEventHandler() *BotEventHandler {
	return &BotEventHandler{}
}

// OnAddedAsFriendOperation ...
func (be *BotEventHandler) OnAddedAsFriendOperation(mids []string) {
	botClient.SendText(mids, "友達追加してくれてありがとうね!")
}

// OnBlockedAccountOperation ...
func (be *BotEventHandler) OnBlockedAccountOperation(mids []string) {
	botClient.SendText(mids, "あらら,,, (このメッセージは届かない)")
}

// OnTextMessage ...
func (be *BotEventHandler) OnTextMessage(from, text string) {
	log.Print("=== Received Text ===")
}

// OnImageMessage ...
func (be *BotEventHandler) OnImageMessage(from string) {
	log.Print("=== Received Image ===")
}

// OnVideoMessage ...
func (be *BotEventHandler) OnVideoMessage(from string) {
	log.Print("=== Received Video ===")
}

// OnAudioMessage ...
func (be *BotEventHandler) OnAudioMessage(from string) {
	log.Print("=== Received Audio ===")
}

// OnLocationMessage ...
func (be *BotEventHandler) OnLocationMessage(from, title, address string, latitude, longitude float64) {
	log.Print("=== Received Location ===")
}

// OnStickerMessage ...
func (be *BotEventHandler) OnStickerMessage(from, stickerPackageID, stickerID, stickerVersion, stickerText string) {
	log.Print("=== Received Sticker ===")
}

// OnContactMessage ...
func (be *BotEventHandler) OnContactMessage(from, MID, displayName string) {
	log.Print("=== Received Contact ===")
}

Get user Profile

fromUser, err := botClient.GetUserProfiles(from)
if err != nil {
	log.Print(err)
}
displayName := fromUser.Contacts[0].DisplayName

Send Text

botClient.SendText([]string{from}, "Hello!")

Send Image

originalContentURL := "http://example.com//robot_original.jpg"
previewImageURL := "http://example.com//robot_preview.jpg"
botClient.SendImage([]string{from}, originalContentURL, previewImageURL)

Send Video

videoOriginalContentURL := "https://example.com/video.mp4"
videoPreviewImageURL := "http://example.com/video.png"
botClient.SendVideo([]string{from}, videoOriginalContentURL, videoPreviewImageURL)

Send Audio

audioOriginalContentURL := "https://example.com/test.mp3"
audlen := "240000"
botClient.SendAudio([]string{from}, audioOriginalContentURL, audlen)

Send Location

address := "Minato-ku, Tokyo 107-0062"
latitude := 35.665525
longitude := 139.717945
title := "俺んち"
botClient.SendLocation([]string{from}, address, latitude, longitude, title)

Send Sticker

stkID := "2"
stkpkgID := "1"
stkVer := "100"
stkText := "happy"
botClient.SendSticker([]string{from}, stkID, stkpkgID, stkVer, stkText)

Send Contact

botClient.SendContact([]string{from}, from, displayName)

Send Rich Message

func getMarkupJSON() string {
	return "{\"canvas\": {\"width\": 1024,\"height\": 576,\"initialScene\": \"scene1\"},\"images\": {\"image1\": {\"x\": 0,\"y\": 0,\"w\": 1024,\"h\": 576}},\"actions\": {\"openHomepage\": {\"type\": \"web\",\"text\": \"Open link1.\",\"params\": {\"linkUri\": \"http://dongri.github.io/\"}},\"showItem\": {\"type\": \"web\",\"text\": \"Open link2.\",\"params\": {\"linkUri\": \"https://dongri.github.io/post/2016-02-22-the-programmer-hierarchy/\"}}},\"scenes\": {\"scene1\": {\"draws\": [{\"image\": \"image1\",\"x\": 0,\"y\": 0,\"w\": 1024,\"h\": 576}],\"listeners\": [{\"type\": \"touch\",\"params\": [0, 0, 1024, 250],\"action\": \"openHomepage\"}, {\"type\": \"touch\",\"params\": [0, 250, 1024, 326],\"action\": \"showItem\"}]}}}"
}

downloadURL := "https://farm1.staticflickr.com/715/22658699705_7591e8d0a6_b.jpg"
altText := "リスト画面に表示される文字列"
markupJSON := getMarkupJSON()
botClient.SendRichMessage([]string{from}, downloadURL, altText, markupJSON)

Send MultipleMessage

messageNotified := 0
var contents []line.Content
textContent := new(line.Content)
textContent.Text = "hoge"
contents = append(contents, *textContent)

imageContent := new(line.Content)
imageContent.OriginalContentURL = "https://farm1.staticflickr.com/715/22658699705_7591e8d0a6_b.jpg"
contents = append(contents, *imageContent)
botClient.SendMultipleMessage([]string{from}, messageNotified, contents)

Util

URLShortener

shortURL := linebot.URLShortener("APIKEY", "https://map.google.com/********")
fmt.Println(shortURL) // https://goo.gl/***