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