Library for telegram bots written in pure go
Fetch with
go get -u github.com/NicoNex/echotron
A very simple implementation:
package main
import "github.com/NicoNex/echotron"
type bot struct {
chatId int64
echotron.Api
}
func newBot(api echotron.Api, chatId int64) echotron.Bot {
return &bot{
chatId,
api,
}
}
func (b *bot) Update(update *echotron.Update) {
if update.Message.Text == "/start" {
b.SendMessage("Hello world", b.chatId)
}
}
func main() {
dsp := echotron.NewDispatcher("TELEGRAM TOKEN", newBot)
dsp.Run()
}
Also proof of concept with self destruction for low ram usage
package main
import "github.com/NicoNex/echotron"
type bot struct {
chatId int64
echotron.Api
}
var dsp echotron.Dispatcher
func newBot(api echotron.Api, chatId int64) echotron.Bot {
var bot = &bot{
chatId,
api,
}
echotron.AddTimer(bot.chatId, "selfDestruct", bot.selfDestruct, 60)
return bot
}
func (b *bot) selfDestruct() {
b.SendMessage("goodbye", b.chatId)
echotron.DelTimer(b.chatId, "selfDestruct")
dsp.DelSession(b.chatId)
}
func (b *bot) Update(update *echotron.Update) {
if update.Message.Text == "/start" {
b.SendMessage("Hello world", b.chatId)
}
}
func main() {
dsp = echotron.NewDispatcher("TELEGRAM TOKEN", newBot)
dsp.Run()
}