質問編集履歴

1

書式の改善

2025/03/04 17:01

投稿

nobinomio
nobinomio

スコア45

test CHANGED
@@ -1 +1 @@
1
- miibo のレスポンスを LINE に返信したい
1
+ LINE にflexmessageを返信したい
test 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
  ・できている事
5
+  AでもBでもどれを選択してもご回答ありがとうございます。登録しましたと出る
6
+ ```GAS
7
+ function handlePostback(e) {
8
+ const postbackData = e.postback.data;
9
+ if (["A", "B", "C", "D", "E", "全部"].includes(postbackData)) {
10
+ const cells = sheet.createTextFinder(e.source.userId).findAll();
11
+ if (cells.length == 0) {
6
- 適当な文字をLINEで入力した場合、miiboは起動する。 miibo側の管理画面よりログを見ると
12
+ sheet.appendRow([e.source.userId, postbackData]);
7
- LINEの入力に反応している。
13
+ } else {
14
+ cells[0].offset(0, 1).setValue(postbackData);
15
+ }
16
+ replyToLine(e.replyToken, { type: "text", text: "ご回答ありがとうございます。登録しました。" });
17
+ }
18
+ }
19
+ ```
8
20
  ・できていない事
9
- miibo側のログで確認できた内容がLINE返信されな
21
+ Aを選択したとは、Aに対する回答、Bを選択し際はB関する回答にした
22
+
10
23
 
11
24
  ### 発生している問題・分からないこと
12
- miibo レスポンスを LINE にかえすGAS codeがわからない
25
+ 分岐のcodeがわからない
13
-
14
26
 
15
27
 
16
28
  ### 該当のソースコード
17
-
18
- ```function callback(e)
29
+ Bを選択の時は違う回答をするcodeを記入した
30
+ ```GAS
19
- function callback(e) {
31
+ 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;
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
+ });
27
- if (["テスト会員", "テスト2会員"].includes(postbackData)) {
44
+ } else if (["A", "C", "D", "E", "全部"].includes(postbackData)) {
28
- const cells = sheet.createTextFinder(e.source.userId).findAll();
45
+ const cells = sheet.createTextFinder(e.source.userId).findAll();
29
- if (cells.length == 0) {
46
+ if (cells.length == 0) {
30
- sheet.appendRow([e.source.userId, postbackData]);
47
+ sheet.appendRow([e.source.userId, postbackData]);
31
- } else {
48
+ } else {
32
- cells[0].offset(0, 1).setValue(postbackData);
49
+ 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
50
  }
51
+ replyToLine(e.replyToken, { type: "text", text: "ご回答ありがとうございます。登録しました。" });
52
+ }
43
53
  }
44
- ```
45
-
46
- ```function callMiiboAPI
47
- function callMiiboAPI(userMessage, userId) {
48
- const apiurl = "https://api-mebo.dev/api"; // miibo API のエンドポイント
49
-
50
- const payload = {
51
- api_key: "******************", // ← ここにリクエストAPIのAPI KEYを設定
52
- agent_id: "**************************", // ← ここにリクエストAPIのエージェントIDを設定
53
- utterance: userMessage,
54
- uid: userId
55
- };
56
-
57
- const options = {
58
- method: "post",
59
- contentType: "application/json",
60
- payload: JSON.stringify(payload)
61
- };
62
-
63
- try {
64
- const response = UrlFetchApp.fetch(apiurl, options);
65
- const jsonResponse = JSON.parse(response.getContentText());
66
- if (jsonResponse.bestResponse) {
67
- return jsonResponse.bestResponse.utterance; // miibo の返答メッセージ
68
- } else {
69
- return "申し訳ありません、理解できませんでした。";
70
- }
71
- } catch (error) {
72
- Logger.log("miibo API エラー: " + error.toString());
73
- return "エラーが発生しました。";
74
- }
75
- }
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
- 特になし