This article ** Somehow push to heroku every time I check it! !! ** For those who think.
Before solving the problem, think about why you were using heroku in the first place. LINEbot and heroku were connected via an access token, secret key and ** webhook URL **. "Figure" Once a heroku app is published (deployed), it can have a URL (~ herokuapp.com). This is the one I use as the ** webhook URL **. I wish I could ** prepare this webhook URL by myself **, but I can't ** easily **. So I had no choice but to deploy and use heroku to get the URL of the app (but not only).
** Publish your localhost ** to the outside world ** without using heroku. By doing this, you can prepare a webhook URL. (I'm wondering if it's safe to publish localhost, but I'll leave it.) The tool I use is ** ngrok **.
Download the zip file with the red button on the ngrok official website
Next, use the following command to decompress and check the version.
ngrok installation
cd ~/Downloads
unzip ngrok-stable-linux-amd64.zip
sudo cp ngrok /usr/bin/
ngrok version
You can now use ngrok. When actually using
Published externally on ngrok
ngrok http [port number]
The [port number] of the local server is exposed to the outside.
ngrok execution result
ngrok by @inconshreveable (Ctrl+C to quit)
Session Status online
Session Expires 7 hours, 59 minutes
Version 2.3.35
Region United States (us)
Web Interface http://127.0.0.1:☓☓☓☓
Forwarding http://☓☓☓☓☓☓☓☓.ngrok.io -> http://localhost:5000
Forwarding https://☓☓☓☓☓☓☓☓.ngrok.io -> http://localhost:5000
Connections
ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
The URL when https: //☓☓☓☓☓☓☓☓.ngrok.io
next to Forwarding
exposes localhost to the outside (port number is 5000). Use this as a ** webhook URL! ** **
** Preparation for LINEbot ** There are many articles so please try google
** Environment variable settings ** Set environment variables. Create a file called .env under the project and copy and paste the usual two from LINE developers (without adding'or'or anything, just as it is).
.env is a file that sets environment variables, so don't push it to GitHub. To avoid pushing, find out how to use the .gitignore file.
.env
LINE_CHANNEL_ACCESS_TOKEN = ☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓
LINE_CHANNEL_SECRET = ☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓
・ ** Run main.py ** Prepare two terminals and run ngrok again on one. (Ngrok associates a different URL each time it is executed, so the value should have changed earlier). With that running, LINE developers webhook URL
https://☓☓☓☓☓☓☓☓.ngrok.io/callback
Set to (https!). Finally, run main.py in another terminal as follows.
main.Run py
pipenv shell //In a virtual environment
pipenv run python3.8 main.py
Hopefully the sample will work.
** In case of error **
Error: the command python3.8 could not be found within PATH or Pipfile's [scripts].
If you get an error like the one above,
Change python3.8
to one that suits your environment.
main.Run py
pipenv run python main.py
For example, python
or python3
.
I was able to check the operation locally without pushing to heroku every time. Edit main.py ⇒ Execute ⇒ Just edit, so development is smooth.
However, ** ngrok changes the URL every time it is executed **, so you have to change the webhook URL of LINE developers every time you stop ngrok. Also, I feel that exposing localhost to the outside ** is bad for security ** (I don't know the details).
that's all!
It seems that ngrok is safe or dangerous, free or paid, and so on. Verify actions around webhook on localhost ngrok is too convenient Use ngrok as safely as possible (Basic authentication)
Recommended Posts