From df4cf8089a09b66502c161fa85f81e0b9ed80493 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Thu, 31 Mar 2016 10:23:44 +0100 Subject: [PATCH] MSPA: Use a notification pool --- mspa/main.go | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/mspa/main.go b/mspa/main.go index 739a4c6..1c8e109 100644 --- a/mspa/main.go +++ b/mspa/main.go @@ -18,20 +18,30 @@ func assert(err error) { } } -var chatID *int var lastPage string var broker *tg.Broker -var apichat *tg.APIChat +var apichat map[int]*tg.APIChat + +const devid = 75510391 const upd8url = "http://tjb0607.me/upd8/check.js" func process(b *tg.Broker, update tg.APIMessage) { - if update.Chat.ChatID == *chatID && *(update.Text) == "!starths" { + if broker == nil { broker = b - apichat = update.Chat - broker.SendTextMessage(apichat, "Ok! Aspetto updates..", &update.MessageID) } - if update.Chat.ChatID == *chatID && *(update.Text) == "!tesths" { + if update.Text == nil { + return + } + if *(update.Text) == "!starths" { + apichat[update.Chat.ChatID] = update.Chat + broker.SendTextMessage(update.Chat, "Ok! Cercherò di notificarti di eventuali updates..", &update.MessageID) + } + if *(update.Text) == "!stophs" { + delete(apichat, update.Chat.ChatID) + broker.SendTextMessage(update.Chat, "Ok! Non ti notificherò più", &update.MessageID) + } + if *(update.Text) == "!tesths" { isok, data := getupd8("showupd8(\"009309\", \"[S] ACT 6 ACT 6 ACT 5\", 12);\r\n") if isok { advert(data) @@ -42,9 +52,17 @@ func process(b *tg.Broker, update tg.APIMessage) { } func advert(upd8 Upd8) { - pageint := strings.TrimLeft(upd8.Page, "0") - text := "Nuova pagina uscita!\n\n#" + pageint + "\n" + upd8.Title + "\n\nClicca qui per leggerla" - broker.SendTextMessage(apichat, text, nil) + if broker != nil { + pageint := strings.TrimLeft(upd8.Page, "0") + msg := "È uscita una nuova pagina!" + if upd8.Count > 1 { + msg = "Sono uscite " + strconv.Itoa(upd8.Count) + " pagine!" + } + text := "" + msg + "\n\n#" + pageint + "\n" + upd8.Title + "\n\nClicca qui per leggerla" + for _, v := range apichat { + broker.SendTextMessage(v, text, nil) + } + } } type Upd8 struct { @@ -87,7 +105,7 @@ func getupd8(body string) (isupd8 bool, out Upd8) { func startPolling(delay int64) { panicFn := func(err error) { - broker.SendTextMessage(apichat, "Errore cercando di leggere l'RSS! @HAMCHA LEGGI LA CONSOLE!", nil) + broker.SendTextMessage(&tg.APIChat{ChatID: devid}, "Errore cercando di leggere l'update feed! @HAMCHA LEGGI LA CONSOLE!", nil) panic(err) } for broker == nil { @@ -114,9 +132,9 @@ func startPolling(delay int64) { func main() { brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port") delay := flag.Int64("delau", 10, "How many seconds between each poll") - chatID = flag.Int("chatid", -68373985, "Telegram Chat ID to count stats for") flag.Parse() lastPage = "" + apichat = make(map[int]*tg.APIChat) go startPolling(*delay)