fix notification spam

botanswer
AnisB 4 years ago
parent 2c2032237c
commit 426bb24d88

@ -22,13 +22,15 @@ import (
"strconv" "strconv"
"path" "path"
"encoding/xml" "encoding/xml"
"time"
) )
const ( const (
// APIEndpoint = "https://hodlhodl.com/api/v1" // APIEndpoint = "https://hodlhodl.com/api/v1"
//TestAPIEndpoint = "https://hhtestnet.com/api/v1" //TestAPIEndpoint = "https://hhtestnet.com/api/v1"
//APIkey = "***REMOVED***" //APIkey = "***REMOVED***"
JobSchedulerInterval = 60 * time.Second
// Config // Config
infoFormat = "====== " infoFormat = "====== "
defaultConfigFilePath = "./" defaultConfigFilePath = "./"
@ -47,7 +49,6 @@ const (
) )
var ( var (
CorrespChan = make(chan string, 1)
textChan = make(chan string, 5) textChan = make(chan string, 5)
rawTextChan = make(chan string, 5) rawTextChan = make(chan string, 5)
killChan = make(chan error, 1) killChan = make(chan error, 1)
@ -110,7 +111,6 @@ func main() {
} }
startClient(c) startClient(c)
} }
func startClient(config *config) { func startClient(config *config) {
@ -134,7 +134,6 @@ func startClient(config *config) {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
router.HandleFunc("message", handleMessage)
if client, err = xmpp.NewClient(clientCfg, router, errorHandler); err != nil { if client, err = xmpp.NewClient(clientCfg, router, errorHandler); err != nil {
log.Panicln(fmt.Sprintf("Could not create a new client ! %s", err)) log.Panicln(fmt.Sprintf("Could not create a new client ! %s", err))
} }
@ -165,58 +164,45 @@ func startClient(config *config) {
// ==================== // ====================
// Start working // 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]) // 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") 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 { for {
select { select {
case err := <-killChan: case notif := <-notifications:
if err == disconnectErr {
sc := client.(xmpp.StreamClient)
sc.Disconnect() // Test if notif is nil or skip this loop
} else { if notif == "" { break ;}
logger.Println(err)
} fmt.Println("sending notification through xmpp")
return
case text = <-textChan: reply := stanza.Message{Attrs: stanza.Attrs{To: currentContact, Type: stanza.MessageTypeChat}, Body: notif}
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}
if logger != nil { if logger != nil {
raw, _ := xml.Marshal(reply) raw, _ := xml.Marshal(reply)
logger.Println(string(raw)) 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 { func (n Notification ) IsEmpty() bool {
return reflect.DeepEqual(Notification{}, n) return reflect.DeepEqual(Notification{}, n)
} }
@ -264,7 +238,7 @@ func gethdlNotif(APIKey string, APIEndPoint string ) string {
fmt.Println(err) fmt.Println(err)
} }
fmt.Println(resp.Status) //fmt.Println(resp.Status)
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -273,26 +247,22 @@ func gethdlNotif(APIKey string, APIEndPoint string ) string {
json.Unmarshal([]byte(body), &res) json.Unmarshal([]byte(body), &res)
//fmt.Println("RESULT title", res.Notifications[0].Title) //fmt.Println("RESULT title", res.Notifications[0].Title)
//fmt.Println("RESULT body", res.Notifications[0].Body) //fmt.Println("RESULT body", res.Notifications[0].Body)
fmt.Println("IS EMPTY: ", res.IsEmpty) //fmt.Println("res: ", res)
fmt.Println("res: ", res) //fmt.Println("res.Notifications: ", res.Notifications)
fmt.Println("res.Notifications: ", res.Notifications)
if (len(res.Notifications) > 0) { 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("Join: ", strings.Join([]string{res.Notifications[0].Title, res.Notifications[0].Body}, " "))
//fmt.Println("RESULT stirng(body)", string(body)) //fmt.Println("RESULT stirng(body)", string(body))
//fmt.Println("RESULTAT", string(body)) //fmt.Println("RESULTAT", string(body))
notif := strings.Join([]string{res.Notifications[0].Title, res.Notifications[0].Body}, " ") notif := strings.Join([]string{res.Notifications[0].Title, res.Notifications[0].Body}, " ")
return notif return notif
} else {
fmt.Println("No notifications")
notif := "No notifications"
return notif
} }
} }
defer resp.Body.Close() defer resp.Body.Close()
return "err" fmt.Println("no notif empty string")
return ""
} }

Loading…
Cancel
Save