LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

LINE BOT 開発 ホスティング編

Posted at

5. ホスティングする

Herokuに作成したBotアプリケーションをホスティングしましょう。
アカウントを持っていない人はアカウント作成をして下さい。

以下からダッシュボードに移動します。

New > Create new appを選択します。

任意のApp Nameを入力して 「Create App」を選択します。
ちなみにアンダースコア(_)は使えないようです。

アプリケーションが作成されるとアプリケーションの管理画面が表示されます。

今回はn0bisuke-gs-lineというアプリケーションにしました。

ファイルの追加とソースコードの修正

プロジェクト内で.gitignoreという名前のファイルを作成し、以下を記述します。

node_modules

また、Procfileという名前のファイルを作成し、以下を記述します。

web: node app.js

app.jsconst PORT = 3000;を以下に変更します。

const PORT = process.env.PORT || 3000;

参考までに、この時点でのファイル構成は以下のようになっています。

$ ls -a
.            .gitignore   node_modules
..           Procfile     package.json
.git         app.js       tweet.js

Herokuコマンドでデプロイしてみよう

こちらからHerokuコマンドを取得します。

$ brew install heroku

brewコマンドが使えない場合(command not found: brewとか出る場合)は参考記事からインストールして下さい。

あとはダッシュボードの記述通りに進めます。

$ heroku login

メールアドレスとパスワードを求められるので、Herokuにユーザー登録した際の値を入力しましょう。
パスワード入力の際には表示はされません。

Enter your Heroku credentials.
Email: (メールアドレスを入力)
Password (typing will be hidden): (パスワードを入力)
Logged in as xxxxxxxx@xxx.xxx

作成しているBotアプリケーションのフォルダに移動しておいて下さい。
gitリポジトリを登録します。

$ git init
$ heroku git:remote -a n0bisuke-gs-line (Herokuのアプリケーション名)

デプロイ実行

$ git add .
$ git commit -am "make it better"
$ git push heroku master
Counting objects: 727, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (663/663), done.
Writing objects: 100% (727/727), 855.00 KiB | 0 bytes/s, done.
Total 727 (delta 143), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:

省略...

remote: Verifying deploy.... done.
To https://git.heroku.com/n0bisuke-gs-line.git
 * [new branch]      master -> master

これでhttps://Herokuのアプリケーション名.herokuapp.com/にアクセスするとこんにちはと表示され、デプロイが完了です。

https://n0bisuke-gs-line.herokuapp.com/

最後に LINE developersの画面でWebhook URLを先ほどngrokのアドレスにしていましたが、Herokuのアドレスに差し替えましょう。

VERIFYを押して、 Successと表示されれば問題なく動作しています。

ここまでで、作成したアプリケーションのホスティングが完了です。
手元のNode.jsプロセスを終了してもBotは稼働してくれます。

付録: SSH Keyの設定

アプリケーションをデプロイするにあたりPCのSSH KeyをHerokuに登録する必要が(たしか)あります。

まずは.sshフォルダを作ります。既にある人は大丈夫です。
ls ~/.sshでファイルが表示される人はここはスキップして下さい。

$ cd
$ ls .ssh
ls: .ssh: No such file or directory
$ mkdir .ssh

.sshフォルダに移動して、ssh-keygenコマンドを実行します。

$ cd .ssh
$ ssh-keygen
ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/nobisuke/.ssh/id_rsa): (エンターキー入力)
Enter passphrase (empty for no passphrase): (エンターキー入力)
Enter same passphrase again: (エンターキー入力)
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
...
The key's randomart image is:
+--[ RSA 2048]----+
|  .o     ..      |
|  . + o  ..      |
| . . B o .+      |
|. . * .oo= o     |
| o o   .S o      |
|E        +       |
|.       .        |
|                 |
|                 |
+-----------------+

全てエンターで進めます。
id_rsaid_rsa.pubが作成されます。

$ ls
id_rsa id_rsa.pub

id_rsa.pubの中身を確認します。

$ more id_rsa.pub

長い文字列が表示されます。
この文字列をHerokuの設定画面で登録しましょう。

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up