Simple Telegram Bot framework use to easily create easy interaction with bots commands.
Below is an interactive demo
dsn = "MYSQL DSN Required"
token = "Your-Awesome-Bot-Token"
root = []
this will create storage for session (Future will support multiple storage type: maybe redis, sqlite, mongodb, etc)
There are simply five steps to go:
- Parse Config file
- Init DB Connection
- Init Bot API
- Register commands
- Run it
Sample file:
package main
import (
"flag"
"time"
"github.com/VOID001/simple-tg-bot/command"
"github.com/VOID001/simple-tg-bot/model"
"github.com/VOID001/simple-tg-bot/module"
_ "github.com/go-sql-driver/mysql"
tg "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)
var configPath string
func init() {
flag.StringVar(&configPath, "c", "config.toml", "Select the config file to use ")
flag.Parse()
}
func main() {
cfg := new(module.Config)
cmdHandler := new(module.CommandHandler)
// Read the config file
err := cfg.Parse(configPath)
// Initialize DB
model.DB, err = sqlx.Connect("mysql", cfg.DSN)
// Initialize bot
bot, err := tg.NewBotAPI(cfg.Token)
cmdHandler.Bot = bot
// Register the Command
YourCommand := new(command.YourCommand)
cmdHandler.Register("yourcommand", YourCommand)
// Run it!
cmdHandler.Run()
}
Then implement the command just create a file and import github.com/VOID001/simple-tg-bot/module/session
And implement the Command Interface
package command
// Demo to show Dialog Basic usage
import (
"fmt"
session "github.com/VOID001/simple-tg-bot/module/session"
tg "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/pkg/errors"
)
// You do not need to contain any export member here
type Ping struct {
msg *tg.Message
sess session.Session
}
func (p *Ping) Init(sess *session.Session, args ...interface{}) {
p.sess = *sess
}
func (p *Ping) Dialog(state int, data string) (reply tg.Chattable, nextState int, err error) {
return
}
func (p *Ping) Info() (reply tg.Chattable, err error) {
return
}
func (p *Ping) Run() (reply tg.Chattable, err error) {
return
}
func (p *Ping) ParseMessage(msg *tg.Message) (errMsg tg.Chattable, err error) {
return
}
func (p *Ping) IsFinalState(state int) (ok bool) {
return
}
go build && ./your-awesome-bot -c config.toml
You can find examples in command/
- [Dialog State Switch & Input example] (https://github.com/VOID001/simple-tg-bot/tree/master/command/demo.go)
- Simple Dialog Example