I made a LINE BOT that returns parrots with Go

I made a LINE BOT that returns parrots by referring to the sample program below. https://github.com/line/line-bot-sdk-go/blob/master/examples/echo_bot/server.go

Deploying to AWS or Heroku is a hassle, so I used ngrok to publish the service from my local device to the outside world. After that, it is not preferable to publish the channel token etc. when publishing the program on GitHub, so I got it from the ini file.

Program

main.go


package main

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

	"github.com/line/line-bot-sdk-go/linebot"
	"gopkg.in/ini.v1"
)

type Config struct {
	channelSecrt string
	channelToken string
}

var Cnf Config

func init() {
	c, _ := ini.Load("config.ini")
	Cnf = Config{
		channelSecrt: c.Section("lineBotApi").Key("secret").String(),
		channelToken: c.Section("lineBotApi").Key("token").String(),
	}
}

func main() {
	http.HandleFunc("/callback", lineHandler)
	log.Fatal(http.ListenAndServe(":8080", nil))
}

func lineHandler(w http.ResponseWriter, r *http.Request) {
	bot, err := linebot.New(
		Cnf.channelSecrt,
		Cnf.channelToken,
	)
	if err != nil {
		log.Fatal(err)
	}
	events, err := bot.ParseRequest(r)
	if err != nil {
		if err == linebot.ErrInvalidSignature {
			w.WriteHeader(400)
		} else {
			w.WriteHeader(500)
		}
		return
	}
	for _, event := range events {
		if event.Type == linebot.EventTypeMessage {
			switch message := event.Message.(type) {
			case *linebot.TextMessage:
				if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage(message.Text)).Do(); err != nil {
					log.Print(err)
				}
			case *linebot.StickerMessage:
				replyMessage := fmt.Sprintf(
					"sticker id is %s, stickerResourceType is %s", message.StickerID, message.StickerResourceType)
				if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage(replyMessage)).Do(); err != nil {
					log.Print(err)
				}
			}
		}
	}
	if err := http.ListenAndServe(":"+os.Getenv("PORT"), nil); err != nil {
		log.Fatal(err)
	}
}

config.ini


[lineBotApi]
secret = <Secret token>
token = <Channel access token>

What is ngrok?

A service that exposes a locally running web service to the outside world. > Ngrok exposes the local server behind NAT and firewalls to the public Internet through a secure tunnel. https://ngrok.com/product

This time, the following command is executed.

ngrok http 8080

The URL in the red frame is registered in the Webhook URL of the LINE BOT setting.

image.png

Impressions

Next, I will try to create a BOT that allows daily conversation using the Talk API. https://a3rt.recruit-tech.co.jp/product/talkAPI/

Recommended Posts

I made a LINE BOT that returns parrots with Go
I made a stamp substitute bot with line
I made a LINE Bot with Serverless Framework!
[AWS] I made a reminder BOT with LINE WORKS
I made a household account book bot with LINE Bot
I made a LINE BOT with Python and Heroku
[AWS] I made a reminder BOT with LINE WORKS (implementation)
[Python] I made a Line bot that randomly asks English words.
I made a Twitter Bot with Go x Qiita API x Lambda
I made a Mattermost bot with Python (+ Flask)
I made a discord bot
I made a Twitter BOT with GAE (python) (with a reference)
I made a vim learning game "PacVim" with Go
I made a Line Bot that uses Python to retrieve unread Gmail emails!
I made a LINE Bot that sends recommended images every day on time
[Python] I made a LINE Bot that detects faces and performs mosaic processing.
In Python, I made a LINE Bot that sends pollen information from location information.
A story that stumbled when I made a chatbot with Transformer
I made a wikipedia gacha bot
I made a fortune with Python.
Until I return something with a line bot in Django!
I made a rigid Pomodoro timer that works with CUI
I made a daemon with Python
I made a plug-in that can "Daruma-san fell" with Minecraft
I made a package that can compare morphological analyzers with Python
I made a Twitter bot that mutters Pokemon caught by #PokemonGO
I made a shuffle that can be reset (reverted) with Python
[LINE Messaging API] Create a BOT that connects with someone with Python
I made a slack bot that notifies me of the temperature
I made a program that automatically calculates the zodiac with tkinter
I made a character counter with Python
I made a Hex map with Python
I made a life game with Numpy
I made a stamp generator with GAN
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a WEB application with Django
I made a neuron simulator with Python
I made a Line bot that guesses the gender and age of a person from an image
I created a Go library nzargv that arranges command line arguments nicely.
I made a plug-in "EZPrinter" that easily outputs map PDF with QGIS.
I made a Discord bot in Python that translates when it reacts
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
I made a tool that makes decompression a little easier with CLI (Python3)
I wrote a Slack bot that notifies delay information with AWS Lambda
I made a module PyNanaco that can charge nanaco credit with python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
I made my dog "Monaka Bot" with LineBot
A memo that made a graph animated with plotly
Create a LINE BOT with Minette for Python
I made a Twitter fujoshi blocker with Python ①
[Python] I made a bot that tells me the current temperature when I enter a place name on LINE
[Python] I made a Youtube Downloader with Tkinter.
I made a simple Bitcoin wallet with pycoin
Serverless LINE bot made with IBM Cloud Functions
Make a LINE WORKS bot with Amazon Lex
I made a random number graph with Numpy
I made a bin picking game with Python
I made a QR code image with CuteR