Skip to content

Commit

Permalink
(failing) sends multipart messages to bluebubbles
Browse files Browse the repository at this point in the history
  • Loading branch information
trek-boldly-go committed Jan 18, 2024
1 parent 1f51831 commit b6b94a7
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 8 deletions.
82 changes: 74 additions & 8 deletions imessage/bluebubbles/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,8 @@ func (bb *blueBubbles) SendFile(chatID, text, filename string, pathOnDisk string
payload := map[string]interface{}{
"chatGuid": chatID,
"name": filename,
"method": "private-api",
"method": "apple-script",
"tempGuid": fmt.Sprintf("temp-%s", RandString(8)),
"selectedMessageGuid": replyTo,
"partIndex": fmt.Sprint(replyToPart),
}
Expand All @@ -637,24 +638,89 @@ func (bb *blueBubbles) SendFile(chatID, text, filename string, pathOnDisk string
}

if response.Status == 200 {
bb.log.Debug().Msg("Sent a file!")

bb.SendMessage(chatID, text, replyTo, replyToPart, nil, nil)
if text != "" {
if len(response.Data.Attachments) > 0 {
time.Sleep(5 * time.Second)

var imessageSendResponse = imessage.SendResponse{
GUID: response.Data.GUID,
Service: response.Data.Handle.Service,
Time: time.Unix(0, response.Data.DateCreated*int64(time.Millisecond)),
attachmentId := response.Data.Attachments[0].GUID

imessageSendResponse, err := bb.sendMultipartMessage(chatID, text, attachmentId, filename, replyTo, replyToPart)

if err != nil {
bb.log.Err(err).Msg("Attachment sent, but failed to send caption")
return nil, err
}

return imessageSendResponse, nil
}

err := errors.New("empty attachments response from BlueBubbles")

bb.log.Err(err).Any("response", response).Msg("BlueBubbles reported success sending attachment, but did not return the attachment id so the caption cannot be added")

// TODO instead of a complete failure here, fall back to just sending a regular text

return nil, err
} else {
var imessageSendResponse = imessage.SendResponse{
GUID: response.Data.GUID,
Service: response.Data.Handle.Service,
Time: time.Unix(0, response.Data.DateCreated*int64(time.Millisecond)),
}

return &imessageSendResponse, nil
}

return &imessageSendResponse, nil
// bb.SendMessage(chatID, text, replyTo, replyToPart, nil, nil)
} else {
bb.log.Error().Any("response", response).Msg("Failure when sending message to BlueBubbles")

return nil, errors.New("could not send message")
}
}

func (bb *blueBubbles) sendMultipartMessage(chatID string, text string, attachmentId string, attachmentName string, replyTo string, replyToPart int) (*imessage.SendResponse, error) {
bb.log.Trace().Str("chatID", chatID).Str("text", text).Str("attachmentId", attachmentId).Str("attachmentName", attachmentName).Str("replyTo", replyTo).Int("replyToPart", replyToPart).Msg("sendMultipartMessage")

request := SendMultipartMessageRequest{
ChatGUID: chatID,
SelectedMessageGuid: replyTo,
PartIndex: &replyToPart,
Parts: []MultipartMessagePart{
{
PartIndex: 0,
Text: text,
},
{
PartIndex: 1,
Attachment: attachmentId,
Name: attachmentName,
},
},
}

var res SendTextResponse

err := bb.apiPost("/api/v1/message/multipart", request, &res)
if err != nil {
return nil, err
}

if res.Status != 200 {
bb.log.Error().Any("response", res).Msg("Failure when sending message to BlueBubbles")

return nil, errors.New("could not send message")

}

return &imessage.SendResponse{
GUID: res.Data.GUID,
Service: res.Data.Handle.Service,
Time: time.Unix(0, res.Data.DateCreated*int64(time.Millisecond)),
}, nil
}

func (bb *blueBubbles) SendFileCleanup(sendFileDir string) {
_ = os.RemoveAll(sendFileDir)
}
Expand Down
17 changes: 17 additions & 0 deletions imessage/bluebubbles/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ type SendTextResponse struct {
Error interface{} `json:"error,omitempty"`
}

type SendMultipartMessageRequest struct {
ChatGUID string `json:"chatGuid"`
SelectedMessageGuid string `json:"selectedMessageGuid,omitempty"`
PartIndex *int `json:"partIndex,omitempty"`
Parts []MultipartMessagePart `json:"parts"`
}

type MultipartMessagePart struct {
PartIndex int `json:"partIndex"`
// (Optional) the text to use as a message
Text string `json:"text,omitempty"`
// (Optional) UUID of the previously uploaded attachment to link to
Attachment string `json:"attachment,omitempty"`
// (Optional) the name of the previously uploaded attachment to link to
Name string `json:"name,omitempty"`
}

type SendReactionRequest struct {
ChatGUID string `json:"chatGuid"`
Reaction string `json:"reaction"`
Expand Down

0 comments on commit b6b94a7

Please sign in to comment.