GithubHelp home page GithubHelp logo

line-bot-sdk-go-1's Introduction

The LINE bot SDK for Go (Golang)

Start using it

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

Examples

package main

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

	"github.com/dongri/line-bot-sdk-go/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", 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/***

line-bot-sdk-go-1's People

Stargazers

iThanos avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.