[Rails] Run LINEBot in local environment using ngrok

Overview

Last time, I created an echoBot using LINE's Messaging API. https://qiita.com/shizu9d/items/2bd2618409c9e5c93e0f If you edit the code, it will not be reflected unless you deploy it on Heroku, so You can easily debug by running it in the local environment.

environment

・ Ruby 2.6.5 ・ Rails 6.0.3.2 ・ PostgreSQL ・ Heroku

ngrok Using ngrok makes it easy to expose a locally running web server to the outside world. You will be able to run your bot in your development environment without deploying!

ngrok installation

$ brew install ngrok

If the above does not work, use the cask option.

$ brew cask install ngrok

Run

Specify the port number of the local startup page.

  http://localhost:3000

In such a case

 $ ngrok http 3000

Please command.

image.png If it is displayed like this, it's OK!

Setting environment variables

Set environment variables in the local environment. This time, I would like to use dotenv and manage it in an .env file.

gem dotenv-rails Use the gem'dotev-rails'.

Gemfile


 gem 'dotenv-rails'
 $ bundle install

Create an .env file directly under the root of the application.

 $ touch .env

Set the access token and channel secret in the created .env file.

.env


 LINE_CHANNEL_SECRET=Paste the Channel Secret here
 LINE_CHANNEL_TOKEN=Paste the access token here

Add the .env file to gitignore and set it not to upload.

gitignore


 /.env

Webhook URL settings

image.png

Add / callback to the ngrok URL (second Forwarding) and set it as the Webhook URL.

image.png

Now you are ready to run LINE Bot in your local environment! Thank you to everyone except Rails 6!

Blocked

Rails 6 seems to block servers with IP address notation and servers other than localhost. Therefore, set to allow access of ngrok.

config/environments/development.rb


Rails.application.configure do
~ Omitted ~
 config.hosts << '.ngrok.io'
end

Summary

Thanks to being able to run locally, debugging is easier and you can see how the app works. You can now study efficiently. Currently, I have created a restaurant search bot using the API of Gurunavi.

References

I referred to the following article. https://qiita.com/hirokisoccer/items/7033c1bb9c85bf6789bd https://qiita.com/suketa/items/f00570e6f171cb987ddd

Recommended Posts

[Rails] Run LINEBot in local environment using ngrok
Rails5.1 + puma SSL connection in local production environment
SSL in the local environment of Docker / Rails / puma
Japaneseize using i18n with Rails
[Rails 6] Customize Bootstrap in Rails + Bootstrap 5.0.0-alpha environment
[Environment construction] Uninstall rails from local
ngrok --https communication in local environment (exposing the server to the outside)
After learning Progate, I tried to make an SNS application using Rails in the local environment
Rails API server environment construction using docker-compose
Run Redmine in the local environment of Windows10 Pro-Use Docker Desktop for Windows
[Rails] About local: true described in form_with
Batch implementation in RubyOnRails environment using Digdag
Show Better Errors in Rails + Docker environment
[Rails] Reset the database in the production environment
Let's run batch in container using Azure Batch
Implement button transitions using link_to in Rails
Run static analysis Infer in Windows environment
Implement share button in Rails 6 without using Gem
(Basic authentication) environment variables in rails and Docker
rails tutorial About account activation in production environment
Implement star rating function using Raty in Rails6
[Note] Struts2 environment construction using Gradle in Eclipse
[Rails] Show avatars in posts using Active Storage
500 Internal Server Error occurs in Rails production environment
Allow development in Eclipse environment using iPLAss SDK
Support out of support in docker environment using centos6
Docker × Laravel HTTPS (SSL) communication in local environment
[Rails] How to use PostgreSQL in Vagrant environment
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
Group_by in Rails
Sharing research on ER diagram automation in Rails environment
Migration error after Activerecord association in Rails5 + Docker environment
Regarding overcapacity when setting Rails tutorial environment in Cloud 9
Ruby on Rails environment construction using VirtualBox, Vagrant, cyberduck
Beginners use ubuntu in windows to prepare rails environment
I installed Squid on CentOS in my local environment
[Rails] What to do if you accidentally install bundle in the production environment in your local environment