LoginSignup
6
7

More than 3 years have passed since last update.

IFTTTとGmailの連携ができなくなったのでgoogle app scriptでGmail→Lineを自作してみた

Posted at

IFTTTとGmailの連携が終了

IFTTT、3月31日からGmailのトリガーと下書き作成が利用不可に
https://japanese.engadget.com/2019/03/23/ifttt-3-31-gmail/

今日の今日まで放置してたが、しゃあないと思い自分で書いた。

事前準備

line notifyに登録し、アクセストークンを取得する
https://notify-bot.line.me/ja/

google app scriptを書く

function line_notfication() {
    //hoge@hoge.comからのメールで題名がhogehogeで未読のものを最大50取得
    var threads = GmailApp.search('from:hoge@hoge.com is:unread subject:hogehoge', 0, 50);
    Logger.log(threads.length + "mails");
    if (threads == null || threads.length === 0) {
    return;
    } else {
        for (var i in threads) {
            var msg = threads[i].getMessages()[0];
            var msg_body = msg.getBody();

            //lineにメッセージを送る
            var accessToken = '*********'
            var options = {
                'method': 'post',
                'headers': {
                    'Authorization': 'Bearer ' + accessToken
                 },
                'payload': {
                    'message': msg_body
                 }
            };
            var response = UrlFetchApp.fetch('https://notify-api.line.me/api/notify', options);
            Logger.log(response);

            //既読にする
            threads[i].markRead();
        }
    }
}

書いてみて

公式リファレンスが一番わかりりやすい。英語大事。

6
7
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
6
7