package main import ( "fmt" tb "gopkg.in/tucnak/telebot.v2" "regexp" "time" ) var ( b *tb.Bot ) func StartBot() { // Registering bot b, err := tb.NewBot(tb.Settings{ Token: cfg.Telegram.Token, URL: cfg.Telegram.URL, Poller: &tb.LongPoller{Timeout: 10 * time.Second}, }) failOnError(err, "StartBot") b.Handle("/hello", func(m *tb.Message) { s, err := botHello(m) logOnError(err, "/hello") if err == nil { b.Send(m.Sender, s) } }) b.Handle("/msg_rescan", func(m *tb.Message) { s, err := botMsgRescan(m) logOnError(err, "/msg_rescan") if err == nil { b.Send(m.Sender, s) } }) b.Handle(tb.OnPhoto, botPhoto) b.Handle(tb.OnChannelPost, botChannelPost) b.Handle(tb.OnQuery, botQuery) b.Handle(tb.OnText, botText) b.Start() } func botPhoto(m *tb.Message) { fmt.Println("botPhoto :", m.Text) // photos only } func botHello(m *tb.Message) (string, error) { fmt.Println("botHello :", m.Text) if !m.Private() { fmt.Println("botHello : !m.Private()") return ``, nil } // fmt.Println("Hello payload :", m.Payload) // PrintText(m) return `hello world`, nil } func botChannelPost(m *tb.Message) { fmt.Println("botChannelPost :", m.Text) PrintText(m) // channel posts only } func botQuery(q *tb.Query) { fmt.Println("botQuery") // incoming inline queries } func botText(m *tb.Message) { fmt.Println("botText :", m.Text) PrintText(m) // all the text messages that weren't // captured by existing handlers } func botMsgRescan(m *tb.Message) (string, error) { fmt.Println("botRescanMsg :", m.Text) if !m.Private() { fmt.Println("botRescanMsg : !m.Private()") return ``, nil } r := regexp.MustCompile("^[0-9]+$") if r.MatchString(m.Payload) { return "Rescaning msg #" + m.Payload, nil } r = regexp.MustCompile("^all$") if r.MatchString(m.Payload) { return "Rescaning all msg", nil } return `rescaning`, nil }