LoginSignup
1
4

More than 5 years have passed since last update.

LINE BOTを作ってみる

Last updated at Posted at 2018-02-18

今回参考にしたページ

1時間でLINE BOTを作るハンズオン (資料+レポート) in Node学園祭2017 #nodefest
https://qiita.com/n0bisuke/items/ceaa09ef8898bee8369d


現場でNode.jsの話題が上がり、自分も勉強したいなと思って探したらとてもお手軽そうなハンズオンがあったのでやってみた。
開発環境はcloud9。

1. LINEのBOTアカウント作成と設定

ここは省略

2. Node.jsのインストール~プロジェクト作成まで

cloud9なのでインストールはされているだろうと思い、バージョン確認から。

 $ node -v
v4.7.3

npm init -y でpackage.jsonが作成されるようだ。

$ npm init -y
Wrote to ******/*****/******/package.json:

{
  "name": "line",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}


$ ls
package.json

続いてパッケージのインストール

$ npm i --save @line/bot-sdk express
npm WARN package.json line@1.0.0 No description
npm WARN package.json line@1.0.0 No repository field.
npm WARN package.json line@1.0.0 No README data
express@4.16.2 node_modules/express
├── escape-html@1.0.3
├── array-flatten@1.1.1
├── setprototypeof@1.1.0
├── cookie-signature@1.0.6
├── utils-merge@1.0.1
├── content-type@1.0.4
├── merge-descriptors@1.0.1
├── methods@1.1.2
├── encodeurl@1.0.2
├── content-disposition@0.5.2
├── cookie@0.3.1
├── fresh@0.5.2
├── range-parser@1.2.0
├── parseurl@1.3.2
├── etag@1.8.1
├── vary@1.1.2
├── serve-static@1.13.1
├── path-to-regexp@0.1.7
├── statuses@1.3.1
├── safe-buffer@5.1.1
├── depd@1.1.2
├── qs@6.5.1
├── on-finished@2.3.0 (ee-first@1.1.1)
├── finalhandler@1.1.0 (unpipe@1.0.0)
├── debug@2.6.9 (ms@2.0.0)
├── proxy-addr@2.0.2 (forwarded@0.1.2, ipaddr.js@1.5.2)
├── type-is@1.6.16 (media-typer@0.3.0, mime-types@2.1.18)
├── accepts@1.3.4 (negotiator@0.6.1, mime-types@2.1.18)
├── send@0.16.1 (ms@2.0.0, destroy@1.0.4, mime@1.4.1, http-errors@1.6.2)
└── body-parser@1.18.2 (bytes@3.0.0, raw-body@2.3.2, http-errors@1.6.2, iconv-lite@0.4.19)

@line/bot-sdk@5.2.0 node_modules/@line/bot-sdk
├── file-type@7.6.0
├── @types/file-type@5.2.1
├── @types/node@7.0.55
├── body-parser@1.18.2 (content-type@1.0.4, bytes@3.0.0, depd@1.1.2, on-finished@2.3.0, qs@6.5.1, raw-body@2.3.2, http-errors@1.6.2, debug@2.6.9, iconv-lite@0.4.19, type-is@1.6.16)
├── axios@0.16.2 (is-buffer@1.1.6, follow-redirects@1.4.1)
└── @types/body-parser@1.16.8 (@types/express@4.11.1)

$ ls
node_modules/  package.json

インストールできた模様。
続いて server.js の作成

$ touch server.js

$ ls
node_modules/  package.json  server.js

中身を編集し(Channel SecretとAccess Tokenを追記し)、起動

$ node server.js 
Server running at 8080

続いてnowのインストールを実施

$ npm i -g now
/home/ubuntu/.nvm/versions/node/v4.7.3/bin/now -> /home/ubuntu/.nvm/versions/node/v4.7.3/lib/node_modules/now/download/dist/now


> now@9.3.2 postinstall /home/ubuntu/.nvm/versions/node/v4.7.3/lib/node_modules/now
> node download/install.js

> For the source code, check out: https://github.com/zeit/now-cli

> Downloading Now CLI 9.3.2 [====================] 100%

now@9.3.2 /home/ubuntu/.nvm/versions/node/v4.7.3/lib/node_modules/now

で、

  let mes = ''
  if(event.message.text === 'ほげ'){
    mes = 'ほげほげほげ';
  }else{
    mes = event.message.text;
  }

中身を適当に書き換えて、 now でデプロイして試してみたら成功。

わざわざnowへデプロイせずにcloud9上でデバッグしたいがちょっと調べてる時間がなかったので今回はこの辺で。

1
4
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
1
4