This time, I decided to notify slack of Redmine ticket update in business, so I will summarize it.
My company uses Redmine for outsourcing a certain business, but when there is a remand from the outsourcer, the response to the ticket is slow! !! It was often said (although the words are a little softer ...).
After that, when I searched for the cause, it seemed that the main cause was ** I didn't notice the remand ** </ font> in the first place.
Well, you can only catch notifications when you renew your ticket on Redmine by email. .. I understand that feeling ...! If you can set notifications like Backlog, you will notice it even when you are still doing other work, but of course there is no such function.
I was wondering what to do, but if I get a notification to ** slack **, which is very active as a business chat tool at our company, I think I will notice it. I thought that I made this function.
Immediately came out https://www.sejuku.net/blog/852461 This article
Result: ✕ Since the Redmine used by us is created in the intranet, I gave up because I can not connect to Redmine from slack using RSS in the first place.
Result: ✕ Notifying slack with a webhook using a plugin called "Redmine Slack" The content of this was exactly what I wanted to do this time, but the trigger is ** only when a new ticket is created ... **. It's delicious ... (Since it is a plug-in that seems to be in demand like this, I wanted you to have a little more freedom ...)
Finally, use Redmine API to actively acquire data (tickets) that meet the conditions and notify slack of it. I settled down to the correspondence. Since I hit the API as I like, it seems to be highly versatile in the future.
sashimodoshi.py
# -*- coding: utf-8 -*-
import digdag
import requests
import sys
import os
from datetime import datetime, timedelta
import json
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from .utils.util_slacker import UtilSlacker
class Main():
def __init__(self, incoming_webhook_url, slack_token):
self.incoming_webhook_url = incoming_webhook_url
self.slack_token = slack_token
self.slacker = UtilSlacker(self.incoming_webhook_url, self.slack_token)
def main(self):
redmine_api_key = "API access key" # 個人設定>API access keyから取得
redmine_url = "http://URL of redmine"
#Update date within 30 minutes
now = datetime.utcnow() + timedelta(minutes=-30)
now = now.strftime('%Y-%m-%dT%H:%M:%SZ')
request_url_for_issues = redmine_url + "/issues.json"
params = {
"key": redmine_api_key,
"project_id": "dd",
"status_id": 4,
"updated_on": ">=" + now
}
issues_res = requests.get(request_url_for_issues, params)
issues_responses = json.loads(issues_res.text)
print(issues_responses)
fields = []
if len(issues_responses["issues"]) == 0: #If it is 0, it will be processed
return
for issue in issues_responses["issues"]:
issue_id = issue["id"]
subject = issue["subject"]
assigned_to_name = issue["assigned_to"]["name"]
field = {
"title": subject,
"value": "Person in charge:" + assigned_to_name + "\r\nlink:<" + redmine_url + "/issues/{0}|{0}>".format(issue_id),
}
print(field)
fields.append(field)
msg = "`Remand`I have a ticket for.\r\n Please check the person in charge."
channel = "#pj-〇〇"
user = "redmine remand notification"
icon_emoji = ":redmine:"
fallback = "redmine"
title = ""
attachment1 = self.slacker.attachment_creater(
fallback=fallback, title=title, color="danger", fields=fields)
attachments = [attachment1]
self.slacker.slack_messenger(
msg, channel, user, icon_emoji, is_link_name=1, attachments=attachments)
I call this process in batch, but since we already have a digdag server, we decided to run it on it.
sashimodoshi.dig
timezone: Asia/Tokyo
schedule:
cron>: 00,30 10-18 * * 1-5
weekly>: Thu,10:30:00
_export:
workflow_name: "sashimodoshi"
+sashimodoshi:
py>: bin.python_project.sashimodoshi.Main.main
incoming_webhook_url: ${env.incoming_webhook_url}
slack_token: ${env.slack_token}
The dig file looks like this. In the schedule settings, it is set to run for 30 minutes between 10:00 and 18:00 from Monday to Friday. (I'm just imitating this place, but I haven't caught up with my understanding yet ...)
The implementation image looks like this. I'm using attachment in the slack notification API this time, and it looks good!
This time, we implemented slack notification with momentum to improve work efficiency and automation, but as a remaining issue
It's around, so I'll fix it soon.
This is the first time I've touched Python properly, but I'm still studying because it seems that it will continue to play an active role in terms of work efficiency and automation, such as being able to connect APIs quickly and in some cases Excel operations. I will go! !!