LoginSignup
0
1

More than 3 years have passed since last update.

Slackでランチのお誘いbot

Posted at

お誘い

毎日決まった時間にchannel全員をお昼を誘い、出発時間直前に出発をお知らせします。
いい感じにランチを決める Slack ボットを作った」を使わせていただいています。

function isHoliday(){
  var today = new Date();
  //土日か判定
  var weekInt = today.getDay();
  if(weekInt <= 0 || 6 <= weekInt){
    return true;
  }

  //祝日か判定
  var calendarId = "ja.japanese#holiday@group.v.calendar.google.com";
  var calendar = CalendarApp.getCalendarById(calendarId);
  var todayEvents = calendar.getEventsForDay(today);
  if(todayEvents.length > 0){
    return true;
  }
  return false;
}
  var holiday = isHoliday()
    if(holiday == false ){
        ~~~
    }

で、平日のみ実行するようにしています。

時間になったら声をかけてほしいのでリアクションしてくれた人に声をかけるようにしました(ローカルPCからPythonで無理やり)

lunch.png

ランチ行く人の特定

tokenやらチャンネルIDやらBOTIDやらの準備をします。
チャンネルIDはチャンネルリストから「リンクをコピー」で取得できます。
BOTIDもBotの発言から「リンクをコピー」で取得できます。

import requests
import json
import math
import time

histurl = "https://slack.com/api/channels.history"
posturl = "https://slack.com/api/chat.postMessage"
token = "SLACKTOKEN"
channel_id = "SLACKCHANNNELID"
bot_id = "SLACKBOTID"

channels.historyで帰ってくるタイムスタンプ(ts)がUNIX時間なので

    #今日取得(UNIX時間)
    now = time.time()
    #3600=1時間 前の時間
    nowd = math.floor(now) - 3600

チャンネルの発言をjsonで取得して

    payload = {
        "token": token,
        "channel": channel_id
        }
    response = requests.get(histurl, params=payload)
    json_data = response.json()

botの1時間前の発言にリアクションしてくれた人を特定します。

    for i in json_data["messages"]:
        try:
            if i["bot_id"] == bot_id:
                if float(i['ts']) >= nowd:
                    for u in i['reactions']:
                        for v in u["users"]:
                            realist.append(v)
        except:
            pass

あとは出発をお知らせするメッセージをSlackに投げます

    if realist != []:
        for ulis in realist:
            atesaki += "<@" + ulis + ">"
        message = atesaki + "お昼いけますか?:airplane:"
        postpayload = {
            "token": token,
            "channel": channel_id,
            "username": "ランチのお誘い",
            "icon_emoji": ':ramen:',
            "text": message
        }
    else:
        postpayload = {
            "token": token,
            "channel": channel_id,
            "username": "ランチのお誘い失敗",
            "icon_emoji": ':kevin:',
            "text": "誰もお昼に行かないようです"
        }
    requests.post(posturl, data = postpayload)

あとはどこかで定期実行
全部1つにまとめたいのでそのうち前半部分をPythonにし(てFaaSとかで動かし)ます。

0
1
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
0
1