Hey there, fellow Go enthusiast! Ready to dive into the world of Line API integration? You're in for a treat. We'll be using the awesome line-bot-sdk-go
package to make our lives easier. Buckle up, and let's get coding!
Before we jump in, make sure you've got:
Let's kick things off by creating a new Go project:
mkdir line-bot-project cd line-bot-project go mod init github.com/yourusername/line-bot-project
Now, let's grab that line-bot-sdk-go
package:
go get -u github.com/line/line-bot-sdk-go/v7/linebot
Head over to the Line Developer Console and create a new bot. You'll need to jot down two important pieces of info:
Keep these safe – we'll need them in a bit!
Time to write some Go! Create a main.go
file and let's get that bot client up and running:
package main import ( "log" "os" "github.com/line/line-bot-sdk-go/v7/linebot" ) func main() { bot, err := linebot.New( os.Getenv("CHANNEL_SECRET"), os.Getenv("CHANNEL_TOKEN"), ) if err != nil { log.Fatal(err) } // We'll add more code here soon! }
Pro tip: Use environment variables for your secrets. Your future self will thank you!
Now, let's set up our webhook to handle those incoming events:
http.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) { events, err := bot.ParseRequest(req) if err != nil { if err == linebot.ErrInvalidSignature { w.WriteHeader(400) } else { w.WriteHeader(500) } return } for _, event := range events { // We'll handle events here } }) // Don't forget to start the server! if err := http.ListenAndServe(":8080", nil); err != nil { log.Fatal(err) }
Let's add some smarts to our event handling:
for _, event := range events { switch event.Type { case linebot.EventTypeMessage: switch message := event.Message.(type) { case *linebot.TextMessage: // Handle text messages case *linebot.StickerMessage: // Handle stickers // Add more cases as needed } // Handle other event types } }
Time to make our bot talk back!
// Sending a text reply if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage("Hello, World!")).Do(); err != nil { log.Print(err) } // Sending a button template template := linebot.NewButtonsTemplate( "", "My button sample", "Hello, my button", linebot.NewURIAction("Go to line.me", "https://line.me"), linebot.NewPostbackAction("Say hello1", "hello こんにちは", "", "hello こんにちは"), linebot.NewPostbackAction("言 hello2", "hello こんにちは", "hello こんにちは", ""), linebot.NewMessageAction("Say message", "Rice=米"), ) if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTemplateMessage("Buttons alt text", template)).Do(); err != nil { log.Print(err) }
Always be prepared for the unexpected:
if err != nil { log.Printf("Error occurred: %v", err) // Handle the error appropriately }
Use Line's webhook tester to make sure everything's working smoothly. Once you're confident, take it for a spin in the real Line app. Exciting, right?
Remember, Line requires HTTPS for webhooks in production. Also, keep an eye on your bot's performance as it scales – you might need to optimize as you grow.
And there you have it! You've just built a Line bot in Go. Pretty cool, huh? This is just the beginning – there's so much more you can do with the Line API. Keep exploring, keep coding, and most importantly, have fun with it!
For more in-depth info, check out the line-bot-sdk-go documentation and the Line Messaging API docs.
Now go forth and build some awesome bots! 🚀