【GMOリサーチではエンジニアを募集中です】採用情報はこちら!

Write your First Chatbot in Vanilla Ruby and Heroku

Write your First Chatbot in Vanilla Ruby and Heroku

Hi, all! I am Eishia Konno, a developer from GMO-Research System Department.

Today, we will build together a LINE chatbot. I think chatbot’s importance is emerging more than ever, especially nowadays that almost everything is being switched online. Times have come where offline response or even marketing is crucial for every business to thrive in these trying times. Having this in mind, I started a passion project recently with the use of LINE API, Ruby and Heroku. I chose these tools to learn new technologies and to brush up on my Ruby skills. 

Outline

  1. Setup Line Messaging API
  2. Setup Heroku
  3. Write Ruby code
  4. Deploy code with Heroku Service
  5. Add your LINE bot as a friend. Test it. Enjoy! 

Are you ready? Let’s create the chatbot! 

Step 1. LINE Messaging API 

In order to do this, you need to sign up for a LINE Developers account. If you already have a LINE account you can easily just login to this site https://developers.line.biz/en/. The LINE Messaging API is able to do push and receives messages. Once you’ve logged in we’re ready.

Create a provider. 

Once you have created a provider, you are now able to create a dedicated Messaging API channel for your bot. When prompted to choose a channel type, click Create a Messaging API channel. Take note that the Channel Icon and Name are going to appear in your bot’s LINE profile. Make sure it’s what you want as you may not be able to edit it later.

 

After creating the channel, issue Channel Secret token, Assertion Signing token in the Basic Settings tab. After that, move to the Messaging API tab and issue the Channel Access Token. Keep them in a note for easier access as we will use them later.

Step 2: Heroku Web App Setup 

Register with Heroku if you don’t have an account yet. Once able to login, create a new app. Since Heroku is a service to build and run applications on the cloud, we will later deploy our code to Heroku. 

Step 3: Write your code

In the app.rb file:

 

 

For a kickstart, I’ve uploaded a package in this link to help you get started.

Of course, you may also use other programming languages such as PHP and Python. Assuming you’d use the programming language of your choice, I did not include any more the steps on how to set up the Ruby environment. But if you are interested in Ruby, check out Ruby’s official documentation to  help you get started. 

Step 4: Deploy code to Heroku 

Hosting your bot with Heroku is pretty straightforward. A deployment tab which has the steps is readily available in the Heroku site. 

URL: https://dashboard.heroku.com/apps/{HEROKU_APP_NAME}/deploy/heroku-git

Or, you may also just follow the steps here. There are two deployment methods available but we will choose the Heroku CLI method. 

Install the Heroku CLI

Download and install the Heroku CLI.

If you haven’t already, log in to your Heroku account and follow the prompts to create a new SSH public key.

Create a new Git repository

Initialize a git repository in a new or existing directory

Set the tokens saved from Step 2 as environment variables on Heroku.

You may directly input environment variables in your Heroku dashboard.

Deploy your application

Commit your code to the repository and deploy it to Heroku using Git. Every time you make changes just repeat these same steps!

Finally, go back to the LINE Console (Page) and input the webhook URL in the Messaging API tab. The URL must be in the following format:

https://{HEROKU_APP_NAME}.herokuapp.com/callback

Click the Verify button to confirm the webhook works! If it returns SUCCESS then we are good to go.

Step 5: Add your LINE Bot

Finally, add your LINE Bot through the QR code displayed in the Messaging API tab. You may start chatting with your new bot friend. If it sends back a reply, then, congratulations! You’ve just successfully created your simple LINE bot. Continue testing and if you have time, tweak as you like. You may also send stickers and other file types with the API. 

For debugging: https://dashboard.heroku.com/apps/{HEROKU_APP_NAME}/logs

Additional Information:

I also integrated IBM’s Watson Visual Recognition Tool in my personal project. When an image gets sent to the bot, the service tries to identify the image sent. Lastly, I believe not everybody is friends with regex. So I would like to share this site which I think is also useful for testing Ruby regular expressions.

Feel free to add GMOR Bot as your LINE friend! Thank you for reading!

References:

https://developers.line.biz/en/docs/messaging-api/building-bot/

https://developers.line.biz/en/docs/messaging-api/building-sample-bot-with-heroku/

未分類カテゴリの最新記事