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

質問編集履歴

1

書式の改善

2025/03/04 17:01

投稿

nobinomio
nobinomio

スコア45

title CHANGED
@@ -1,1 +1,1 @@
1
- miibo のレスポンスを LINE に返信したい
1
+ LINE にflexmessageを返信したい
body CHANGED
@@ -1,79 +1,56 @@
1
1
  ### 実現したいこと
2
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2025-03-04/44516251-d914-4266-8ee3-e354a804b24e.png)
3
2
 
4
- LINEに”お友だち登録ありがとうございます!”以外の文字を入力した場合にmiibo動し、LINEへ返信したい
3
+ LINEに”お友だち登録ありがとうございます!”の文字を入力した場合にGAS動し、LINEへflexmessageが出る。flexmessageのアンケートに答えると選択したものによって返答を変えた
5
4
  ・できている事
6
- 適当な文字をLINEで入力した場合、miiboは起動する。 miibo側の管理画面よりログを見ると
7
- LINEの入力に反応している。
8
- ・できていない事
9
- miibo側のログで確認できた内容がLINEに返信されない。
10
-
11
- ### 発生している問題・分からないこと
12
- miibo のレスポンス LINE にかえすGAS codeわからない。
5
+  AでもBでもどれ選択してもご回答ありとうござます登録しましたと出る
13
-
14
-
15
-
16
- ### 該当のソースコード
6
+ ```GAS
17
-
18
- ```function callback(e)
19
- function callback(e) {
7
+ function handlePostback(e) {
20
- if (bot.bool(e, "message", "text","友だち登録ありがとうございます!")) {
21
- // ユーザーがメッセージ送ってきた時、応答メッセージでアンケートを送る
22
- question(e);
23
- } else if (e.type == "follow") {
24
- question(e);
25
- } else if (e.type == "postback") {
26
- const postbackData = e.postback.data;
8
+ const postbackData = e.postback.data;
27
- if (["テスト会員", "テスト2会員"].includes(postbackData)) {
9
+ if (["A", "B", "C", "D", "E", "全部"].includes(postbackData)) {
28
- const cells = sheet.createTextFinder(e.source.userId).findAll();
10
+ const cells = sheet.createTextFinder(e.source.userId).findAll();
29
- if (cells.length == 0) {
11
+ if (cells.length == 0) {
30
- sheet.appendRow([e.source.userId, postbackData]);
12
+ sheet.appendRow([e.source.userId, postbackData]);
31
- } else {
13
+ } else {
32
- cells[0].offset(0, 1).setValue(postbackData);
14
+ cells[0].offset(0, 1).setValue(postbackData);
33
- }
34
- else if (e.type === "message" && e.message.type === "text") {
35
- // miibo API へのリクエスト処理
36
- const userMessage = e.message.text;
37
- const userId = e.source.userId;
38
- // miibo API を呼び出してレスポンスを取得
39
- const replyMessage = callMiiboAPI(userMessage, userId);
40
- // miibo のレスポンスを LINE に返信
41
- bot.replyMessage(e, bot.textMessage(replyMessage));
42
15
  }
16
+ replyToLine(e.replyToken, { type: "text", text: "ご回答ありがとうございます。登録しました。" });
17
+ }
43
18
  }
44
19
  ```
20
+ ・できていない事
21
+ Aを選択したときは、Aに対する回答、Bを選択した際はBに関する回答にしたい
45
22
 
46
- ```function callMiiboAPI
47
- function callMiiboAPI(userMessage, userId) {
48
- const apiurl = "https://api-mebo.dev/api"; // miibo API のエンドポイント
49
23
 
50
- const payload = {
51
- api_key: "******************", // ← ここにリクエストAPIのAPI KEYを設定
52
- agent_id: "**************************", // ← ここにリクエストAPIのエージェントIDを設定
53
- utterance: userMessage,
24
+ ### 発生している問題・分からないこと
54
- uid: userId
25
+ 分岐のcodeがわからない
55
- };
56
26
 
57
- const options = {
58
- method: "post",
59
- contentType: "application/json",
60
- payload: JSON.stringify(payload)
61
- };
62
27
 
28
+ ### 該当のソースコード
29
+ Bを選択の時は違う回答をするcodeを記入した
63
- try {
30
+ ```GAS
31
+ function handlePostback(e) {
64
- const response = UrlFetchApp.fetch(apiurl, options);
32
+ const postbackData = e.postback.data;
33
+ if (postbackData === "B") {
34
+ replyToLine(e.replyToken, {
35
+ type: "text",
36
+ text: "ご回答ありがとうございます。\n Bで登録しました。\nこれからBのお知らせを配信しますね。",
37
+ emojis: [
38
+ { index: 15, productId: "5ac1bfd5040ab15980c9b435", emojiId: "038" },
39
+ { index: 27, productId: "5ac1bfd5040ab15980c9b435", emojiId: "044" },
40
+ { index: 46, productId: "5ac1bfd5040ab15980c9b435", emojiId: "047" },
41
+ { index: 58, productId: "5ac1bfd5040ab15980c9b435", emojiId: "056" }
42
+ ]
43
+ });
44
+ } else if (["A", "C", "D", "E", "全部"].includes(postbackData)) {
65
- const jsonResponse = JSON.parse(response.getContentText());
45
+ const cells = sheet.createTextFinder(e.source.userId).findAll();
66
- if (jsonResponse.bestResponse) {
46
+ if (cells.length == 0) {
67
- return jsonResponse.bestResponse.utterance; // miibo の返答メッセージ
47
+ sheet.appendRow([e.source.userId, postbackData]);
68
- } else {
48
+ } else {
69
- return "申し訳ありません、理解できませんでした。";
70
- }
71
- } catch (error) {
72
- Logger.log("miibo API エラー: " + error.toString());
49
+ cells[0].offset(0, 1).setValue(postbackData);
73
- return "エラーが発生しました。";
74
50
  }
51
+ replyToLine(e.replyToken, { type: "text", text: "ご回答ありがとうございます。登録しました。" });
52
+ }
75
53
  }
76
-
77
54
  ```
78
55
 
79
56
  ### 試したこと・調べたこと
@@ -83,15 +60,4 @@
83
60
  - [ ] その他
84
61
 
85
62
  ##### 上記の詳細・結果
86
- 下記codeを
87
- // miibo のレスポンスを LINE に返信
88
- ```google App Script
89
- bot.replyMessage(e, bot.textMessage(replyMessage));
90
- ```
91
- 下記codeに変更してみましたが、何も変わらない状況です。
92
- ```google App Script
93
- bot.replyMessage(e.replyToken, bot.textMessage("テストメッセージ"));
63
+ codeをBを選択した場合のみ違う回答が出るcodeを組んだが、結果Bを選択した場合のみ回答が出なくなった他、AやCを選択した場合は従来通りのご回答ありがとうございます。登録しました。の文字が出る
94
- ```
95
-
96
- ### 補足
97
- 特になし