chirpnest/bot.go

65 lines
1.1 KiB
Go
Raw Normal View History

2019-05-03 05:22:58 +02:00
package main
import (
"time"
"log"
"fmt"
tb "gopkg.in/tucnak/telebot.v2"
)
2019-05-03 05:34:38 +02:00
var (
b *tb.Bot
)
2019-05-03 05:22:58 +02:00
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},
})
if err != nil {
log.Fatal(err)
return
}
2019-05-03 05:33:06 +02:00
b.Handle("/hello", botHello)
2019-05-03 05:38:44 +02:00
b.Handle(tb.OnPhoto, botPhoto)
b.Handle(tb.OnChannelPost, botChannel)
b.Handle(tb.OnQuery, botQuery)
b.Handle(tb.OnText, botText)
2019-05-03 05:22:58 +02:00
b.Start()
2019-05-03 05:33:06 +02:00
}
2019-05-03 05:38:44 +02:00
func botPhoto(m *tb.Message) {
fmt.Println("OnPhoto :", m.Text)
// photos only
}
2019-05-03 05:33:06 +02:00
func botHello(m *tb.Message) {
if !m.Private() {
return
}
// fmt.Println("Hello payload :", m.Payload) // <PAYLOAD>
PrintText(m)
b.Send(m.Sender, "hello world")
2019-05-03 05:38:44 +02:00
}
func botChannelPost(m *tb.Message) {
fmt.Println("OnChannelPost :", m.Text)
PrintText(m)
// channel posts only
}
func botQuery(q *tb.Query) {
fmt.Println("Query ?")
// incoming inline queries
}
func botText(m *tb.Message) {
PrintText(m)
// all the text messages that weren't
// captured by existing handlers
2019-05-03 05:22:58 +02:00
}