@ibarakitarou123456

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

A3RTでチャットボットを作成しようとしています!!

Q&A

Closed

A3RTというAI系APIとLINEのMessageAPIを使って、会話をしてくれるChat botを作ろうとしているが返信が返ってこずエラーが表示されます。

エラー: Exception: Request failed for https://api.a3rt.recruit.co.jp returned code 500. Truncated server response: {"status": 1500, "message": "server error"} (use muteHttpExceptions option to examine full response)

LINEの返信がエラーが発生しました。!と表示されます

該当するソースコード


// LINE developersのメッセージ送受信設定に記載のアクセストークン
var ACCESS_TOKEN = '****************';

function doPost(e) {
  // WebHookで受信した応答用Token
  var replyToken = JSON.parse(e.postData.contents).events[0].replyToken;
  // ユーザーのメッセージを取得
  var userMessage = JSON.parse(e.postData.contents).events[0].message.text;
  
  // AIによる応答を取得
  var resMessage = AI_bot(userMessage);
  
  // LINEに応答を送信
  sendLineReply(replyToken, resMessage);

  return ContentService.createTextOutput(JSON.stringify({'content': 'post ok'})).setMimeType(ContentService.MimeType.JSON);
}

function AI_bot(lineMessage) {
  // A3RTのTalk APIのエンドポイントとAPIキー
  var apiUrl = 'https://api.a3rt.recruit.co.jp/talk/v1/smalltalk';
  var apikey = PropertiesService.getScriptProperties().getProperty('***************');

  // リクエストパラメータの構築
  var payload = {
    "apikey": apikey,
    "query": lineMessage
  };
  var headers = {
    "Content-Type": "application/json", // コンテンツタイプをJSONに設定
  };
  // A3RT APIへのリクエスト
  var options = {
    "method": "POST",
    "headers": headers, // ヘッダーを設定
    "payload": JSON.stringify(payload)
  };

  try {
    var response = UrlFetchApp.fetch(apiUrl, options);

    if (response.getResponseCode() === 200) {
      var res = response.getContentText();
      var obj = JSON.parse(res);
      return obj.results[0].reply;
    } else {
      console.error("A3RT APIエラー:", response.getResponseCode());
      return "A3RT APIエラーが発生しました。";
    }
  } catch (e) {
    console.error("エラー:", e.toString());
    return "エラーが発生しました。";
  }
}

function sendLineReply(replyToken, message) {
  // LINEに応答を送信するAPI URL
  var url = 'https://api.line.me/v2/bot/message/reply';
  
  // 応答メッセージの構築
  var replyPayload = {
    'replyToken': replyToken,
    'messages': [{
      'type': 'text',
      'text': message + '!'
    }]
  };

  // LINEに応答を送信
  UrlFetchApp.fetch(url, {
    'headers': {
      'Content-Type': 'application/json; charset=UTF-8',
      'Authorization': 'Bearer ' + ACCESS_TOKEN,
    },
    'method': 'post',
    'payload': JSON.stringify(replyPayload),
  });
}




0 likes

1Answer

use muteHttpExceptions option to examine full response

エラーにmuteHttpExceptionsオプションを使ってすべてのレスポンスを受け取りましょうとあるので、まずはオプションを設定してより詳細なエラーを調べてみましょう。

以下のような変更で見れると思います。

// A3RT APIへのリクエスト
  var options = {
    "method": "POST",
    "headers": headers, // ヘッダーを設定
    "payload": JSON.stringify(payload)
    "muteHttpExceptions": true // この行を追加
  };

~~~
var response = UrlFetchApp.fetch(apiUrl, options);
console.log(response.getContentText()); // この行を追加
1Like

Comments

  1. 返信ありがとうございます。
    以下のエラーが表示されました。
    情報 {"status": 1500, "message": "server error"}
    1エラー A3RT APIエラー: 500

  2. なるほど、変わらずですか。
    実施ありがとうございます。

    このエラーですが、サーバー側で処理しているときに起こるエラーです。
    APIを提供しているリクルート側の問題かと思いますので下記リンクから個人お問い合わせしてみるしかないと思います。
    https://a3rt.recruit.co.jp/contact/individual/

  3. ありがとうございます!!
    問い合わせしてみます!

Your answer might help someone💌