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 (
"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
}

View File

@ -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().