I'd like to sort emails that come to gmail into specific groups in the chat work according to the keywords in the body.

Asked 1 years ago, Updated 1 years ago, 44 views

I wanted to distribute the email that came to gmail to the specified group of chat work according to the keywords in the body, so I created it as follows, but it will not be forwarded to chat work even if I receive an email that meets the requirements.

Only function main1 works fine, but how should I correct it if I make multiple distributions?
Please let me know.

function main1(){
  // Chat work token ID
  const CW_TOKEN = "AAAA";
  const CW_ROOM = BBBB;
  const CW_TO=123456;

  // Search Gmail for specific criteria threads and retrieve mail
  var strTerms = 'is:unread' ■■■■ AROUND ●●●● ';
  var myThreads=GmailApp.search(strTerms, 0,50); // Get threads that match the criteria
  var myMsgs = GmailApp.getMessagesForThreads (myThreads); // Retrieve mail from thread → Store in a two-dimensional array

  // Chatwork API
  varclient=ChatWorkClient.factory({
    token —CW_TOKEN
  });

  // Each thread x mail
  for(vari=myMsgs.length-1;i>=0;i--){
    varmsgsInThread=myMsgs[i];
    for (varj=0;j<msgsInThread.length;j++){
      varmsg=msgsInThread[j];

      // Not Read Only
      if(msg.isUnread()){
        // Make the mail read
        msg.markRead();

        // Message creation
        varmsgBody="[To:"+CW_TO+"]+
                      US>"[info]" +
                          "[title]" + msg.getSubject() + "[/title]" +
                          msg.getFrom() + "[hr]" +
                          msg.getPlainBody().slice(0,400)+"[hr]"+
                          msg.getDate()+
                      "[/info]";

        // Send to Chat Work
        client.sendMessage({
          room_id —CW_ROOM,
          body —msgBody
        });
      }
    }
  }
}


function main2(){
  // Chat work token ID
  const CW_TOKEN = "AAAA";
  const CW_ROOM = CCCC;
  const CW_TO=789123;

  // Search Gmail for specific criteria threads and retrieve mail
  var strTerms = 'is:unread' ■■■■ AROUND ●●●● ';
  var myThreads=GmailApp.search(strTerms, 0,50); // Get threads that match the criteria
  var myMsgs = GmailApp.getMessagesForThreads (myThreads); // Retrieve mail from thread → Store in a two-dimensional array

  // Chatwork API
  varclient=ChatWorkClient.factory({
    token —CW_TOKEN
  });

  // Each thread x mail
  for(vari=myMsgs.length-1;i>=0;i--){
    varmsgsInThread=myMsgs[i];
    for (varj=0;j<msgsInThread.length;j++){
      varmsg=msgsInThread[j];

      // Not Read Only
      if(msg.isUnread()){
        // Make the mail read
        msg.markRead();

        // Message creation
        varmsgBody="[To:"+CW_TO+"]+
                      US>"[info]" +
                          "[title]" + msg.getSubject() + "[/title]" +
                          msg.getFrom() + "[hr]" +
                          msg.getPlainBody().slice(0,400)+"[hr]"+
                          msg.getDate()+
                      "[/info]";

        // Send to Chat Work
        client.sendMessage({
          room_id —CW_ROOM,
          body —msgBody
        });
      }
    }
  }
}


function main3(){
  // Chat work token ID
  const CW_TOKEN = "AAAA";
  const CW_ROOM = DDDD;
  const CW_TO=456789;

  // Search Gmail for specific criteria threads and retrieve mail
  var strTerms = 'is:unread' ■■■■ AROUND ●●●● ';
  var myThreads=GmailApp.search(strTerms, 0,50); // Get threads that match the criteria
  var myMsgs = GmailApp.getMessagesForThreads (myThreads); // Retrieve mail from thread → Store in a two-dimensional array

  // Chatwork API
  varclient=ChatWorkClient.factory({
    token —CW_TOKEN
  });

  // Each thread x mail
  for(vari=myMsgs.length-1;i>=0;i--){
    varmsgsInThread=myMsgs[i];
    for (varj=0;j<msgsInThread.length;j++){
      varmsg=msgsInThread[j];

      // Not Read Only
      if(msg.isUnread()){
        // Make the mail read
        msg.markRead();

        // Message creation
        varmsgBody="[To:"+CW_TO+"]+
                      US>"[info]" +
                          "[title]" + msg.getSubject() + "[/title]" +
                          msg.getFrom() + "[hr]" +
                          msg.getPlainBody().slice(0,400)+"[hr]"+
                          msg.getDate()+
                      "[/info]";

        // Send to Chat Work
        client.sendMessage({
          room_id —CW_ROOM,
          body —msgBody
        });
      }
    }
  }
}

google-apps-script

2022-09-30 20:19

1 Answers

Let me confirm one thing.
"If there is ""A"" in the body of the email, it means ""X"" group in the chat work, and if there is ""B"", it means ""Y"" group, right?"

If the above assumptions are correct, the reason is that main1, main2, and main3 have only changed the function name, CW_ROOM, and CW_TO.
Where you set the criteria for searching for mail ↓

var strTerms='is:unread'●●●●AROUND■■■■';

This must be a different value for main1, main2, and main3.

Hereinafter referred to as Snakefoot

In this case, wouldn't it be better to summarize the conditions and make the actual process a function?
This code is for your reference. (I didn't move it)

function main(){
  // Chat work token ID
  const CW_TOKEN = "AAAA";
  const CW_ROOM = BBBB;
  const CW_TO=123456;

  // Chatwork API
  varclient=ChatWorkClient.factory({
    token —CW_TOKEN
  });

  const list = [{
    "cw_room": "BBBB",
    "cw_to": "123456",
    " terms": 'is:unread' ●●●● AROUND ■■■■_1" '
  },
  {
    "cw_room": "CCCC",
    "cw_to": "789123",
    " terms": 'is:unread' ●●●● AROUND ■■■■_2"'
  },{
    "cw_room": "DDDD",
    "cw_to": "456789",
    " terms": 'is:unread' ●●●● AROUND ■■■■_3"'
  }]

  for(i=0;i<list.length;i++){
    varo=list[0]
    sub(o["cw_room", o["cw_to", o["terms"))
  }
}

function sub(cw_room, cw_to, terms) {

  // Search Gmail for specific criteria threads and retrieve mail
  var myThreads=GmailApp.search(terms, 0,50); // Get threads that match the criteria
  var myMsgs = GmailApp.getMessagesForThreads (myThreads); // Retrieve mail from thread → Store in a two-dimensional array

  // Each thread x mail
  for(vari=myMsgs.length-1;i>=0;i--){
    varmsgsInThread=myMsgs[i];
    for (varj=0;j<msgsInThread.length;j++){
      varmsg=msgsInThread[j];

      // Not Read Only
      if(msg.isUnread()){
        // Make the mail read
        msg.markRead();

        // Message creation
        varmsgBody="[To:"+cw_to+"]+
                      US>"[info]" +
                          "[title]" + msg.getSubject() + "[/title]" +
                          msg.getFrom() + "[hr]" +
                          msg.getPlainBody().slice(0,400)+"[hr]"+
                          msg.getDate()+
                      "[/info]";

        // Send to Chat Work
        client.sendMessage({
          room_id —cw_room,
          body —msgBody
        });
      }
    }
  }
}


2022-09-30 20:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.