shimajima-eiji
10/1/2018 - 3:33 PM

gas_slack2line: [https://nomuraya.work/techzine/0177, https://shimajima-eiji.github.io/gist/gas_slack2line/]

function mail(text) {
  /**
   * Slackで@okanとか@otonとかヒットする名前にした場合
   * @lineは既に使用済みなのでここではヒットしない
   */
  lines = text.split(' ')

  if (/^@okan/.test(lines[0])) {
    point = lines[0].length + ' '.length;
    title = '';
    if (/^\[.*\]/.test(lines[1]) ) {
      point += '['.length;
      title_len = text.indexOf(']') - point;
      title = text.substr(point, title_len);
      point = point + title_len + ']'.length;
    }
    to = PropertiesService
      .getScriptProperties()
      .getProperty('MAIL_OKAN');
    message = text.substr(point);
    send_okan(to, title, message + '\n\n【このメールはツールから送ってるです】');
  }

  return true;
}

function send_okan(to, subject, message) {
  var from = PropertiesService
    .getScriptProperties()
    .getProperty('MAIL_FROM');
  var user = PropertiesService
    .getScriptProperties()
    .getProperty('MAIL_NAME');
  GmailApp.sendEmail(to, subject, message, {from: from, name: user});
}
function line(text) {
  /**
   * LINEに送る
   */
  if (/^@line$/.test(text.split(' ')[0]) == false) {
    return false;
  }
  message = text.substr('@line '.length);
  //lineトークン
  var token = PropertiesService
    .getScriptProperties()
    .getProperty('LINE_NOTIFY_TOKEN');

  var op =
      {
        "method" : "post",
        "payload": "message=" + message,
        "headers":{"Authorization" : "Bearer " + token}
      };
  UrlFetchApp.fetch("https://notify-api.line.me/api/notify",op);
  return true;
}

gas_slack2line

appsscript.json

view latest

doPost.gs
(SLACK_OUTGOING_TOKEN)

line.gs
(LINE_NOTIFY_TOKEN)

mail.gs
(MAIL_FROM)
(MAIL_USER)
(MAIL_OKAN)
(MAIL_OTON)
...

doPost.js

view latest

function doPost(e) {
  // 投稿のトークン取得
  var t = e.parameter.token;

  // トークンが一致しているか
  // [TODO] 何かメッセージがあったとかSpreadSheetにログ残してもいいかも
  if( t != PropertiesService
  .getScriptProperties()
  .getProperty('SLACK_OUTGOING_TOKEN'))
  {return;}
  
  exec(e.parameter.text);
}

function exec(text) {
  /**
   * どのファイルを実行するか制御する
   * それぞれのクラス(残念ながらただの関数的オブジェクト)の実行判定をtrue/falseで取得している
   */
  if (line(text)) {
    return;
  }
  else if(mail(text)) {
    return;
  }
}

line.gs

view latest

function line(text) {
  /**
   * LINEに送る
   */
  if (/^@line$/.test(text.split(' ')[0]) == false) {
    return false;
  }
  message = text.substr('@line '.length);
  //lineトークン
  var token = PropertiesService
    .getScriptProperties()
    .getProperty('LINE_NOTIFY_TOKEN');

  var op =
      {
        "method" : "post",
        "payload": "message=" + message,
        "headers":{"Authorization" : "Bearer " + token}
      };
  UrlFetchApp.fetch("https://notify-api.line.me/api/notify",op);
  return true;
}

mail.js

view latest

function mail(text) {
  /**
   * Slackで@okanとか@otonとかヒットする名前にした場合
   * @lineは既に使用済みなのでここではヒットしない
   */
  lines = text.split(' ')

  if (/^@okan/.test(lines[0])) {
    point = lines[0].length + ' '.length;
    title = '';
    if (/^\[.*\]/.test(lines[1]) ) {
      point += '['.length;
      title_len = text.indexOf(']') - point;
      title = text.substr(point, title_len);
      point = point + title_len + ']'.length;
    }
    to = PropertiesService
      .getScriptProperties()
      .getProperty('MAIL_OKAN');
    message = text.substr(point);
    send_okan(to, title, message + '\n\n【このメールはツールから送ってるです】');
  }

  return true;
}

function send_okan(to, subject, message) {
  var from = PropertiesService
    .getScriptProperties()
    .getProperty('MAIL_FROM');
  var user = PropertiesService
    .getScriptProperties()
    .getProperty('MAIL_NAME');
  GmailApp.sendEmail(to, subject, message, {from: from, name: user});
}

back

function doPost(e) {
  // 投稿のトークン取得
  var t = e.parameter.token;

  // トークンが一致しているか
  // [TODO] 何かメッセージがあったとかSpreadSheetにログ残してもいいかも
  if( t != PropertiesService
  .getScriptProperties()
  .getProperty('SLACK_OUTGOING_TOKEN'))
  {return;}
  
  exec(e.parameter.text);
}

function exec(text) {
  /**
   * どのファイルを実行するか制御する
   * それぞれのクラス(残念ながらただの関数的オブジェクト)の実行判定をtrue/falseで取得している
   */
  if (line(text)) {
    return;
  }
  else if(mail(text)) {
    return;
  }
}
doPost.gs
(SLACK_OUTGOING_TOKEN)

line.gs
(LINE_NOTIFY_TOKEN)

mail.gs
(MAIL_FROM)
(MAIL_USER)
(MAIL_OKAN)
(MAIL_OTON)
...