OILS / devtools / services / zulip.sh View on Github | oils.pub

146 lines, 74 significant
1#!/usr/bin/env bash
2#
3# Usage:
4# devtools/services/zulip.sh <function name>
5#
6# https://oilshell.zulipchat.com -> Personal -> Bots for email and API key
7#
8# To get a thread, you have to get the messages in the stream, and the filter
9# it with JQ.
10
11set -o nounset
12set -o pipefail
13set -o errexit
14
15# private AUTH
16. zulip-env.sh
17
18my-curl() {
19 # --get affects -d
20 curl \
21 --silent --show-error --get \
22 "$@"
23}
24
25messages-in-stream() {
26 local bot_email=${1:-$ZULIP_EMAIL}
27 local bot_api_key=${2:-$ZULIP_KEY}
28 local stream=${3:-'blog-ideas'}
29 local apply_markdown=${4:-false} # or 'true'
30
31 local narrow='[{"operand": "'$stream'", "operator": "stream"}]'
32
33 # copied from example at https://zulip.com/api/get-messages
34 my-curl \
35 -u "$bot_email:$bot_api_key" \
36 -d 'anchor=newest' \
37 -d 'num_before=3000' \
38 -d 'num_after=0' \
39 -d "apply_markdown=$apply_markdown" \
40 --data-urlencode narrow="$narrow" \
41 https://oilshell.zulipchat.com/api/v1/messages
42
43 # doesn't work
44 # --data-urlencode narrow='[{"operand": "0.8.4 Release Notes", "operator": "topic"}]' \
45}
46
47print-thread() {
48 # Get these from Zulip web interface
49 local bot_email=$1
50 local bot_api_key=$2
51 local stream=${3:-'oil-dev'}
52 local subject=${4:-'Test thread'}
53 local apply_markdown=${5:-false} # or 'true'
54
55 # https://stackoverflow.com/questions/28164849/using-jq-to-parse-and-display-multiple-fields-in-a-json-serially/31791436
56
57 # JQ query
58 # - narrow to messages array
59 # - create record with content and subject field
60 # - select records where subject is "needle" var
61 # - print the content. -r prints it raw.
62
63 messages-in-stream "$bot_email" "$bot_api_key" "$stream" "$apply_markdown" | \
64 jq --arg subject "$subject" -r \
65 '.messages[] | { content: .content, subject: .subject } |
66 select( .subject == $subject ) | (.content + "\n\n")'
67}
68
69print-both() {
70 local prefix=${1:-_tmp/zulip/sept}
71 mkdir -p "$(dirname $prefix)"
72
73 local stream='blog-ideas'
74 local subject='Oils September Status Update'
75
76 print-thread $ZULIP_EMAIL $ZULIP_KEY "$stream" "$subject" false | tee $prefix.md
77 print-thread $ZULIP_EMAIL $ZULIP_KEY "$stream" "$subject" true | tee $prefix.html
78
79 ls -l $prefix*
80}
81
82to-html() {
83 local prefix=${1:-_tmp/zulip/sept}
84
85 doctools/cmark.sh cmark-py < $prefix.md > $prefix.cmark.html
86 ls -l $prefix*
87}
88
89common-mark-prefix() {
90 local date_str=$(date +'%Y/%m/%d')
91
92 echo "\
93---
94title: Post created on $date_str
95date: $date_str
96css_file: blog-bundle-v7.css
97body_css_class: width35
98default_highlighter: oils-sh
99comments_url: TODO
100published: no
101---
102
103<div id="toc">
104</div>
105
106## Heading
107"
108}
109
110to-common-mark() {
111 local prefix=${1:-_tmp/zulip/sept}
112
113 local dest=../oils.pub/blog/2025/09/_sept.md
114 { common-mark-prefix
115 cat $prefix.md | devtools/services/zulip.py
116 } > $dest
117 echo "Wrote $dest"
118}
119
120#
121# These weren't needed
122#
123
124topics() {
125 local bot_email=$1
126 local bot_api_key=$2
127
128 # stream ID for #oil-dev. You get the max ID
129 local stream_id=121539
130
131 my-curl \
132 -u "$bot_email:$bot_api_key" \
133 https://oilshell.zulipchat.com/api/v1/users/me/$stream_id/topics
134}
135
136one-message() {
137 local bot_email=$1
138 local bot_api_key=$2
139
140 # message ID from max_id of topics
141 my-curl \
142 -u "$bot_email:$bot_api_key" \
143 https://oilshell.zulipchat.com/api/v1/messages/158997038
144}
145
146"$@"