93 lines
1.7 KiB
Go
93 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type garminClient struct {
|
|
c *http.Client
|
|
apiKey string
|
|
}
|
|
|
|
type garminMessageRequest struct {
|
|
Messages []garminMessage `json:"messages"`
|
|
}
|
|
|
|
type garminMessage struct {
|
|
Recipients []string `json:"recipients"`
|
|
Sender string `json:"sender"`
|
|
Timestamp string `json:"timestamp"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
type garminMessageResponse struct {
|
|
Count int `json:"count"`
|
|
}
|
|
|
|
func newGarminClient(apiKey string) *garminClient {
|
|
return &garminClient{
|
|
c: &http.Client{},
|
|
apiKey: apiKey,
|
|
}
|
|
}
|
|
|
|
func (gc *garminClient) sendMessage(imei, sender, msg string) error {
|
|
buf := &bytes.Buffer{}
|
|
err := json.NewEncoder(buf).Encode(garminMessageRequest{
|
|
Messages: []garminMessage{
|
|
{
|
|
Recipients: []string{imei},
|
|
Sender: sender,
|
|
Timestamp: time.Now().UTC().Format("2006-01-02T15:04:05Z"),
|
|
Message: msg,
|
|
},
|
|
},
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Printf("[->garmin] %s", buf.String())
|
|
|
|
req, err := http.NewRequest("POST", "https://ipcinbound.inreachapp.com/IPC/IPCInboundApi/api/Messaging/Message", buf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("x-api-key", gc.apiKey)
|
|
|
|
resp, err := gc.c.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
if resp.StatusCode != 200 {
|
|
return fmt.Errorf("%s", string(body))
|
|
}
|
|
|
|
grResp := garminMessageResponse{}
|
|
err = json.Unmarshal(body, &grResp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Printf("[<-garmin] %s", string(body))
|
|
|
|
if grResp.Count != 1 {
|
|
return fmt.Errorf("expected 1 message, got %d", grResp.Count)
|
|
}
|
|
|
|
return nil
|
|
}
|