From e1be5d24c4b0acb7fff03835a1478765f18c7562 Mon Sep 17 00:00:00 2001 From: Nefrace Date: Thu, 8 Jun 2023 01:44:38 +0300 Subject: [PATCH] Improved dispatchers and doc handler --- handlers.go | 34 +++++++++++++++++++++++++++------- main.go | 1 + states.go | 12 +++++------- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/handlers.go b/handlers.go index 09240d3..90c8b94 100644 --- a/handlers.go +++ b/handlers.go @@ -7,6 +7,7 @@ import ( "math/rand" "net/http" "net/url" + "strings" "time" "git.nefrace.ru/nefrace/nechotron" @@ -14,8 +15,7 @@ import ( "github.com/NicoNex/echotron/v3" ) -var docApiURL40 = "https://docs.godotengine.org/_/api/v2/search/?q=%s&project=godot&version=latest&language=en" -var docApiURL35 = "https://docs.godotengine.org/_/api/v2/search/?q=%s&project=godot&version=3.5&language=en" +var docApiURL = "https://docs.godotengine.org/_/api/v2/search/?q=%s&project=godot&version=%s&language=en" var docURL = "https://docs.godotengine.org/ru/stable/search.html?q=%s" type DocResponse struct { @@ -36,10 +36,10 @@ type DocHighlights struct { Title []string `json:"title"` } -func getDocs(topic string) (string, error) { +func getDocs(topic string, version string) (string, error) { topic_escaped := nechotron.EscapeMd2(topic) not_found := fmt.Sprintf("Извините, по запросу *%s* ничего не найдено.", topic_escaped) - req, err := url.ParseRequestURI(fmt.Sprintf(docApiURL40, url.QueryEscape(topic))) + req, err := url.ParseRequestURI(fmt.Sprintf(docApiURL, url.QueryEscape(topic), version)) if err != nil { return not_found, err } @@ -62,7 +62,7 @@ func getDocs(topic string) (string, error) { link, _ := url.JoinPath(r.Domain, r.Path) textResults += fmt.Sprintf("%d\\. [%s](%s)\n", i+1, text, link) } - text := fmt.Sprintf("Вот что я нашла по запросу *%s*: \n\n%s", topic_escaped, textResults) + text := fmt.Sprintf("Вот что я нашла по запросу *%s* \\(для версии `%s`\\): \n\n%s", topic_escaped, version, textResults) return text, nil } @@ -71,7 +71,7 @@ func handleDocRequest(u *nechotron.Update) error { kb := nechotron.NewInlineKeyboard(). Row( nechotron.InButtonCallback("Дай для 3.5", fmt.Sprintf("docs3:%s", topic)), - nechotron.InButtonCallback("А на русском можно?", fmt.Sprintf("docs3:%s", topic))). + nechotron.InButtonCallback("А на русском можно?", "rudocs")). Row(nechotron.InButtonURL("Поищу сам", fmt.Sprintf(docURL, url.QueryEscape(topic)))). Row(nechotron.InButtonCallback("Спасибо, не надо", "delete")) opts := nechotron.NewOptions(). @@ -80,7 +80,7 @@ func handleDocRequest(u *nechotron.Update) error { ReplyMarkup(kb.Markup()). MessageOptions() - text, docerr := getDocs(topic) + text, docerr := getDocs(topic, "latest") if docerr != nil { log.Println("Can't get docs: ", docerr) } @@ -88,6 +88,26 @@ func handleDocRequest(u *nechotron.Update) error { return err } +func handleDocRequest3(u *nechotron.Update) error { + topic := strings.TrimPrefix(u.Callback(), "docs3:") + kb := nechotron.NewInlineKeyboard(). + Row( + nechotron.InButtonCallback("А на русском можно?", "rudocs")). + Row(nechotron.InButtonURL("Поищу сам", fmt.Sprintf(docURL, url.QueryEscape(topic)))). + Row(nechotron.InButtonCallback("Спасибо, не надо", "delete")) + opts := nechotron.NewOptions(). + MarkdownV2(). + ReplyMarkup(kb.Markup()). + MessageTextOptions() + + text, docerr := getDocs(topic, "3.5") + if docerr != nil { + log.Println("Can't get docs: ", docerr) + } + _, err := u.EditText(text, opts) + return err +} + func handleKarma(u *nechotron.Update) error { from, _ := u.Ctx.Value("userfrom").(*User) to, _ := u.Ctx.Value("userto").(*User) diff --git a/main.go b/main.go index f3c74b6..b6d3893 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ var BuildTime string var db *tongo.Database func main() { + log.Println("Starting Go-Dette") godotenv.Load() var err error db, err = tongo.NewConnection(os.Getenv("MONGO_URI"), "godette") diff --git a/states.go b/states.go index 70c09c5..1d13798 100644 --- a/states.go +++ b/states.go @@ -6,22 +6,20 @@ import ( var MainState = neco.State{ Fn: func(u *neco.Update) error { - callbackDispatcher := neco.NewDispatcher(). - HandleCallback(neco.CallbackExact("delete"), handleDeleteCallback) - // HandleCallback(neco.CallbackPrefix("docs3"), handleDocRequest3) adminOnly := neco.NewDispatcher(). - HandleFilter(neco.IsUserAdmin, callbackDispatcher.Run) + HandleCallback(neco.CallbackExact("delete"), handleDeleteCallback). + HandleCommand(commandSay, handleSay) mainCommands := neco.NewDispatcher(). HandleCommand(commandMe, handleMe). - HandleCommand(commandHelp, handleHelp). - HandleCommand(commandSay, handleSay) + HandleCommand(commandHelp, handleHelp) replyDispatcher := neco.NewDispatcher(). HandleCommand(commandWarn, handleWarn). HandleFilter(karmaTriggers, handleKarma) replies := neco.NewDispatcher(). HandleFilter(neco.IsReply, replyDispatcher.Run) docs := neco.NewDispatcher(). - HandleFilter(docRequest, handleDocRequest) + HandleFilter(docRequest, handleDocRequest). + HandleCallback(neco.CallbackPrefix("docs3"), handleDocRequest3) triggers := neco.NewDispatcher(). HandleFilter(offtopTrigger, handleOfftop) return neco.ChainRun(u, mainCommands, replies, docs, adminOnly, triggers)