MSPA: Use a notification pool

This commit is contained in:
Hamcha 2016-03-31 10:23:44 +01:00
parent d97a39febd
commit df4cf8089a

View file

@ -18,20 +18,30 @@ func assert(err error) {
} }
} }
var chatID *int
var lastPage string var lastPage string
var broker *tg.Broker 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" const upd8url = "http://tjb0607.me/upd8/check.js"
func process(b *tg.Broker, update tg.APIMessage) { func process(b *tg.Broker, update tg.APIMessage) {
if update.Chat.ChatID == *chatID && *(update.Text) == "!starths" { if broker == nil {
broker = b 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") isok, data := getupd8("showupd8(\"009309\", \"[S] ACT 6 ACT 6 ACT 5\", 12);\r\n")
if isok { if isok {
advert(data) advert(data)
@ -42,9 +52,17 @@ func process(b *tg.Broker, update tg.APIMessage) {
} }
func advert(upd8 Upd8) { func advert(upd8 Upd8) {
if broker != nil {
pageint := strings.TrimLeft(upd8.Page, "0") pageint := strings.TrimLeft(upd8.Page, "0")
text := "<b>Nuova pagina uscita!</b>\n\n#" + pageint + "\n" + upd8.Title + "\n\n<a href=\"http://www.mspaintadventures.com/?s=6&p=" + upd8.Page + "\">Clicca qui per leggerla</a>" msg := "È uscita una nuova pagina!"
broker.SendTextMessage(apichat, text, nil) if upd8.Count > 1 {
msg = "Sono uscite " + strconv.Itoa(upd8.Count) + " pagine!"
}
text := "<b>" + msg + "</b>\n\n#" + pageint + "\n" + upd8.Title + "\n\n<a href=\"http://www.mspaintadventures.com/?s=6&p=" + upd8.Page + "\">Clicca qui per leggerla</a>"
for _, v := range apichat {
broker.SendTextMessage(v, text, nil)
}
}
} }
type Upd8 struct { type Upd8 struct {
@ -87,7 +105,7 @@ func getupd8(body string) (isupd8 bool, out Upd8) {
func startPolling(delay int64) { func startPolling(delay int64) {
panicFn := func(err error) { 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) panic(err)
} }
for broker == nil { for broker == nil {
@ -114,9 +132,9 @@ func startPolling(delay int64) {
func main() { func main() {
brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port") brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port")
delay := flag.Int64("delau", 10, "How many seconds between each poll") 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() flag.Parse()
lastPage = "" lastPage = ""
apichat = make(map[int]*tg.APIChat)
go startPolling(*delay) go startPolling(*delay)