Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8936

【個人メモ】Node.js LINEBOT雛形

$
0
0

プロジェクト作成とハローワールド

$mkdir linebot
$cd linebot 
$npm init -y
$npm i @line/bot-sdk express 
$npm install dotenv --save
$touch index.js $$ .env
$code .
index.js
'use strict';constexpress=require('express');constline=require('@line/bot-sdk');constPORT=process.env.PORT||3000;constdotenv=require('dotenv')dotenv.config()constconfig={channelAccessToken:process.env.CHANNEL_ACCESS_TOKEN,channelSecret:process.env.CHANNEL_SECRET};constapp=express();app.get('/',(req,res)=>res.send('Hello(GET)'));app.post('/webhook',line.middleware(config),(req,res)=>{console.log(req.body.events);if(req.body.events[0].replyToken==='00000000000000000000000000000000'&&req.body.events[1].replyToken==='ffffffffffffffffffffffffffffffff'){res.send('Hello!(POST)');console.log('疎通確認用');return;}Promise.all(req.body.events.map(handleEvent)).then((result)=>res.json(result));});constclient=newline.Client(config);functionhandleEvent(event){if(event.type!=='message'||event.message.type!=='text'){returnPromise.resolve(null);}returnclient.replyMessage(event.replyToken,{type:'text',text:event.message.text});}app.listen(PORT);console.log(`Server running at ${PORT}`);
.env.sample
CHANNEL_ACCESS_TOKEN = "xxxxxxxxxxxxxxxxxxx"
CHANNEL_SECRET = "xxxxxxxxxxxxxx"
BASE_URL = 'xxxxxxxxxxxxx.ngrok.io'
$ngrok http 3000

BASE_URLの書き換え

$node index.js

webhookURLの設定

https://xxxxxxxxxxxxxxxxxx.ngrok.io/webhook


Viewing all articles
Browse latest Browse all 8936

Trending Articles