Skip to content

Commit

Permalink
standardized SendMessage and implemented SendReadReceipt
Browse files Browse the repository at this point in the history
  • Loading branch information
trek-boldly-go committed Jan 16, 2024
1 parent c524e97 commit dc782aa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 39 deletions.
75 changes: 36 additions & 39 deletions imessage/bluebubbles/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions imessage/bluebubbles/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}

0 comments on commit dc782aa

Please sign in to comment.