New names for doc trigger

This commit is contained in:
Nefrace 2023-06-07 21:21:54 +03:00
parent 402fb6f4c7
commit 1ce64520e1
2 changed files with 33 additions and 21 deletions

View File

@ -3,6 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"math/rand" "math/rand"
"net/http" "net/http"
"net/url" "net/url"
@ -13,7 +14,8 @@ import (
"github.com/NicoNex/echotron/v3" "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" var docURL = "https://docs.godotengine.org/ru/stable/search.html?q=%s"
type DocResponse struct { type DocResponse struct {
@ -34,34 +36,22 @@ type DocHighlights struct {
Title []string `json:"title"` Title []string `json:"title"`
} }
func handleDocRequest(u *nechotron.Update) error { func getDocs(topic string) (string, error) {
topic := u.Ctx.Value(nechotron.FilteredValue("docTopic")).(string)
topic_escaped := nechotron.EscapeMd2(topic) topic_escaped := nechotron.EscapeMd2(topic)
not_found := fmt.Sprintf("Извините, по запросу *%s* ничего не найдено.", topic_escaped)
kb := nechotron.NewInlineKeyboard(). req, err := url.ParseRequestURI(fmt.Sprintf(docApiURL40, url.QueryEscape(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()
req, err := url.ParseRequestURI(fmt.Sprintf(docApiURL, url.QueryEscape(topic)))
if err != nil { if err != nil {
return err return not_found, err
} }
result, err := http.Get(req.String()) result, err := http.Get(req.String())
if err != nil { if err != nil {
u.AnswerText(fmt.Sprintf("Извините, по запросу *%s* ничего не найдено.", topic_escaped), opts) return not_found, err
return err
} }
defer result.Body.Close() defer result.Body.Close()
var response DocResponse var response DocResponse
err = json.NewDecoder(result.Body).Decode(&response) err = json.NewDecoder(result.Body).Decode(&response)
if err != nil { if err != nil {
u.AnswerText(fmt.Sprintf("Извините, по запросу *%s* ничего не найдено.", topic_escaped), opts) return not_found, err
return err
} }
textResults := "" textResults := ""
for i, r := range response.Results { 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) 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*: \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 return err
} }

View File

@ -7,7 +7,8 @@ import (
var MainState = neco.State{ var MainState = neco.State{
Fn: func(u *neco.Update) error { Fn: func(u *neco.Update) error {
callbackDispatcher := neco.NewDispatcher(). callbackDispatcher := neco.NewDispatcher().
HandleCallback("delete", handleDeleteCallback) HandleCallback(neco.CallbackExact("delete"), handleDeleteCallback)
// HandleCallback(neco.CallbackPrefix("docs3"), handleDocRequest3)
adminOnly := neco.NewDispatcher(). adminOnly := neco.NewDispatcher().
HandleFilter(neco.IsUserAdmin, callbackDispatcher.Run) HandleFilter(neco.IsUserAdmin, callbackDispatcher.Run)
mainCommands := neco.NewDispatcher(). mainCommands := neco.NewDispatcher().