/
gopushacs.go
101 lines (84 loc) · 2.21 KB
/
gopushacs.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// By: Indra BW
package main
import (
"encoding/json"
"github.com/andelf/go-curl"
)
// REST API Push structure
type JsonPush struct {
Alert string `json:"alert"`
Title string `json:"title"`
Vibrate bool `json:"vibrate"`
Sound string `json:"sound"`
}
// Callback function for curl data, write to chan
func write_data(ptr []byte, userdata interface{}) bool {
ch, ok := userdata.(chan string)
if ok {
ch <- string(ptr)
return true // ok
} else {
println("ERROR!")
return false
}
return false
}
func main() {
//--- SETUP
key := "APP_KEY"
username := "ACS_USER"
password := "ACS_PASSWORD"
channel := "PUSH_CHANNEL"
cookief := "cookie.txt"
to_ids := "everyone"
message := "MY_MESSAGE"
title := "MY_TITLE"
vibrate := true
sound := "default"
// Inject payload post push data
jsonD := JsonPush{
message,
title,
vibrate,
sound}
jsonB, _ := json.Marshal(jsonD)
// Login post params
postLoginData := "login=" + username + "&password=" + password
// Initialize curl
easy := curl.EasyInit()
defer easy.Cleanup()
// Silently retreive cookie please...
silentTransfer := func(buf []byte, userdata interface{}) bool {
return true
}
//--- LOGIN
easy.Setopt(curl.OPT_URL, "https://api.cloud.appcelerator.com/v1/users/login.json?key="+key)
easy.Setopt(curl.OPT_VERBOSE, false)
easy.Setopt(curl.OPT_COOKIEJAR, cookief)
easy.Setopt(curl.OPT_COOKIEFILE, cookief)
easy.Setopt(curl.OPT_POST, 1)
easy.Setopt(curl.OPT_FOLLOWLOCATION, 1)
easy.Setopt(curl.OPT_TIMEOUT, 60)
easy.Setopt(curl.OPT_POSTFIELDS, postLoginData)
easy.Setopt(curl.OPT_WRITEFUNCTION, silentTransfer)
if err := easy.Perform(); err != nil {
println("ERROR: ", err.Error())
}
postPushData := "channel=" + channel + "&to_ids=" + to_ids + "&payload=" + string(jsonB)
//-- SEND PUSH
easy.Setopt(curl.OPT_URL, "https://api.cloud.appcelerator.com/v1/push_notification/notify.json?key="+key)
easy.Setopt(curl.OPT_POSTFIELDS, postPushData)
easy.Setopt(curl.OPT_WRITEFUNCTION, write_data)
// make channel, print status
ch := make(chan string)
go func(ch chan string) {
for {
data := <-ch
println(data)
}
}(ch)
easy.Setopt(curl.OPT_WRITEDATA, ch)
if err := easy.Perform(); err != nil {
println("ERROR: ", err.Error())
}
}