質問するログイン新規登録

質問編集履歴

1

ご回答を頂き試したコードを追加

2020/08/21 04:52

投稿

true_colors_20
true_colors_20

スコア25

title CHANGED
File without changes
body CHANGED
@@ -95,4 +95,99 @@
95
95
  app.listen(port, () => {
96
96
  console.log(`listening on ${port}`);
97
97
  });
98
+ ```
99
+ ↓ 追記:20200821 13:48
100
+
101
+ NovisHub様からご回答を頂き、試してみたコードです。
102
+ こちらのコードでもメッセージが送信されませんでした。
103
+
104
+ また、現在herokuで動かしており、
105
+ `コードを修正 → デプロイ → LINEでメッセージを送って動作確認`
106
+ のような感じで作業しているのですが、エラーメッセージを見るなど良いデバッグ方法はありませんでしょうか?
107
+
108
+ こちらがサイト側のURLです。
109
+ [https://line-recipe.herokuapp.com/](https://line-recipe.herokuapp.com/)
110
+
111
+
112
+ ### ご回答を頂いて試してみたコード
113
+ ```
114
+ 'use strict';
115
+
116
+ const line = require('@line/bot-sdk');
117
+ const express = require('express');
118
+ const axios = require('axios');
119
+
120
+ // create LINE SDK config from env variables
121
+ const config = {
122
+ channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN,
123
+ channelSecret: process.env.CHANNEL_SECRET,
124
+ };
125
+
126
+ // create LINE SDK client
127
+ const client = new line.Client(config);
128
+
129
+ // create Express app
130
+ // about Express itself: https://expressjs.com/
131
+ const app = express();
132
+
133
+ // register a webhook handler with middleware
134
+ // about the middleware, please refer to doc
135
+ app.post('/callback', line.middleware(config), (req, res) => {
136
+ Promise
137
+ .all(req.body.events.map(handleEvent))
138
+ .then((result) => res.json(result))
139
+ .catch((err) => {
140
+ console.error(err);
141
+ res.status(500).end();
142
+ });
143
+ });
144
+
145
+ // event handler
146
+ function handleEvent(event) {
147
+ if (event.replyToken === '00000000000000000000000000000000') {
148
+ return {}
149
+ }
150
+ if (event.type !== 'message' || event.message.type !== 'text') {
151
+ // ignore non-text-message event
152
+ return Promise.resolve(null);
153
+ }
154
+
155
+ // RAKUTEN Recipe API
156
+ const appId = process.env.RAKUTEN_RECIPE_API_APP_ID
157
+ const catId = event.message.text
158
+ const request = `https://app.rakuten.co.jp/services/api/Recipe/CategoryRanking/20170426?applicationId=${appId}&formatVersion=2&categoryId=${catId}`
159
+
160
+ return new Promise(function (resolve, reject) {
161
+ fetch(request, {
162
+ method: "GET",
163
+ })
164
+ .then(response => response.text())
165
+ .then(data => {
166
+ console.log(data);
167
+ const echo = [{
168
+ type: 'text',
169
+ text: event.message.text
170
+ },
171
+ {
172
+ type: 'text',
173
+ text: data
174
+ }
175
+ ]
176
+ client.replyMessage(event.replyToken, echo).then(function (messageAPIResponseBase) {
177
+ resolve();
178
+ }).catch(function (error) {
179
+ reject(error);
180
+ });
181
+ });
182
+ });
183
+
184
+ // use reply API
185
+ // return client.replyMessage(event.replyToken, echo);
186
+ }
187
+
188
+ // listen on port
189
+ const port = process.env.PORT || 3000;
190
+ app.listen(port, () => {
191
+ console.log(`listening on ${port}`);
192
+ });
98
193
  ```