Lunch invitation bot on Slack

Invitation

We invite all the channels for lunch at a fixed time every day and inform you of the departure just before the departure time. I am using "I made a Slack bot that decides a nice lunch".

.js


function isHoliday(){
  var today = new Date();
  //Judgment whether it is Saturday and Sunday
  var weekInt = today.getDay();
  if(weekInt <= 0 || 6 <= weekInt){
    return true;
  }

  //Judgment whether it is a holiday
  var calendarId = "ja.japanese#[email protected]";
  var calendar = CalendarApp.getCalendarById(calendarId);
  var todayEvents = calendar.getEventsForDay(today);
  if(todayEvents.length > 0){
    return true;
  }
  return false;
}
  var holiday = isHoliday()
    if(holiday == false ){
        ~~~
    }

So, I try to run it only on weekdays.

I want you to talk to me when it's time, so I tried to talk to the person who reacted (forced with Python from the local PC)

lunch.png

Identifying who goes for lunch

Prepare tokens, channel IDs, BOTIDs, etc. You can get the channel ID by clicking "Copy Link" from the channel list. BOTID can also be obtained by "copying the link" from the bot's remark.

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"

Because the time stamp (ts) returned in channels.history is UNIX time

    #Get today(UNIX time)
    now = time.time()
    #3600=1 hour ago
    nowd = math.floor(now) - 3600

Get the channel remarks with json

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

Identify the person who responded to what the bot said an hour ago.

    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

Then send a message to Slack to inform you of your departure

    if realist != []:
        for ulis in realist:
            atesaki += "<@" + ulis + ">"
        message = atesaki + "Can you have lunch?:airplane:"
        postpayload = {
            "token": token,
            "channel": channel_id,
            "username": "Lunch invitation",
            "icon_emoji": ':ramen:',
            "text": message
        }
    else:
        postpayload = {
            "token": token,
            "channel": channel_id,
            "username": "Lunch invitation failure",
            "icon_emoji": ':kevin:',
            "text": "No one seems to go for lunch"
        }
    requests.post(posturl, data = postpayload)

After that, it will be executed regularly somewhere I want to put them all together, so I'll use Python for the first half (and run it with FaaS).

Recommended Posts

Lunch invitation bot on Slack
Golf on Slack
Create a slack bot
How to make a slack bot
Easy with Slack using Bot #NowPlaying