1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Chatwork】自分宛てのメッセージ(返信含む)と全員宛てのメッセージを別のルームに転送する

Posted at
const pData = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("必要事項入力欄"); 
const pToken = pData.getRange("B2").getValue();//APIトークン
const roomID = pData.getRange("B3").getValue();//このルームでの自分宛てのメッセージを一覧化する
const accountID = pData.getRange("B4").getValue();//自分のアカウントID 環境設定から確認できる
const mychatID = pData.getRange("B5").getValue();//自分だけのルームID

function getMessages(){

  var params = {
    headers : {"X-ChatWorkToken" : pToken }, //チャットワークAPIトークン
    method : "get"
  };
  
  var url = "https://api.chatwork.com/v2/rooms/" + roomID + "/messages?force=1"; //指定のグループチャットからメッセージを取得
  
  
  try {
    var respons = UrlFetchApp.fetch(url, params).getContentText(); //チャットワークAPIエンドポイントからレスポンスを取得
    var json = JSON.parse(respons); //文字列をJSON形式として解析しJSONオブジェクトとして返


    json.forEach (function(obj) {
      
    var message = obj.body;
    var fromAccountId = obj.account.account_id;
    var messageId = obj.message_id;
    var sendTime = obj.send_time;
    var toaccount = getTOAccount(obj,accountID).flat();
    

    if(toaccount.includes(String(accountID))){

       // 情報を整形して投稿する
      var client = createClient_(pToken);
      client.sendMessage(
        {
          room_id: mychatID,
          body: Utilities.formatString("[qt][qtmeta aid=%s time=%s]%s[/qt]\nhttps://www.chatwork.com/#!rid%s-%s", fromAccountId, sendTime, message, roomID, messageId),
        }
      );
    }

    });

  } catch(e) {

    Logger.log("エラーが発生しました");
    Logger.log(e.message);
  }
}


function createClient_(api_token)
{
  return ChatWorkClient.factory(
    {
      'token': api_token,
    }
  );
}

//メッセージが誰宛か取得する・誰に返信したかも取得する・全員当てのメッセージも自分宛てに変換
function getTOAccount(obj,accound_id){
  let body = obj.body;
  let reg = /To:[0-9]{7}/g;
  let to_ids = body.match(reg);
  let reg2 = /\[rp aid=[0-9]{7}/g;
  let aid_ids = body.match(reg2);
  
  let reg3 =/\[toall\]/g;
  let toall_id = body.match(reg3);

  let toaccount = [];
  if(Array.isArray(to_ids) || to_ids !== null){
    to_ids.map((value,index,array)=>{
      
      value = String(value);
      value = value.replace(/To:/g,"");
      array[index] = value;
      
    })
    toaccount.push(to_ids);

  }else if(Array.isArray(aid_ids)){
    aid_ids.map((value,index,array)=>{
      
      value = String(value);
      value = value.replace(/\[rp aid=/g,"");
      array[index] = value;
      
    })
    toaccount.push(aid_ids);
  }else if(toall_id !== null){
    toaccount.push(accound_id)
  }else{
    toaccount.push(to_ids);
    toaccount.push(aid_ids)
  }
  return toaccount;
}
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?