Added basic commands in dispatcher

This commit is contained in:
nefrace 2023-01-22 23:53:20 +03:00
parent 24fa86f4c9
commit 8d4523efd7
3 changed files with 36 additions and 29 deletions

28
commands.go Normal file
View File

@ -0,0 +1,28 @@
package main
import (
"fmt"
"git.nefrace.ru/nefrace/nechotron"
)
var commandMe = nechotron.NewCommand("me", false)
func handleMe(u *nechotron.UpdateCommand) error {
_, err := u.AnswerMarkdown(fmt.Sprintf("_*%s* %s_", nechotron.EscapeMd2(u.From().FirstName), nechotron.EscapeMd2(u.Param)))
return err
}
var commandHelp = nechotron.NewCommand("help", false)
func handleHelp(u *nechotron.UpdateCommand) error {
_, err := u.AnswerPlain("Тут текст помощи")
return err
}
var commandSay = nechotron.NewCommand("say", true)
func handleSay(u *nechotron.UpdateCommand) error {
_, err := u.AnswerMarkdown(fmt.Sprintf("*_%s_*", nechotron.EscapeMd2(u.Param)))
return err
}

View File

@ -8,6 +8,6 @@ import (
func main() {
neko := nechotron.NewTron("1698825178:AAHy4Er7z768CmYy9M5drJOMQO2S-QnwdTI", &MainState)
neko.ApiServer = "http://192.168.100.15:7373"
// neko.ApiServer = "http://192.168.100.15:7373"
log.Fatal(neko.DispatchPoll())
}

View File

@ -1,37 +1,16 @@
package main
import (
"log"
neco "git.nefrace.ru/nefrace/nechotron"
"github.com/NicoNex/echotron/v3"
)
var MainState = neco.State{
Fn: func(u *neco.Update) (neco.Runnable, error) {
log.Println(u.Message.ID)
if len(u.Message.Photo) > 0 {
f, err := u.Bot.GetFile(u.Message.Photo[len(u.Message.Photo)-1].FileID)
if err != nil {
log.Println(err)
return nil, err
}
log.Println(f.Result.FileID, f.Result.FilePath)
dl, err := u.Bot.DownloadFile(f.Result.FilePath)
if err != nil {
log.Println(err)
return nil, err
}
// log.Println(string(dl))
_, err = u.Bot.SendPhoto(echotron.NewInputFileBytes("newfile.jpg", dl), u.ChatID(), &echotron.PhotoOptions{})
res, err := u.Bot.SendPhoto(echotron.NewInputFilePath("TEST.png"), u.ChatID(), &echotron.PhotoOptions{})
if err != nil {
log.Println(err)
return nil, err
}
log.Println(res.Result.Photo[len(res.Result.Photo)-1].FileID)
return nil, err
}
return nil, nil
Fn: func(u *neco.Update) error {
disp := neco.NewDispatcher().
HandleCommand(commandMe, handleMe).
HandleCommand(commandHelp, handleHelp).
HandleCommand(commandSay, handleSay)
return disp.Run(u)
},
}