Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(message): add missing inline & markup builders #1148

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions telegram/message/inline/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type ResultBuilder struct {
// If passed, clients will display a button with specified text that switches the user to
// a private chat with the bot and sends the bot a start message with a certain parameter.
switchPm tg.InlineBotSwitchPM
// If passed, clients will display a button on top of the remaining inline result list
// with the specified text, that switches the user to the specified bot web app.
switchWebview tg.InlineBotWebView
}

// New creates new ResultBuilder.
Expand Down Expand Up @@ -84,6 +87,18 @@ func (r *ResultBuilder) SwitchPM(text, startParam string) *ResultBuilder {
return r
}

// SwitchWebview sets SwitchWebview field.
//
// If passed, clients will display a button on top of the remaining inline result list
// with the specified text, that switches the user to the specified bot web app.
func (r *ResultBuilder) SwitchWebview(text, url string) *ResultBuilder {
r.switchWebview = tg.InlineBotWebView{
Text: text,
URL: url,
}
return r
}

// Set sets inline results for given query.
func (r *ResultBuilder) Set(ctx context.Context, opts ...ResultOption) (bool, error) {
res := resultPageBuilder{
Expand All @@ -98,13 +113,14 @@ func (r *ResultBuilder) Set(ctx context.Context, opts ...ResultOption) (bool, er
}

ok, err := r.raw.MessagesSetInlineBotResults(ctx, &tg.MessagesSetInlineBotResultsRequest{
Private: r.private,
QueryID: r.queryID,
Results: res.results,
CacheTime: r.cacheTime,
NextOffset: r.nextOffset,
SwitchPm: r.switchPm,
Gallery: r.gallery,
Private: r.private,
QueryID: r.queryID,
Results: res.results,
CacheTime: r.cacheTime,
NextOffset: r.nextOffset,
SwitchPm: r.switchPm,
Gallery: r.gallery,
SwitchWebview: r.switchWebview,
})
if err != nil {
return false, errors.Wrap(err, "set inline results")
Expand Down
61 changes: 61 additions & 0 deletions telegram/message/markup/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,20 @@ func Buy(text string) *tg.KeyboardButtonBuy {
}
}

// InputURLAuth creates button to request a user to authorize via URL using Seamless Telegram Login.
// Can only be sent or received as part of an inline keyboard, use URLAuth for reply keyboards.
func InputURLAuth(requestWriteAccess bool, text, fwdText, url string, bot tg.InputUserClass) *tg.InputKeyboardButtonURLAuth {
return &tg.InputKeyboardButtonURLAuth{
RequestWriteAccess: requestWriteAccess,
Text: text,
FwdText: fwdText,
URL: url,
Bot: bot,
}
}

// URLAuth creates button to request a user to authorize via URL using Seamless Telegram Login.
// Can only be sent or received as part of a reply keyboard, use InputURLAuth for inline keyboards.
func URLAuth(text, url string, buttonID int, fwdText string) *tg.KeyboardButtonURLAuth {
return &tg.KeyboardButtonURLAuth{
Text: text,
Expand All @@ -92,3 +105,51 @@ func RequestPoll(text string, quiz bool) *tg.KeyboardButtonRequestPoll {
Quiz: quiz,
}
}

// InputUserProfile creates button that links directly to a user profile.
// Can only be sent or received as part of an inline keyboard, use UserProfile for reply keyboards.
func InputUserProfile(text string, user tg.InputUserClass) *tg.InputKeyboardButtonUserProfile {
return &tg.InputKeyboardButtonUserProfile{
Text: text,
UserID: user,
}
}

// UserProfile creates button that links directly to a user profile.
// Can only be sent or received as part of a reply keyboard, use InputUserProfile for inline keyboards.
func UserProfile(text string, userID int64) *tg.KeyboardButtonUserProfile {
return &tg.KeyboardButtonUserProfile{
Text: text,
UserID: userID,
}
}

// WebView creates button to open a bot web app using messages.requestWebView, sending over user information after
// user confirmation.
// Can only be sent or received as part of an inline keyboard, use SimpleWebView for reply keyboards.
func WebView(text, url string) *tg.KeyboardButtonWebView {
return &tg.KeyboardButtonWebView{
Text: text,
URL: url,
}
}

// SimpleWebView creates button to open a bot web app using messages.requestSimpleWebView, without sending user
// information to the web app.
// Can only be sent or received as part of a reply keyboard, use WebView for inline keyboards.
func SimpleWebView(text, url string) *tg.KeyboardButtonSimpleWebView {
return &tg.KeyboardButtonSimpleWebView{
Text: text,
URL: url,
}
}

// RequestPeer creates button that prompts the user to select and share a peer with the bot using
// messages.sendBotRequestedPeer.
func RequestPeer(text string, buttonID int, peerType tg.RequestPeerTypeClass) *tg.KeyboardButtonRequestPeer {
return &tg.KeyboardButtonRequestPeer{
Text: text,
ButtonID: buttonID,
PeerType: peerType,
}
}
4 changes: 3 additions & 1 deletion telegram/message/markup/inline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ func TestInlineRow(t *testing.T) {
SwitchInline("inline", "query", true),
Game("game"),
Buy("buy"),
URLAuth("text", "url", 1, "fwd"),
InputURLAuth(false, "text", "fwdText", "url", &tg.InputUserSelf{}),
RequestPoll("poll", true),
InputUserProfile("me", &tg.InputUserSelf{}),
WebView("demo", "https://webappcontent.telegram.org/demo"),
}

v, ok := InlineRow(buttons...).(*tg.ReplyInlineMarkup)
Expand Down
3 changes: 3 additions & 0 deletions telegram/message/markup/keyboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func TestSingleRow(t *testing.T) {
Buy("buy"),
URLAuth("text", "url", 1, "fwd"),
RequestPoll("poll", true),
UserProfile("BotFather", 93372553),
SimpleWebView("demo", "https://webappcontent.telegram.org/demo"),
RequestPeer("peer", 0, &tg.RequestPeerTypeUser{}),
}

v, ok := SingleRow(buttons...).(*tg.ReplyKeyboardMarkup)
Expand Down
Loading