From 1ce64520e1bb4ac040888345ae6f1e21714fa2d8 Mon Sep 17 00:00:00 2001 From: Nefrace Date: Wed, 7 Jun 2023 21:21:54 +0300 Subject: [PATCH] New names for doc trigger --- handlers.go | 51 +++++++++++++++++++++++++++++++-------------------- states.go | 3 ++- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/handlers.go b/handlers.go index 431c103..09240d3 100644 --- a/handlers.go +++ b/handlers.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "log" "math/rand" "net/http" "net/url" @@ -13,7 +14,8 @@ import ( "github.com/NicoNex/echotron/v3" ) -var docApiURL = "https://docs.godotengine.org/_/api/v2/search/?q=%s&project=godot-ru&version=stable&language=ru" +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 docURL = "https://docs.godotengine.org/ru/stable/search.html?q=%s" type DocResponse struct { @@ -34,34 +36,22 @@ type DocHighlights struct { Title []string `json:"title"` } -func handleDocRequest(u *nechotron.Update) error { - topic := u.Ctx.Value(nechotron.FilteredValue("docTopic")).(string) +func getDocs(topic string) (string, error) { topic_escaped := nechotron.EscapeMd2(topic) - - kb := nechotron.NewInlineKeyboard(). - Row(nechotron.InButtonURL("Поищу сам", fmt.Sprintf(docURL, url.QueryEscape(topic)))). - Row(nechotron.InButtonCallback("Спасибо, не надо", "delete")) - opts := nechotron.NewOptions(). - MarkdownV2(). - ReplyTo(u.MessageID()). - ReplyMarkup(kb.Markup()). - MessageOptions() - - req, err := url.ParseRequestURI(fmt.Sprintf(docApiURL, url.QueryEscape(topic))) + not_found := fmt.Sprintf("Извините, по запросу *%s* ничего не найдено.", topic_escaped) + req, err := url.ParseRequestURI(fmt.Sprintf(docApiURL40, url.QueryEscape(topic))) if err != nil { - return err + return not_found, err } result, err := http.Get(req.String()) if err != nil { - u.AnswerText(fmt.Sprintf("Извините, по запросу *%s* ничего не найдено.", topic_escaped), opts) - return err + return not_found, err } defer result.Body.Close() var response DocResponse err = json.NewDecoder(result.Body).Decode(&response) if err != nil { - u.AnswerText(fmt.Sprintf("Извините, по запросу *%s* ничего не найдено.", topic_escaped), opts) - return err + return not_found, err } textResults := "" for i, r := range response.Results { @@ -73,7 +63,28 @@ func handleDocRequest(u *nechotron.Update) error { textResults += fmt.Sprintf("%d\\. [%s](%s)\n", i+1, text, link) } text := fmt.Sprintf("Вот что я нашла по запросу *%s*: \n\n%s", topic_escaped, textResults) - _, err = u.AnswerText(text, opts) + return text, nil +} + +func handleDocRequest(u *nechotron.Update) error { + topic := u.Ctx.Value(nechotron.FilteredValue("docTopic")).(string) + kb := nechotron.NewInlineKeyboard(). + Row( + nechotron.InButtonCallback("Дай для 3.5", fmt.Sprintf("docs3:%s", topic)), + nechotron.InButtonCallback("А на русском можно?", fmt.Sprintf("docs3:%s", topic))). + Row(nechotron.InButtonURL("Поищу сам", fmt.Sprintf(docURL, url.QueryEscape(topic)))). + Row(nechotron.InButtonCallback("Спасибо, не надо", "delete")) + opts := nechotron.NewOptions(). + MarkdownV2(). + ReplyTo(u.MessageID()). + ReplyMarkup(kb.Markup()). + MessageOptions() + + text, docerr := getDocs(topic) + if docerr != nil { + log.Println("Can't get docs: ", docerr) + } + _, err := u.AnswerText(text, opts) return err } diff --git a/states.go b/states.go index b524299..70c09c5 100644 --- a/states.go +++ b/states.go @@ -7,7 +7,8 @@ import ( var MainState = neco.State{ Fn: func(u *neco.Update) error { callbackDispatcher := neco.NewDispatcher(). - HandleCallback("delete", handleDeleteCallback) + HandleCallback(neco.CallbackExact("delete"), handleDeleteCallback) + // HandleCallback(neco.CallbackPrefix("docs3"), handleDocRequest3) adminOnly := neco.NewDispatcher(). HandleFilter(neco.IsUserAdmin, callbackDispatcher.Run) mainCommands := neco.NewDispatcher().