DEV Community

Evan Lin
Evan Lin

Posted on • Originally published at evanlin.com on

[Conference Notes] [DSC] Building a LINE Chatbot with Go, 20200507

Preface

Hello everyone, I am Evan Lin, a Technology Evangelist at LINE Taiwan. Thanks to the invitation from DSC (Google Developer Student Clubs), I gave a basic course to the students online on 2020/05/07. The content will include a basic introduction to Golang, and through building a simple LINE Bot, I will demonstrate some of the advantages of Golang to you.

This time, there are DSC club students from four universities (National Chung Hsing University, National Taipei University, Taipei University of Technology, and Ming Chuan University). The entire course is also open for live streaming, hoping to provide a good teaching material for beginners who are interested in LINE Bot.

Build LINE Bot with Go / Senior Development Technology Promotion Engineer Evan Lin

Slides

The slides for this sharing are divided into three main parts:

Why did I learn Golang? What is Golang?

After ten years of working in a multinational multimedia software company, I tried many programming languages ​​in order to learn new web programming languages, and finally chose Golang to study. And in order to learn Golang in depth, I launched Project 52 as a way to learn Golang, and thus learned a lot of algorithms and advanced data format applications.

Here are also some basic advantages of Golang:

  • Gofmt / goimport: Helps you keep your code style consistent and avoid missing some packages that need to be used.
  • Go test: Helps you quickly prepare for testing, and writing test code is no longer difficult. And you can do performance tuning work very quickly.
  • GoDoc: Documenting has always been a headache for developers, not because developers are unwilling to write. Often, the output to the document requires some related conversions, or requires a lot of extra data in the code. Using GoDoc, as long as the developer's code is written, it can be found directly on the Internet (for example: https://pkg.go.dev/github.com/line/line-bot-sdk-go/linebot is a website that directly converts https://github.com/line/line-bot-sdk-go/linebot into documents.
  • GoRoutine: Doing multiple routine programming is actually difficult and cumbersome in many languages. In addition to the multi-threaded model itself, there are many things that need to be paid attention to, and the code required to build such a thing is actually very complex. Using goroutine is quite lightweight and quite easy. Developers can put all their energy into how to handle multi-threaded related controls.

How to learn Golang:

Since the main audience this time are still university students (some are freshmen), many of the operational examples are directly operated in Golang Playground. This can save a lot of problems with environment setup that beginners often encounter.

At the same time, it is also recommended that beginners can share the problematic code through Golang Playground and share it on various social media to ask questions. This way, it is also easy to spot and solve problems.

At the same time, it is also recommended that students can learn the relevant knowledge step by step through Golang Tour. Each page has code that can be used, and can be edited and executed online. You can also deepen your impression by practicing and understanding at the same time.

What is LINE Bot? How to quickly build a LINE Bot with Golang?

The best way to learn a language is to immediately build something with it, whether it's a product, a service, or even a small tool. Here, it is highly recommended that you use building a LINE Bot as a way to learn Golang. The main reasons are as follows:

  • Building a LINE Bot does not require advanced artistic concepts, for those who have difficulty with some UI aesthetics. Learning LINE Bot can not only get started faster, but also allow developers to focus more on the functions of the program itself.
  • Using the correct platform and message sending method, there is no cost to develop a LINE Bot. In the example in the video, Heroku is used as the server platform. The LINE Bot built only replies to the user's questions and does not require any additional fees.
  • LINE is a very good tool (or service) publishing platform. If developers develop very useful tools, they can quickly recommend them to friends and family through LINE. And it does not require the cumbersome review process similar to the App Store, and the publishing process is also very fast.

Next, start explaining how to quickly build a LINE Bot:

Sample code: https://github.com/kkdai/LineBotTemplate

1. First, apply for a bot account (LINE Bot) on the LINE official website

img

  • Please confirm that the account is enabled in the LINE Developer Console
  • Then create a Messaging API Channel
  • On the "Basic Setting" page, get the Channel Secret
  • On the "Messaging API" page, apply for a Channel Access Token
  • On the "Basic Setting" page, open the LINE official account management interface
  • In the reply settings option, select to enable "webhook"

2. Deploy LINE Bot template

Remember to go to https://github.com/kkdai/LineBotTemplate and click the Deploy button below to deploy the basic code to your heroku.

Deploy]

  • Enter the Channel Access Token and Channel Secret you just obtained.
  • Please remember the Heroku App ID you set, you will use it later.

3. Go back to the LINE Bot Dashboard to set up basic information

Go to your "Basic account information" to set up, the following information needs to be filled in:

  • Callback URL: https://{YOUR_HEROKU_SERVER_ID}.herokuapp.com/callback

Okay... Add your bot. Start talking to him.

This code is the simplest example, after setting it up, it will only repeat the text you typed. More features will be placed in another one.

Video tutorial

You can follow the tutorial in the following video to see how to deploy your own LINE Bot in five minutes

Want to modify the code? Refer to the following video tutorial

Any questions? Welcome to open an issue on github to ask.

How to become an expert in Golang?

Finally, because of sharing Golang and Project 52, I have participated in many community activities and seminars, and became one of the organizers of Golang Taipei. Through continuous sharing and community management, I was fortunate enough to work at LINE as a Developer Relations and became a Go GDE. Here, I also share some quick tips for learning Golang.

Cultivate the habit of technical writing:

Since the main audience are students, I also highly encourage students to learn a new technology or programming language. Through writing, you can quickly reflect on and learn knowledge, and you will want to understand it more deeply. For example, learn a new language feature, try to write it as a TIL (Today I Learn). In this way, not only can you deepen your impression, but also through online search, you can help many people with the same problems (or even yourself).

Use Golang to build anything

The best way to learn a (programming) language is to use it. Therefore, I also highly recommend that students can actually get started and build any small tools. Whether it's a LINE Bot or some daily small tools. By building it by hand, you can understand the mysteries between each language, and you can know some things that need attention.

Share! Share! Remember to share!!!

People often ask me why I can often share in various occasions. I also honestly tell you that I actually learn more in the process of sharing:

  • Because I need to share, I will turn what I understand into slides and scripts.
  • Because I need to share, I need to rethink how to make people who don't understand understand my questions and solutions.
  • Because I need to share, I find that I understand more deeply and it is easier to remember during the oral communication.

So I also suggest that every student must seize the opportunity to share.

Related links:

Activity Summary

I am very happy to share the combination of Golang and LINE Bot with the students. I remember that the last time I talked about the related introduction content was four years ago. So many of the materials and source code need to be updated. And I have also prepared some new video tutorials, and I really hope that friends who want to learn how to build LINE Bot can get started faster, and I also hope that if you have any questions, you can ask in the LINE official discussion forum or the "LINE Developer Official Community" official account.

Join the "LINE Developer Official Community" official account immediately, and you can receive the first-hand Meetup activities, or the latest news push notifications related to the developer program. ▼

"LINE Developer Official Community" Official Account ID: @line_tw_dev

About "LINE Developer Community Program"

LINE launched the "LINE Developer Community Program" in Taiwan at the beginning of this year, and will invest long-term manpower and resources to hold developer community gatherings, job fairs, developer conferences, etc., both internally and externally, online and offline, in Taiwan, and has already held more than 30 events. Readers are welcome to continue to check back for the latest updates. For details, please see:

Top comments (0)