This article is from JSL \ (Japan System Giken ) Advent Calendar 2019 -Qiita on December 6th. Last year's calendar is here! !!
TL;DR
@ here
Chalice
(Python) and ʻAWS Lambda`Although we have a long history, we are a company that sells free working styles that "recommend full flex & remote work without core time". All the time to go to work is different, and about 30% are remote workers. Therefore, it is rare for everyone who has an office to come to work.
However, when the way of working changes, there are ** good and bad things to discuss and problems to be solved **. This article is about trying to solve the problem of remote work.
Recently, I see that the topic of Slack operation of each company, such as @ here
, @ channel
prohibition, has become a topic.
Notifications are useful, but they can also distract programmers.
Meanwhile, there was such a @ here
mention on our Slack (my post ...)
How about that? As for the situation, "a little hurry" and "I want to notify only active users", so I think it is a general usage of @ here
.
But what about remote workers? For remote workers, it should not be a problem if the usage status of the meeting room and reception room of the company building is not shared.
So remote workers don't need this notification.
Of course, you may feel sorry for not only the person being notified but also the person notifying by @ here
to skip the notification to the remoteworker.
If it's a company that "recommends remote work ... but I'll give you a lot of unrelated notifications !!", it's not a comfortable environment for programmers ...?
As you can see, as the number of remote workers increases, it becomes important to design a ** notification destination for chat **.
So I thought about ** dynamic mention **. I thought that this problem could be solved if there was a mention that was notified only to users who met certain conditions.
In this example, it would be ** "mentions that are notified only to those who are at work today" **.
By the way, @ here
is also a dynamic mention in the sense that it only notifies active users in the channel.
We have an in-house destination board and its REST API. If you use this, you can get a list of people who are in the office. So it would be nice if I could somehow mention on Slack toward the list of people who are in the office.
When I was looking for a nice API on Slack, I found an API called ʻusergroups.users.update`.
usergroups.users.update method | Slack
You can update users in a user group by passing an array of user IDs to usergroup and user IDs to users.
So
It seems that we should be able to realize!
So I decided to use ʻAWS Lambda`.
With Lambda, even if it works like a minute on weekdays (20 days) + 1 hour before and after (10 hours) 20 * 10 * 60 = 12,000 So I can afford to fit in the free quota of 1,000,000 requests every month,
I also used chalice
to deploy Lambda
aws/chalice: Python Serverless Microframework for AWS
chalice
is a framework that allows you to write Lambda based on a Flask
-like decorator and even create (delete) peripheral services.
If you want to receive Http (s), Lambda is troublesome because it needs to cooperate with ʻAPIGateway`, but it takes care of that area.
For example, the regular execution, which is the requirement this time, can also be written in the syntax of chalice
.
Example of regular execution every 5 minutes
from chalice import Chalice, Rate
app = Chalice(app_name="helloworld")
# Automatically runs every 5 minutes
@app.schedule(Rate(5, unit=Rate.MINUTES))
def periodic_task(event):
return {"hello": "world"}
The final code is as follows (the part that uses the company's API is omitted)
src/update_usergroup.py
@app.schedule(Cron('0', '23-9', '?', '*', 'SUN-THU', '*'))
def update_in_office_usergroup(event):
OAUTH_TOKEN = 'token_xxxx_xxxx'
#Pre-created user group UID
in_office_usergroup_uid = 'some_usergorup_uid'
#~abridgement~
#List of Slack IDs of users who are in the office obtained from the API
office_user_slack_ids = ['some', 'user', 'uids']
try:
data = urllib.parse.urlencode(
(
("token", OAUTH_TOKEN),
("usergroup", in_office_usergroup_uid),
("users", ','.join(office_user_slack_ids)),
)
)
data = data.encode("ascii")
request = urllib.request.Request(
"https://slack.com/api/usergroups.users.update",
data=data,
method="POST"
)
request.add_header(
"Content-Type",
"application/x-www-form-urlencoded"
)
with urllib.request.urlopen(request) as r:
resp = json.loads(r.read())
logger.info(resp)
return {'ok': True}
except Exception as e:
logger.error(e)
return {'ok': False}
Since I use Lambda, I use ʻurllib` without external packages.
In addition, the cron formula is shifted by 9 hours by adjusting UTC so that it is Sunday-Thursday 23-9 o'clock (Monday-Friday 9-18 o'clock in Japan time). (Isn't this a good way?)
I named it @in_office
and released it with a doy face.
However, there are reports that such notifications come frequently. ..
Apparently, when I add / remove user groups, notifications fly from Slackbot. I made it for the purpose of reducing unnecessary notifications, but on the contrary, the number of notifications has increased. This is useless ...
I thought, "Is there an option to turn off notifications via API?", So I looked it up, but it didn't seem to be there.
I got stuck here, and although it was a function I made, I stopped using it. This is the original tree Ami.
The main reason is the lack of technical verification.
I started running when I got "I have an API! I have an idea! I think I can do it!" And didn't even try it with a small code. Can the selected technology solve the problem? Is it a suitable way to solve the problem? It is important to consider.
On the other hand, I feel that the idea of designing chat notifications was good.
Well, it's a personal development, so it's a catch that I failed, and this is also part of the technical verification!
Dynamic Mention I think it's a pretty good idea. .. If anyone who has read this article realizes it, please let me know.
When I made an official inquiry in poor English, I received a polite answer. The current usergroup API (October 2019 at the time of inquiry) cannot turn off notifications, but he wants to inform the feedback team so that he can do it in the future.
I would like to challenge again once it is implemented!
Recommended Posts