Skip to content

Commit 26a2b10

Browse files
committed
google-calendar: Support user customization of the message template.
By adding a --format-message command line option, and a format_message config file option.
1 parent 47ffcc7 commit 26a2b10

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

zulip/integrations/google/google-calendar

+20
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class GoogleCalendarOptions:
4141
noauth_local_webserver: bool = False
4242
tokens_path: str = TOKENS_PATH
4343
client_secret_path: str = CLIENT_SECRET_PATH
44+
format_message: Optional[str] = None
4445

4546

4647
class Event(TypedDict):
@@ -69,6 +70,7 @@ usage = r"""google-calendar [--config-file PATH_TO_ZULIPRC_OF_BOT]
6970
[--client-secret-file PATH_TO_CLIENT_SECRET_FILE]
7071
[--tokens-file PATH_TO_GOOGLE_TOKENS_FILE]
7172
[-n] [--noauth_local_webserver]
73+
[-f MESSAGE_TEMPLATE] [--format-message MESSAGE_TEMPLATE]
7274
7375
This integration can be used to send Zulip messages as reminders for upcoming events from your Google Calendar.
7476
@@ -112,6 +114,11 @@ parser.add_argument(
112114
action="store_true",
113115
help="The default authorization process runs a local web server, which requires a browser on the same machine. For non-interactive environments and machines without browser access, e.g., remote servers, this option allows manual authorization. The authorization URL is printed, which the user can copy into a browser, copy the resulting authorization code, and paste back into the command line.",
114116
)
117+
parser.add_argument(
118+
"-f",
119+
"--format-message",
120+
help="A Python f-string to use to format the markdown message template. This option overrides the default message template. The f-string can use the following variables: start, end, title, description, calendar_link, location, google_meet_link.\nNote that the title, description, location, and google_meet_link variables are optional for Google Calendar events, and hence may be empty. Empty fields are displayed as {No title}, {No description}, {No location}, and {No link} in the message template.",
121+
)
115122
commandline_options = parser.parse_args()
116123
if commandline_options.verbose:
117124
logging.getLogger().setLevel(logging.INFO)
@@ -251,6 +258,19 @@ def populate_events() -> Optional[None]:
251258

252259

253260
def construct_message_from_event(event: Event) -> str:
261+
if calendar_options.format_message:
262+
message = calendar_options.format_message.format(
263+
start=event["start"].strftime("%Y-%m-%d %H:%M"),
264+
end=event["end"].strftime("%Y-%m-%d %H:%M"),
265+
title=event["summary"],
266+
description=event["description"] or "{No description}",
267+
calendar_link=event["html_link"],
268+
location=event["location"] or "{No location}",
269+
google_meet_link=event["hangout_link"] or "{No link}",
270+
)
271+
decoded_message = bytes(message, "utf-8").decode("unicode_escape")
272+
return decoded_message
273+
254274
time_period = (
255275
"today"
256276
if event["start"].hour == 0 and event["start"].minute == 0

0 commit comments

Comments
 (0)