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 |
|
11 |
|
12 | set -o nounset
|
13 | set -o pipefail
|
14 | set -o errexit
|
15 |
|
16 | my-curl() {
|
17 | # --get affects -d
|
18 | curl \
|
19 | --silent --show-error --get \
|
20 | "$@"
|
21 | }
|
22 |
|
23 | messages-in-stream() {
|
24 | local bot_email=$1
|
25 | local bot_api_key=$2
|
26 | local stream=${3:-'blog-ideas'}
|
27 |
|
28 | local narrow='[{"operand": "'$stream'", "operator": "stream"}]'
|
29 |
|
30 | # copied from example at https://zulip.com/api/get-messages
|
31 | my-curl \
|
32 | -u "$bot_email:$bot_api_key" \
|
33 | -d 'anchor=newest' \
|
34 | -d 'num_before=3000' \
|
35 | -d 'num_after=0' \
|
36 | -d 'apply_markdown=false' \
|
37 | --data-urlencode narrow="$narrow" \
|
38 | https://oilshell.zulipchat.com/api/v1/messages
|
39 |
|
40 | # doesn't work
|
41 | # --data-urlencode narrow='[{"operand": "0.8.4 Release Notes", "operator": "topic"}]' \
|
42 | }
|
43 |
|
44 | print-thread() {
|
45 | # Get these from Zulip web interface
|
46 | local bot_email=$1
|
47 | local bot_api_key=$2
|
48 | local stream=${3:-'oil-dev'}
|
49 | local subject=${4:-'Test thread'}
|
50 |
|
51 | # https://stackoverflow.com/questions/28164849/using-jq-to-parse-and-display-multiple-fields-in-a-json-serially/31791436
|
52 |
|
53 | # JQ query
|
54 | # - narrow to messages array
|
55 | # - create record with content and subject field
|
56 | # - select records where subject is "needle" var
|
57 | # - print the content. -r prints it raw.
|
58 |
|
59 | messages-in-stream "$bot_email" "$bot_api_key" "$stream" | \
|
60 | jq --arg subject "$subject" -r \
|
61 | '.messages[] | { content: .content, subject: .subject } |
|
62 | select( .subject == $subject ) | (.content + "\n\n")'
|
63 | }
|
64 |
|
65 | #
|
66 | # These weren't needed
|
67 | #
|
68 |
|
69 | topics() {
|
70 | local bot_email=$1
|
71 | local bot_api_key=$2
|
72 |
|
73 | # stream ID for #oil-dev. You get the max ID
|
74 | local stream_id=121539
|
75 |
|
76 | my-curl \
|
77 | -u "$bot_email:$bot_api_key" \
|
78 | https://oilshell.zulipchat.com/api/v1/users/me/$stream_id/topics
|
79 | }
|
80 |
|
81 | one-message() {
|
82 | local bot_email=$1
|
83 | local bot_api_key=$2
|
84 |
|
85 | # message ID from max_id of topics
|
86 | my-curl \
|
87 | -u "$bot_email:$bot_api_key" \
|
88 | https://oilshell.zulipchat.com/api/v1/messages/158997038
|
89 | }
|
90 |
|
91 | "$@"
|