go get github.com/sg3des/tebo
Initialize new instance of bot and new connection to telegram server, at this time try to load bot information /getMe
method, read/initialize history.
func main() {
bot, err := tebo.NewBot(token, ".history")
if err != nil {
// unavailable connection to telegram server or failed to initialize a history
}
bot.Handle("/hello", helloHandler)
}
func helloHandler(msg tebo.Message) string {
return fmt.Sprintf("Hello %s", msg.From.Username)
}
Handle commands is like with http handlers:
bot.Handle("/hello", func(msg tebo.Message) string {
//
// your logic
//
return "response"
})
Complex commands routing, with more than one slash, arguments, allowed prefined arguments, or regular expressions:
bot.Handle("/hello/world", ...)
bot.Handle("/alarm 08:00", ...)
bot.Handle("/some-command start|stop", ...)
bot.Handle("/first/\\w [0-9]", ...)
As well as the famous web frameworks, tebo
allowed used middleware functions:
func CheckTrustedUsers(next tebo.HandleFunc, msg tebo.Message) (tebo.HandleFunc, bool) {
if msg.From.Username != "trustedUsername" {
return nil, false
}
return next, true
}
For all handlers:
bot.Pre(CheckTrustedUsers)
For handler:
bot.Handle("/command", handler, CheckTrustedUsers)
tebo
allow to send messages to known users(to exists chat), without any commands from user. It can be convenient for sending notifications, etc.
err := bot.SendMessage(chatid, "some text")
// or send images
err := bot.SendPhoto(chatid, imgReader, "image caption")
Use chatid
in UI may be ugly, chatname
is more prettier. Bot will find the chat name in the saved history and return its ID.
chatid, ok := bot.LookupChatID(chatname)