forked from sjurtf/tibber-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.go
executable file
·53 lines (47 loc) · 1.4 KB
/
push.go
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
package tibber
import (
"context"
"github.com/machinebox/graphql"
log "github.com/sirupsen/logrus"
)
// PushResponse respons from notification api
type PushResponse struct {
SendPushNotification SendPushNotification `json:"sendPushNotification"`
}
// SendPushNotification data in push response
type SendPushNotification struct {
Successful bool `json:"successful"`
PushedToNumberOfDevices int `json:"pushedToNumberOfDevices"`
}
// PushInput push message
type PushInput struct {
Title string `json:"title"`
Message string `json:"message"`
ScreenToOpen string `json:"screenToOpen"`
}
// SendPushNotification from tibber app
func (t *Client) SendPushNotification(title, msg string) (int, error) {
req := graphql.NewRequest(`
mutation sendPushNotification($input: PushNotificationInput!){
sendPushNotification(input: $input){
successful
pushedToNumberOfDevices
}
}`)
input := PushInput{
Title: title,
Message: msg,
ScreenToOpen: "CONSUMPTION",
}
req.Var("input", input)
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Authorization", "Bearer "+t.Token)
ctx := context.Background()
//ctx, _ := context.WithTimeout(context.Background(), time.Second*2)
var result PushResponse
if err := t.gqlClient.Run(ctx, req, &result); err != nil {
log.Error(err)
return 0, err
}
return result.SendPushNotification.PushedToNumberOfDevices, nil
}