package main import ( "flag" "fmt" "io/ioutil" "net/http" "strconv" "strings" "time" "github.com/hamcha/clessy/tg" ) func assert(err error) { if err != nil { panic(err) } } var chatID *int var lastPage string var broker *tg.Broker var apichat *tg.APIChat const upd8url = "http://tjb0607.me/upd8/check.js" func process(b *tg.Broker, update tg.APIMessage) { if update.Chat.ChatID == *chatID && *(update.Text) == "!starths" { broker = b apichat = update.Chat broker.SendTextMessage(apichat, "Ok! Aspetto updates..", &update.MessageID) } if update.Chat.ChatID == *chatID && *(update.Text) == "!tesths" { isok, data := getupd8("showupd8(\"009309\", \"[S] ACT 6 ACT 6 ACT 5\", 12);\r\n") if isok { advert(data) } else { broker.SendTextMessage(update.Chat, "Test fallito :/", &update.MessageID) } } } 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) } type Upd8 struct { IsUpd8 bool Page string Title string Count int } func getupd8(body string) (isupd8 bool, out Upd8) { parts := strings.Split(body, "(") if parts[0] == "shownoupd8" { isupd8 = false return } isupd8 = true if len(parts) < 2 { fmt.Println("Invalid reply from server: " + body) isupd8 = false return } args := strings.Split(parts[1], ",") if len(args) < 3 { fmt.Println("Invalid reply from server: " + body) isupd8 = false return } out.Page = strings.Trim(args[0], "\" ") out.Title = strings.Trim(args[1], "\" ") pagestr := strings.Trim(args[2], " );\r\n") var err error out.Count, err = strconv.Atoi(pagestr) if err != nil { fmt.Println("Invalid page count: " + pagestr) isupd8 = false return } return } func startPolling(delay int64) { panicFn := func(err error) { broker.SendTextMessage(apichat, "Errore cercando di leggere l'RSS! @HAMCHA LEGGI LA CONSOLE!", nil) panic(err) } for broker == nil { // Wait for broker time.Sleep(time.Second) } for { resp, err := http.Get(upd8url + "?" + strconv.FormatInt(time.Now().Unix(), 10)) if err != nil { panicFn(err) } data, err := ioutil.ReadAll(resp.Body) if err != nil { panicFn(err) } isupd8, upd8data := getupd8(string(data)) if isupd8 { advert(upd8data) } time.Sleep(time.Second * time.Duration(delay)) } } 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 = "" go startPolling(*delay) err := tg.CreateBrokerClient(*brokerAddr, process) assert(err) }