generated from cern-sis/errbot-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
reminder.py
217 lines (178 loc) · 7.34 KB
/
reminder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from datetime import date, datetime, timedelta
import pytz
from errbot import BotPlugin, botcmd
import re
from openai import OpenAI
import httpx
import os
import random
# Daily / Retrospective --> If it's an ordinary day or a retrospective, meeting at 9:30
# Sprint Planning --> If it's a Sprint planning, meeting at 15:30
# Sprint review --> If it's a Sprint Review, meeting at 14:45
# -----------------
utc = pytz.timezone("UTC")
now = utc.localize(datetime.utcnow())
tz_cern = pytz.timezone("Europe/Zurich")
now.astimezone(tz_cern)
EVENTS = {
"sprint planning": (
tz_cern.localize(datetime(2022, 2, 28, 15, 00)),
timedelta(weeks=2),
),
"review": (
tz_cern.localize(datetime(2022, 3, 10, 15, 00)),
timedelta(weeks=2),
),
"Retrospective": (
tz_cern.localize(datetime(2022, 3, 11, 9, 30)),
timedelta(weeks=2),
),
"daily": (
tz_cern.localize(datetime(2022, 3, 1, 9, 30)),
timedelta(days=1),
),
}
CONFIG = {
"ts_stream_id": 311658,
"zoom_url_regex": re.compile('https://cern.zoom.us/j/\d+\?pwd=\w+'),
}
class Reminder(BotPlugin):
@staticmethod
def get_monday(today):
today_weekday = today.weekday()
monday = today.date() - timedelta(days=today_weekday)
monday = datetime.combine(monday, datetime.min.time())
return monday.date()
@staticmethod
def is_sprint_planning(today):
first_iteration_startdate = date(2022, 2, 28)
nb_days = (first_iteration_startdate - Reminder.get_monday(today)).days
nb_weeks = nb_days // 7
return nb_weeks % 2 == 0
@staticmethod
def next_occurance(meeting, today):
today = tz_cern.localize(today)
next_occurance = EVENTS.get(meeting)[0].astimezone(tz_cern)
delta_occurance = EVENTS.get(meeting)[1]
while next_occurance <= today:
next_occurance += delta_occurance
if next_occurance > today:
return next_occurance.strftime("**%Y-%m-%d** at **%H:%M**")
@staticmethod
def next_daily(today):
today = tz_cern.localize(today)
next_daily = EVENTS.get("daily")[0].astimezone(tz_cern)
delta_daily = EVENTS.get("daily")[1]
while next_daily <= today:
next_daily += delta_daily
if Reminder.is_sprint_planning(today):
if next_daily.weekday() == 0:
next_daily += delta_daily
if next_daily.weekday() >= 5:
next_daily += timedelta(days=(7 - next_daily.weekday()))
else:
if next_daily.weekday() >= 3:
next_daily += timedelta(days=(7 - next_daily.weekday() + 1))
return next_daily.strftime("**%Y-%m-%d** at **%H:%M**")
@staticmethod
def get_openai_message(meeting_type, zoom_link, time_until_meeting):
character = ["Marvin from Hitchhiker's Guide To The Galaxy."]
client = OpenAI(timeout=httpx.Timeout(15.0, read=5.0, write=10.0, connect=3.0), api_key=os.environ.get("OPENAI_API_KEY"),)
prompt = (
f"You are a chatbot that announces the next meeting."
f"Your character is {random.choice(character)}."
f"The meeting is happening in {time_until_meeting} minutes and it is a {meeting_type} meeting. "
"Create a short text message for this announcement."
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": prompt}
],
max_tokens=50
)
message_content = response.choices[0].message.content.strip()
final_message = f"@**all** {message_content} \n\n [meeting]({zoom_link})."
return final_message
def zoom_meeting_url(self):
client = self._bot.client
# Get stream by id is currently not available on the python client.
response = client.get_streams(
include_public = False,
)
stream = next(filter(
lambda s: s["stream_id"] == CONFIG["ts_stream_id"],
response["streams"],
))
match = CONFIG["zoom_url_regex"].search(stream["description"])
return match.group()
@botcmd
def test_ai_message(self, msg, args):
return Reminder.get_openai_message("daily", "abc", 5)
@botcmd
def reminder_link(self, msg, args):
return f"[Link]({self.zoom_meeting_url()}) to our Zoom meeting"
@botcmd
def reminder_next(self, msg, args):
today = datetime.now()
next_planning = Reminder.next_occurance("sprint planning", today)
next_daily = Reminder.next_daily(today)
next_review = Reminder.next_occurance("review", today)
next_retrospective = Reminder.next_occurance("Retrospective", today)
return "\n".join(
[
f"Next planning: {next_planning}",
f"Next daily: {next_daily}",
f"Next review: {next_review}",
f"Next retrospective: {next_retrospective}",
]
)
def send_notification(self, meeting, today):
client = self._bot.client
next_occurance = EVENTS.get(meeting)[0]
delta_occurance = EVENTS.get(meeting)[1]
while next_occurance <= today:
next_occurance += delta_occurance
if next_occurance > today:
if next_occurance.date() == today.date():
now_minus_15 = (next_occurance - timedelta(minutes=15)).time()
now_minus_5 = (next_occurance - timedelta(minutes=5)).time()
zoom_link = self.zoom_meeting_url()
if now_minus_15 == today.time():
client.send_message(
{
"type": "stream",
"to": "tools & services",
"topic": meeting,
"content": f"@**all** [meeting]({zoom_link}) in 15 minutes.",
}
)
if now_minus_5 == today.time():
try:
content = Reminder.get_openai_message(meeting, zoom_link, 5)
except Exception:
content = f"@**all** [meeting]({zoom_link}) in 5 minutes."
client.send_message(
{
"type": "stream",
"to": "tools & services",
"topic": meeting,
"content": content,
}
)
def notify_for_meetings(self):
today = (datetime.now().astimezone(tz_cern)).replace(second=0, microsecond=0)
weekday = today.weekday()
if weekday < 5:
if weekday == 0 and self.is_sprint_planning(today):
self.send_notification("sprint planning", today)
elif weekday == 4 and not self.is_sprint_planning(today):
self.send_notification("Retrospective", today)
elif weekday == 3 and not self.is_sprint_planning(today):
self.send_notification("daily", today)
self.send_notification("review", today)
else:
self.send_notification("daily", today)
def activate(self):
super().activate()
self.start_poller(60, self.notify_for_meetings)