fix notification spam

botanswer
AnisB 4 years ago
parent 2c2032237c
commit 426bb24d88

@ -22,13 +22,15 @@ import (
"strconv"
"path"
"encoding/xml"
"time"
)
const (
// APIEndpoint = "https://hodlhodl.com/api/v1"
//TestAPIEndpoint = "https://hhtestnet.com/api/v1"
//APIkey = "***REMOVED***"
JobSchedulerInterval = 60 * time.Second
// Config
infoFormat = "====== "
defaultConfigFilePath = "./"
@ -47,7 +49,6 @@ const (
)
var (
CorrespChan = make(chan string, 1)
textChan = make(chan string, 5)
rawTextChan = make(chan string, 5)
killChan = make(chan error, 1)
@ -110,7 +111,6 @@ func main() {
}
startClient(c)
}
func startClient(config *config) {
@ -134,7 +134,6 @@ func startClient(config *config) {
fmt.Println(err.Error())
}
router.HandleFunc("message", handleMessage)
if client, err = xmpp.NewClient(clientCfg, router, errorHandler); err != nil {
log.Panicln(fmt.Sprintf("Could not create a new client ! %s", err))
}
@ -165,58 +164,45 @@ func startClient(config *config) {
// ====================
// Start working
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)
// fmt.Println("CONFIG.HODLHDOL :", config.Hodlhodl[APIKey])
notifications := gethdlNotif(config.Hodlhodl[APIKey], config.Hodlhodl[APIEndPoint])
startMessaging(client, config, notifications)
timer := time.NewTicker(5 * time.Second)
notifications := make(chan string, 100)
go startMessaging(client, config, notifications)
for {
select {
case <- timer.C:
fmt.Println("cheking notifs ...")
notifications <- gethdlNotif(config.Hodlhodl[APIKey], config.Hodlhodl[APIEndPoint])
}
}
}
func startMessaging(client xmpp.Sender, config *config, notification string) {
func startMessaging(client xmpp.Sender, config *config, notifications chan string) {
fmt.Println("START MESSAGING")
var correspondent string
text := notification
currentContact := strings.Split(config.Contacts, configContactSep)[1]
fmt.Println(infoFormat+"Now sending messages to "+currentContact+" in a private conversation\n")
fmt.Println("currentContacts", currentContact)
for {
select {
case err := <-killChan:
if err == disconnectErr {
sc := client.(xmpp.StreamClient)
sc.Disconnect()
} else {
logger.Println(err)
}
return
case text = <-textChan:
fmt.Println("TEST 1")
reply := stanza.Message{Attrs: stanza.Attrs{To: correspondent, Type: stanza.MessageTypeChat}, Body: text}
if logger != nil {
raw, _ := xml.Marshal(reply)
logger.Println(string(raw))
}
err := client.Send(reply)
if err != nil {
fmt.Printf("There was a problem sending the message : %v", reply)
return
}
case text = <-rawTextChan:
fmt.Println("TEST 2")
if logger != nil {
logger.Println(text)
}
err := client.SendRaw(text)
if err != nil {
fmt.Printf("There was a problem sending the message : %v", text)
return
}
case crrsp := <-CorrespChan:
fmt.Println("TEST 3")
correspondent = crrsp
reply := stanza.Message{Attrs: stanza.Attrs{To: correspondent, Type: stanza.MessageTypeChat}, Body: text}
case notif := <-notifications:
// Test if notif is nil or skip this loop
if notif == "" { break ;}
fmt.Println("sending notification through xmpp")
reply := stanza.Message{Attrs: stanza.Attrs{To: currentContact, Type: stanza.MessageTypeChat}, Body: notif}
if logger != nil {
raw, _ := xml.Marshal(reply)
logger.Println(string(raw))
@ -233,18 +219,6 @@ func startMessaging(client xmpp.Sender, config *config, notification string) {
}
}
func handleMessage(s xmpp.Sender, p stanza.Packet) {
msg, ok := p.(stanza.Message)
if !ok {
_, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p)
return
}
_, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From)
reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: msg.Body}
_ = s.Send(reply)
}
func (n Notification ) IsEmpty() bool {
return reflect.DeepEqual(Notification{}, n)
}
@ -264,7 +238,7 @@ func gethdlNotif(APIKey string, APIEndPoint string ) string {
fmt.Println(err)
}
fmt.Println(resp.Status)
//fmt.Println(resp.Status)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
@ -273,26 +247,22 @@ func gethdlNotif(APIKey string, APIEndPoint string ) string {
json.Unmarshal([]byte(body), &res)
//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)
//fmt.Println("res: ", res)
//fmt.Println("res.Notifications: ", res.Notifications)
if (len(res.Notifications) > 0) {
fmt.Println("type of notif = ", reflect.TypeOf(res.Notifications[0]))
//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"
fmt.Println("no notif empty string")
return ""
}

Loading…
Cancel
Save