-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_messages.go
More file actions
328 lines (283 loc) · 9.67 KB
/
cmd_messages.go
File metadata and controls
328 lines (283 loc) · 9.67 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
import (
"fmt"
"net/url"
"strings"
"github.com/spf13/cobra"
)
func messagesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "messages",
Short: "Manage messages",
}
cmd.AddCommand(messagesListCmd())
cmd.AddCommand(messagesSearchCmd())
cmd.AddCommand(messagesSendCmd())
cmd.AddCommand(messagesEditCmd())
return cmd
}
func messagesListCmd() *cobra.Command {
var cursor, direction string
cmd := &cobra.Command{
Use: "list <chat-id>",
Short: "List messages in a chat",
Long: `List messages in a chat.
Returns messages sorted by timestamp with cursor-based pagination.
Examples:
beeper messages list "!abc123:beeper.local"
beeper messages list "!abc123:beeper.local" --direction before`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}
params := map[string]interface{}{}
if cursor != "" {
params["cursor"] = cursor
}
if direction != "" {
params["direction"] = direction
}
result, err := client.request("GET", "/v1/chats/"+url.PathEscape(cleanID(args[0]))+"/messages", params, nil)
if err != nil {
return err
}
outputResult(result)
return nil
},
}
cmd.Flags().StringVar(&cursor, "cursor", "", "Pagination cursor")
cmd.Flags().StringVar(&direction, "direction", "", "Pagination direction: before or after")
return cmd
}
func messagesSearchCmd() *cobra.Command {
var query string
var accountIDs, chatIDs, mediaTypes []string
var chatType, sender string
var after, before, cursor, direction string
var includeMuted, excludeLowPriority bool
var limit int
cmd := &cobra.Command{
Use: "search",
Short: "Search messages across chats",
Long: `Search messages across chats.
Uses Beeper's message index for fast searching.
Examples:
beeper messages search --query "meeting"
beeper messages search --sender me --after "2025-01-01T00:00:00Z"
beeper messages search --media-type image --media-type video`,
RunE: func(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}
trimmedQuery := strings.TrimSpace(query)
if trimmedQuery == "" {
hasOtherFilters := len(accountIDs) > 0 || len(chatIDs) > 0 || chatType != "" || sender != "" || len(mediaTypes) > 0 || after != "" || before != "" || cmd.Flags().Changed("include-muted") || cmd.Flags().Changed("exclude-low-priority") || limit > 0 || cursor != "" || direction != ""
if !hasOtherFilters {
return fmt.Errorf("query cannot be empty; provide --query or another filter")
}
}
params := map[string]interface{}{}
if trimmedQuery != "" {
params["query"] = trimmedQuery
}
if len(accountIDs) > 0 {
params["accountIDs"] = cleanIDs(accountIDs)
}
if len(chatIDs) > 0 {
params["chatIDs"] = cleanIDs(chatIDs)
}
if chatType != "" {
params["chatType"] = chatType
}
if sender != "" {
params["sender"] = cleanID(sender)
}
if len(mediaTypes) > 0 {
params["mediaTypes"] = mediaTypes
}
if after != "" {
params["dateAfter"] = after
}
if before != "" {
params["dateBefore"] = before
}
if cmd.Flags().Changed("include-muted") {
params["includeMuted"] = includeMuted
}
if cmd.Flags().Changed("exclude-low-priority") {
params["excludeLowPriority"] = excludeLowPriority
}
if limit > 0 {
params["limit"] = limit
}
if cursor != "" {
params["cursor"] = cursor
}
if direction != "" {
params["direction"] = direction
}
result, err := client.request("GET", "/v1/messages/search", params, nil)
if err != nil {
return err
}
outputResult(result)
return nil
},
}
cmd.Flags().StringVarP(&query, "query", "q", "", "Search text")
cmd.Flags().StringArrayVarP(&accountIDs, "account-ids", "a", nil, "Filter by account IDs")
cmd.Flags().StringArrayVarP(&chatIDs, "chat-ids", "c", nil, "Filter by chat IDs")
cmd.Flags().StringVar(&chatType, "chat-type", "", "Filter by chat type: group or single")
cmd.Flags().StringVar(&sender, "sender", "", "Filter by sender: me, others, or user ID")
cmd.Flags().StringArrayVar(&mediaTypes, "media-type", nil, "Filter by media type: any, video, image, link, file")
cmd.Flags().StringVar(&after, "after", "", "Messages after this ISO datetime")
cmd.Flags().StringVar(&before, "before", "", "Messages before this ISO datetime")
cmd.Flags().BoolVar(&includeMuted, "include-muted", true, "Include muted chats")
cmd.Flags().BoolVar(&excludeLowPriority, "exclude-low-priority", true, "Exclude low priority messages")
cmd.Flags().IntVar(&limit, "limit", 0, "Max results")
cmd.Flags().StringVar(&cursor, "cursor", "", "Pagination cursor")
cmd.Flags().StringVar(&direction, "direction", "", "Pagination direction")
return cmd
}
func messagesSendCmd() *cobra.Command {
var replyTo, uploadID, filePath, attachmentType string
var attachmentFilename, attachmentMimeType string
var attachmentDuration float64
var attachmentWidth, attachmentHeight int
cmd := &cobra.Command{
Use: "send <chat-id> [text]",
Short: "Send a message to a chat",
Long: `Send a message to a chat with optional attachment.
Supports markdown formatting in the message text.
To send a file, either:
1. Use --file to upload and send in one step
2. Use --upload-id to reference a previously uploaded file
Attachment options:
--type Attachment type: gif, voiceNote, sticker (auto-detected if omitted)
--duration Duration in seconds (for audio/video)
--filename Override the filename
--mime-type Override the MIME type
--width Override image/video width
--height Override image/video height
Examples:
# Send text message
beeper messages send "!chatid:beeper.local" "Hello, world!"
# Send with markdown
beeper messages send "!chatid:beeper.local" "**Bold** and _italic_"
# Send image with caption
beeper messages send "!chatid:beeper.local" "Check this out!" --file /path/to/image.png
# Send image only (no caption)
beeper messages send "!chatid:beeper.local" --file /path/to/image.png
# Send GIF
beeper messages send "!chatid:beeper.local" --file /path/to/animation.gif --type gif
# Send voice note
beeper messages send "!chatid:beeper.local" --file /path/to/audio.ogg --type voiceNote --duration 15
# Send sticker
beeper messages send "!chatid:beeper.local" --file /path/to/sticker.webp --type sticker
# Reply to a message
beeper messages send "!chatid:beeper.local" "Reply text" --reply-to "msg456"
# Use previously uploaded file
beeper messages send "!chatid:beeper.local" "Here's the file" --upload-id "abc-123-def"`,
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}
// Get text from args (optional when sending attachment only)
text := ""
if len(args) > 1 {
text = args[1]
}
// If file path provided, upload first
if filePath != "" {
uploadResult, err := client.uploadFile("/v1/assets/upload", filePath)
if err != nil {
return err
}
if id, ok := uploadResult["uploadID"].(string); ok {
uploadID = id
}
}
body := map[string]interface{}{}
if text != "" {
body["text"] = text
}
if replyTo != "" {
body["replyToMessageID"] = cleanID(replyTo)
}
if uploadID != "" {
attachment := map[string]interface{}{
"uploadID": uploadID,
}
if attachmentType != "" {
attachment["type"] = attachmentType
}
if attachmentFilename != "" {
attachment["fileName"] = attachmentFilename
}
if attachmentMimeType != "" {
attachment["mimeType"] = attachmentMimeType
}
if attachmentDuration > 0 {
attachment["duration"] = attachmentDuration
}
if attachmentWidth > 0 || attachmentHeight > 0 {
size := map[string]interface{}{}
if attachmentWidth > 0 {
size["width"] = attachmentWidth
}
if attachmentHeight > 0 {
size["height"] = attachmentHeight
}
attachment["size"] = size
}
body["attachment"] = attachment
}
result, err := client.request("POST", "/v1/chats/"+url.PathEscape(cleanID(args[0]))+"/messages", nil, body)
if err != nil {
return err
}
outputResult(result)
return nil
},
}
cmd.Flags().StringVarP(&replyTo, "reply-to", "r", "", "Message ID to reply to")
cmd.Flags().StringVarP(&filePath, "file", "f", "", "File path to upload and attach")
cmd.Flags().StringVar(&uploadID, "upload-id", "", "Upload ID from a previous assets upload")
cmd.Flags().StringVar(&attachmentType, "type", "", "Attachment type: gif, voiceNote, or sticker")
cmd.Flags().StringVar(&attachmentFilename, "filename", "", "Override attachment filename")
cmd.Flags().StringVar(&attachmentMimeType, "mime-type", "", "Override attachment MIME type")
cmd.Flags().Float64Var(&attachmentDuration, "duration", 0, "Duration in seconds (for audio/video)")
cmd.Flags().IntVar(&attachmentWidth, "width", 0, "Override attachment width")
cmd.Flags().IntVar(&attachmentHeight, "height", 0, "Override attachment height")
return cmd
}
func messagesEditCmd() *cobra.Command {
return &cobra.Command{
Use: "edit <chat-id> <message-id> <text>",
Short: "Edit an existing message",
Long: `Edit an existing message.
Examples:
beeper messages edit "!abc123:beeper.local" "msg456" "Updated text"`,
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}
body := map[string]interface{}{
"text": args[2],
}
result, err := client.request("PUT", "/v1/chats/"+url.PathEscape(cleanID(args[0]))+"/messages/"+url.PathEscape(cleanID(args[1])), nil, body)
if err != nil {
return err
}
outputResult(result)
return nil
},
}
}