Working notifcation but crash if empty

botanswer
AnisB 4 years ago
parent f1050fd77f
commit fdcdd29e5a

@ -14,6 +14,17 @@ watch: the watch command keeps running and polling the notifications endpoint, i
for testing purposes, create two accounts on TESTNET version of hodlhodl https://hhtestnet.com/ , create a fake listing with one account, and accept offer with the other account and send messages to generate notifications for the first account.
## NOTES
- Logger working : setup in main funciton
- readconfig function : using viper lib, parse config.yml
- startclient function : Working XMPP client with
- startmessage function : send message
- gethdlnotification : send the correct string without crasing the app -----> in progress

@ -18,6 +18,7 @@ import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
"strings"
"reflect"
"strconv"
"path"
"encoding/xml"
@ -161,20 +162,21 @@ func startClient(config *config) {
// ====================
// Start working
currentContact := strings.Split(config.Contacts, configContactSep)[0]
currentContact := strings.Split(config.Contacts, configContactSep)[1]
fmt.Println(infoFormat+"Now sending messages to "+currentContact+" in a private conversation\n")
CorrespChan <- currentContact
fmt.Println("currentContacts", currentContact)
startMessaging(client, config)
notifications := gethdlNotif()
startMessaging(client, config, notifications)
}
func startMessaging(client xmpp.Sender, config *config) {
func startMessaging(client xmpp.Sender, config *config, notification string) {
fmt.Println("START MESSAGING")
var text string
var correspondent string
text := notification
for {
select {
case err := <-killChan:
@ -239,9 +241,12 @@ func handleMessage(s xmpp.Sender, p stanza.Packet) {
_ = s.Send(reply)
}
func (n Notification ) IsEmpty() bool {
return reflect.DeepEqual(Notification{}, n)
}
func gethdlNotif() {
func gethdlNotif() string {
fmt.Println("get notif")
req, err := http.NewRequest("POST", TestAPIEndpoint + "/notifications/read", nil)
if err != nil {
@ -262,18 +267,35 @@ func gethdlNotif() {
} else {
res:= Notification{}
json.Unmarshal([]byte(body), &res)
fmt.Println("RESULT", res)
fmt.Println("RESULTAT", string(body))
//fmt.Println("RESULT title", res.Notifications[0].Title)
//fmt.Println("RESULT body", res.Notifications[0].Body)
fmt.Println("IS EMPTY: ", res.IsEmpty)
fmt.Println("res: ", res)
fmt.Println("res.Notifications: ", res.Notifications)
if (res.Notifications != nil) {
fmt.Println("type of notif = ", reflect.TypeOf(res.Notifications[0]))
fmt.Println("Join: ", strings.Join([]string{res.Notifications[0].Title, res.Notifications[0].Body}, " "))
//fmt.Println("RESULT stirng(body)", string(body))
//fmt.Println("RESULTAT", string(body))
notif := strings.Join([]string{res.Notifications[0].Title, res.Notifications[0].Body}, " ")
return notif
} else {
fmt.Println("No notifications")
notif := "No notifications"
return notif
}
}
defer resp.Body.Close()
return "err"
}
func readConfig() *config {
viper.SetConfigName(configFileName) // name of config file (without extension)
viper.BindPFlags(pflag.CommandLine)

Loading…
Cancel
Save