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.
・ 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!
$ brew install ngrok
If the above does not work, use the cask option.
$ brew cask install ngrok
Specify the port number of the local startup page.
http://localhost:3000
In such a case
$ ngrok http 3000
Please command.
If it is displayed like this, it's OK!
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
Add / callback to the ngrok URL (second Forwarding) and set it as the Webhook URL.
Now you are ready to run LINE Bot in your local environment! Thank you to everyone except Rails 6!
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
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.
I referred to the following article. https://qiita.com/hirokisoccer/items/7033c1bb9c85bf6789bd https://qiita.com/suketa/items/f00570e6f171cb987ddd
Recommended Posts