From dc782aa57f4c586e740edcb41c9db4fe0e745dff Mon Sep 17 00:00:00 2001 From: trek-boldly-go Date: Mon, 15 Jan 2024 18:45:46 -0600 Subject: [PATCH] standardized SendMessage and implemented SendReadReceipt --- imessage/bluebubbles/api.go | 75 +++++++++++++++---------------- imessage/bluebubbles/interface.go | 14 ++++++ 2 files changed, 50 insertions(+), 39 deletions(-) diff --git a/imessage/bluebubbles/api.go b/imessage/bluebubbles/api.go index ae0527f..636521b 100644 --- a/imessage/bluebubbles/api.go +++ b/imessage/bluebubbles/api.go @@ -492,50 +492,37 @@ func (bb *blueBubbles) BackfillTaskChan() <-chan *imessage.BackfillTask { func (bb *blueBubbles) SendMessage(chatID, text string, replyTo string, replyToPart int, richLink *imessage.RichLink, metadata imessage.MessageMetadata) (*imessage.SendResponse, error) { - url := bb.bridge.GetConnectorConfig().BlueBubblesURL + "/api/v1/message/text?password=" + bb.bridge.GetConnectorConfig().BlueBubblesPassword - method := "POST" - - payload := fmt.Sprintf(`{ - "chatGuid": "%s", - "method": "private-api", - "message": "%s" -}`, chatID, text) - - bb.log.Debug().Msg(payload) + request := SendChatRequest{ + ChatGUID: chatID, + Method: "private-api", + Message: text, + SelectedMessageGuid: replyTo, + PartIndex: replyToPart, + } - client := &http.Client{} - req, err := http.NewRequest(method, url, strings.NewReader(payload)) + var res SendChatResponse - // Set Content-Type header to application/json - req.Header.Set("Content-Type", "application/json") + err := bb.apiPost("/api/v1/message/text", request, &res) if err != nil { - fmt.Println(err) - return nil, err - } - res, err := client.Do(req) - if err != nil { - bb.log.Err(err).Msg("Error sending the Message to BlueBubbles") return nil, err } - defer res.Body.Close() - if res.StatusCode != http.StatusOK { - bb.log.Err(fmt.Errorf("unexpected status code: %d", res.StatusCode)).Msg("Error sending the Message to BlueBubbles") - body, _ := io.ReadAll(res.Body) - bb.log.Error().Bytes("response_body", body).Msg("Response from BlueBubbles") - return nil, fmt.Errorf("unexpected status code: %d", res.StatusCode) - } + if res.Status == 200 { + bb.log.Debug().Msg("Sent a message!") - _, err = io.ReadAll(res.Body) - if err != nil { - bb.log.Err(err).Msg("Error reading the SendMessage response body from Bluebubbles") - return nil, err - } + var imessageSendResponse = imessage.SendResponse{ + GUID: res.Data.GUID, + Service: res.Data.Handle.Service, + Time: time.Unix(0, res.Data.DateCreated*int64(time.Millisecond)), + } - bb.log.Debug().Msg("Sent a message!") + return &imessageSendResponse, nil + } else { + bb.log.Error().Any("response", res).Msg("Failure when sending message to BlueBubbles") - return nil, nil + return nil, errors.New("could not send message") + } } func (bb *blueBubbles) SendFile(chatID, text, filename string, pathOnDisk string, replyTo string, replyToPart int, mimeType string, voiceMemo bool, metadata imessage.MessageMetadata) (*imessage.SendResponse, error) { @@ -554,7 +541,17 @@ func (bb *blueBubbles) SendTapback(chatID, targetGUID string, targetPart int, ta func (bb *blueBubbles) SendReadReceipt(chatID, readUpTo string) error { bb.log.Trace().Str("chatID", chatID).Str("readUpTo", readUpTo).Msg("SendReadReceipt") - return ErrNotImplemented + + var res ChatResponse + err := bb.apiPost(fmt.Sprintf("/api/v1/chat/%s/read", chatID), nil, res) + + if err != nil { + return err + } + + bb.log.Debug().Str("chatID", chatID).Msg("Marked a chat as Read") + + return nil } func (bb *blueBubbles) SendTypingNotification(chatID string, typing bool) error { @@ -616,14 +613,14 @@ func (bb *blueBubbles) PostStartupSyncHook() { func (bb *blueBubbles) Capabilities() imessage.ConnectorCapabilities { return imessage.ConnectorCapabilities{ - MessageSendResponses: false, + MessageSendResponses: true, SendTapbacks: false, - SendReadReceipts: false, - SendTypingNotifications: false, + SendReadReceipts: true, + SendTypingNotifications: true, SendCaptions: false, BridgeState: false, MessageStatusCheckpoints: false, - DeliveredStatus: false, + DeliveredStatus: true, ContactChatMerging: false, RichLinks: false, ChatBridgeResult: false, diff --git a/imessage/bluebubbles/interface.go b/imessage/bluebubbles/interface.go index 875df66..e82533e 100644 --- a/imessage/bluebubbles/interface.go +++ b/imessage/bluebubbles/interface.go @@ -129,3 +129,17 @@ type Handle struct { Service string `json:"service,omitempty"` UncanonicalizedId interface{} `json:"uncanonicalizedId,omitempty"` } + +type SendChatRequest struct { + ChatGUID string `json:"chatGuid"` + Method string `json:"method"` + Message string `json:"message"` + SelectedMessageGuid string `json:"selectedMessageGuid"` + PartIndex int `json:"partIndex"` +} + +type SendChatResponse struct { + Status int64 `json:"status"` + Message string `json:"message"` + Data Message `json:data,omitempty` +}