Skip to content

Commit cadb46a

Browse files
committed
feat: AnswerGuestQuery
messages.setBotGuestChatResult with the InlineQueryResult.toTg machinery; returns the sent inline message id (SentWebAppMessage), mirroring AnswerWebAppQuery. Mapping confirmed against telegram-bot-api 10.1. Hermetic tests; lint clean.
1 parent b49ae8b commit cadb46a

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

answer_guest.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package botapi
2+
3+
import (
4+
"context"
5+
"strconv"
6+
7+
"github.com/gotd/td/tg"
8+
)
9+
10+
// AnswerGuestQuery sets the result of a guest interaction with the bot and sends
11+
// a corresponding message to the chat from which the query originated.
12+
// guestQueryID is the query id received in the update. It returns the sent
13+
// inline message id.
14+
func (b *Bot) AnswerGuestQuery(ctx context.Context, guestQueryID string, result InlineQueryResult) (*SentWebAppMessage, error) {
15+
if result == nil {
16+
return nil, errNilInlineResult()
17+
}
18+
19+
queryID, err := strconv.ParseInt(guestQueryID, 10, 64)
20+
if err != nil {
21+
return nil, &Error{Code: 400, Description: "Bad Request: invalid guest_query_id"}
22+
}
23+
24+
converted, err := result.toTg(ctx, b)
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
res, err := b.raw.MessagesSetBotGuestChatResult(ctx, &tg.MessagesSetBotGuestChatResultRequest{
30+
QueryID: queryID,
31+
Result: converted,
32+
})
33+
if err != nil {
34+
return nil, asAPIError(err)
35+
}
36+
37+
enc, err := encodeInlineMessageID(res)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
return &SentWebAppMessage{InlineMessageID: enc}, nil
43+
}

answer_guest_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package botapi
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/gotd/td/tg"
8+
)
9+
10+
func TestAnswerGuestQuery(t *testing.T) {
11+
mid := &tg.InputBotInlineMessageID64{DCID: 2, OwnerID: 1, ID: 9, AccessHash: 42}
12+
13+
inv := newMockInvoker()
14+
inv.reply(tg.MessagesSetBotGuestChatResultRequestTypeID, mid)
15+
16+
result := &InlineQueryResultArticle{
17+
ID: "r1",
18+
Title: "Title",
19+
InputMessageContent: &InputTextMessageContent{MessageText: "hi"},
20+
}
21+
22+
got, err := newMockBot(inv).AnswerGuestQuery(context.Background(), "12345", result)
23+
if err != nil {
24+
t.Fatalf("AnswerGuestQuery: %v", err)
25+
}
26+
27+
decoded, err := decodeInlineMessageID(got.InlineMessageID)
28+
if err != nil {
29+
t.Fatalf("decode inline message id: %v", err)
30+
}
31+
32+
if decoded.String() != mid.String() {
33+
t.Fatalf("inline message id = %#v, want %#v", decoded, mid)
34+
}
35+
36+
var req tg.MessagesSetBotGuestChatResultRequest
37+
38+
inv.decode(t, tg.MessagesSetBotGuestChatResultRequestTypeID, &req)
39+
40+
if req.QueryID != 12345 {
41+
t.Fatalf("query id = %d", req.QueryID)
42+
}
43+
44+
if _, ok := req.Result.(*tg.InputBotInlineResult); !ok {
45+
t.Fatalf("result = %#v, want inline result", req.Result)
46+
}
47+
}
48+
49+
func TestAnswerGuestQueryInvalid(t *testing.T) {
50+
inv := newMockInvoker()
51+
52+
if _, err := newMockBot(inv).AnswerGuestQuery(context.Background(), "12345", nil); err == nil {
53+
t.Fatal("expected error for nil result")
54+
}
55+
56+
if _, err := newMockBot(inv).AnswerGuestQuery(context.Background(), "notanumber",
57+
&InlineQueryResultArticle{ID: "r1", Title: "T", InputMessageContent: &InputTextMessageContent{MessageText: "x"}}); err == nil {
58+
t.Fatal("expected error for invalid guest_query_id")
59+
}
60+
}

0 commit comments

Comments
 (0)