move gotd fork into repo. (#111)
- update to latest telegram layer - remove some references to fields in tg.Entities that don't exist in the schema - originally added here: https://github.com/beeper/td/commit/820929062a2ba0104397bc01235ab58a9cff780e - referenced here - https://github.com/mautrix/telegramgo/commit/124f0967ed195b5a380c9bd02e170ada9710dde3 - https://github.com/mautrix/telegramgo/commit/4205047aab2e0639217148b5d125bfaab668bd8e
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
package markup
|
||||
|
||||
import "go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
||||
|
||||
// Row creates keyboard row.
|
||||
func Row(buttons ...tg.KeyboardButtonClass) tg.KeyboardButtonRow {
|
||||
return tg.KeyboardButtonRow{
|
||||
Buttons: buttons,
|
||||
}
|
||||
}
|
||||
|
||||
// Button creates new plain text button.
|
||||
func Button(text string) *tg.KeyboardButton {
|
||||
return &tg.KeyboardButton{
|
||||
Text: text,
|
||||
}
|
||||
}
|
||||
|
||||
// URL creates new URL button.
|
||||
func URL(text, url string) *tg.KeyboardButtonURL {
|
||||
return &tg.KeyboardButtonURL{
|
||||
Text: text,
|
||||
URL: url,
|
||||
}
|
||||
}
|
||||
|
||||
// Callback creates new callback button.
|
||||
func Callback(text string, data []byte) *tg.KeyboardButtonCallback {
|
||||
return &tg.KeyboardButtonCallback{
|
||||
Text: text,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
// RequestPhone creates button to request a user's phone number.
|
||||
func RequestPhone(text string) *tg.KeyboardButtonRequestPhone {
|
||||
return &tg.KeyboardButtonRequestPhone{
|
||||
Text: text,
|
||||
}
|
||||
}
|
||||
|
||||
// RequestGeoLocation creates button to request a user's geo location.
|
||||
func RequestGeoLocation(text string) *tg.KeyboardButtonRequestGeoLocation {
|
||||
return &tg.KeyboardButtonRequestGeoLocation{
|
||||
Text: text,
|
||||
}
|
||||
}
|
||||
|
||||
// SwitchInline creates button to force a user to switch to inline mode.
|
||||
// Pressing the button will prompt the user to select one of their chats, open that chat and insert the bot‘s username
|
||||
// and the specified inline query in the input field.
|
||||
//
|
||||
// If samePeer set, pressing the button will insert the bot‘s
|
||||
// username and the specified inline query in the current chat's input field.
|
||||
func SwitchInline(text, query string, samePeer bool) *tg.KeyboardButtonSwitchInline {
|
||||
return &tg.KeyboardButtonSwitchInline{
|
||||
SamePeer: samePeer,
|
||||
Text: text,
|
||||
Query: query,
|
||||
}
|
||||
}
|
||||
|
||||
// Game creates button to start a game.
|
||||
func Game(text string) *tg.KeyboardButtonGame {
|
||||
return &tg.KeyboardButtonGame{
|
||||
Text: text,
|
||||
}
|
||||
}
|
||||
|
||||
// Buy creates button to buy a product.
|
||||
func Buy(text string) *tg.KeyboardButtonBuy {
|
||||
return &tg.KeyboardButtonBuy{
|
||||
Text: text,
|
||||
}
|
||||
}
|
||||
|
||||
// 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,
|
||||
URL: url,
|
||||
ButtonID: buttonID,
|
||||
FwdText: fwdText,
|
||||
}
|
||||
}
|
||||
|
||||
// RequestPoll creates button that allows the user to create and send a poll when pressed.
|
||||
// Available only in private.
|
||||
func RequestPoll(text string, quiz bool) *tg.KeyboardButtonRequestPoll {
|
||||
return &tg.KeyboardButtonRequestPoll{
|
||||
Text: text,
|
||||
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,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// Package markup contain bots inline markup builder.
|
||||
package markup
|
||||
@@ -0,0 +1,11 @@
|
||||
package markup
|
||||
|
||||
import "go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
||||
|
||||
// ForceReply creates markup to force the user to send a reply.
|
||||
func ForceReply(singleUse, selective bool) tg.ReplyMarkupClass {
|
||||
return &tg.ReplyKeyboardForceReply{
|
||||
SingleUse: singleUse,
|
||||
Selective: selective,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package markup
|
||||
|
||||
import "go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
||||
|
||||
// Hide creates markup to hide markup.
|
||||
func Hide() tg.ReplyMarkupClass {
|
||||
return &tg.ReplyKeyboardHide{}
|
||||
}
|
||||
|
||||
// SelectiveHide creates markup to hide markup.
|
||||
// Use this builder if you want to remove the keyboard for specific users only.
|
||||
// Targets:
|
||||
// 1. users that are @mentioned in the text of the Message object;
|
||||
// 2. if the bot's message is a reply (has reply_to_message_id), sender of the original message.
|
||||
//
|
||||
// Example: A user votes in a poll, bot returns confirmation message in reply to the vote
|
||||
// and removes the keyboard for that user, while still showing the keyboard with poll
|
||||
// options to users who haven't voted yet.
|
||||
func SelectiveHide() tg.ReplyMarkupClass {
|
||||
return &tg.ReplyKeyboardHide{
|
||||
Selective: true,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package markup
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
||||
)
|
||||
|
||||
func TestHide(t *testing.T) {
|
||||
v, ok := Hide().(*tg.ReplyKeyboardHide)
|
||||
require.True(t, ok)
|
||||
require.False(t, v.Selective)
|
||||
|
||||
v, ok = SelectiveHide().(*tg.ReplyKeyboardHide)
|
||||
require.True(t, ok)
|
||||
require.True(t, v.Selective)
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package markup
|
||||
|
||||
import "go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
||||
|
||||
// InlineRow creates inline keyboard with single row using given buttons.
|
||||
func InlineRow(buttons ...tg.KeyboardButtonClass) tg.ReplyMarkupClass {
|
||||
return InlineKeyboard(Row(buttons...))
|
||||
}
|
||||
|
||||
// InlineKeyboard creates inline keyboard using given rows.
|
||||
func InlineKeyboard(rows ...tg.KeyboardButtonRow) tg.ReplyMarkupClass {
|
||||
return &tg.ReplyInlineMarkup{
|
||||
Rows: rows,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package markup
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
||||
)
|
||||
|
||||
func TestInlineRow(t *testing.T) {
|
||||
a := require.New(t)
|
||||
|
||||
buttons := []tg.KeyboardButtonClass{
|
||||
Button("gotd"),
|
||||
URL("Google!", "https://google.com?q=gotd"),
|
||||
RequestPhone("phone"),
|
||||
RequestGeoLocation("geo"),
|
||||
SwitchInline("inline", "query", true),
|
||||
Game("game"),
|
||||
Buy("buy"),
|
||||
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)
|
||||
a.True(ok)
|
||||
a.Len(v.Rows, 1)
|
||||
row := v.Rows[0]
|
||||
|
||||
a.Len(row.Buttons, len(buttons))
|
||||
for i, b := range buttons {
|
||||
a.Equal(b, row.Buttons[i])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package markup
|
||||
|
||||
import (
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
||||
)
|
||||
|
||||
// ReplyKeyboardMarkupBuilder is a keyboard markup builder.
|
||||
type ReplyKeyboardMarkupBuilder struct {
|
||||
kb tg.ReplyKeyboardMarkup
|
||||
}
|
||||
|
||||
// Resize sets flag to request clients to resize the keyboard vertically for
|
||||
// optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons).
|
||||
// If not set, the custom keyboard is always of the same height as the app's standard keyboard.
|
||||
func (b *ReplyKeyboardMarkupBuilder) Resize() *ReplyKeyboardMarkupBuilder {
|
||||
b.kb.Resize = true
|
||||
return b
|
||||
}
|
||||
|
||||
// SingleUse sets flag to request clients to hide the keyboard as soon as it's been used.
|
||||
// The keyboard will still be available, but clients will automatically display the usual letter-keyboard
|
||||
// in the chat – the user can press a special button in the input field to see the custom keyboard again.
|
||||
func (b *ReplyKeyboardMarkupBuilder) SingleUse() *ReplyKeyboardMarkupBuilder {
|
||||
b.kb.SingleUse = true
|
||||
return b
|
||||
}
|
||||
|
||||
// Selective sets flag to show the keyboard to specific users only.
|
||||
// Targets:
|
||||
// 1. users that are @mentioned in the text of the Message object;
|
||||
// 2. if the bot's message is a reply (has reply_to_message_id), sender of the original message.
|
||||
//
|
||||
// Example: A user requests to change the bot‘s language, bot replies to the request
|
||||
// with a keyboard to select the new language.
|
||||
// Other users in the group don’t see the keyboard.
|
||||
func (b *ReplyKeyboardMarkupBuilder) Selective() *ReplyKeyboardMarkupBuilder {
|
||||
b.kb.Selective = true
|
||||
return b
|
||||
}
|
||||
|
||||
// Build returns created keyboard.
|
||||
func (b *ReplyKeyboardMarkupBuilder) Build(rows ...tg.KeyboardButtonRow,
|
||||
) tg.ReplyMarkupClass {
|
||||
cp := b.kb
|
||||
cp.Rows = rows
|
||||
return &cp
|
||||
}
|
||||
|
||||
// BuildKeyboard creates keyboard builder.
|
||||
func BuildKeyboard() *ReplyKeyboardMarkupBuilder {
|
||||
return &ReplyKeyboardMarkupBuilder{}
|
||||
}
|
||||
|
||||
// SingleRow creates keyboard with single row using given buttons.
|
||||
func SingleRow(buttons ...tg.KeyboardButtonClass) tg.ReplyMarkupClass {
|
||||
return Keyboard(Row(buttons...))
|
||||
}
|
||||
|
||||
// Keyboard creates keyboard using given rows.
|
||||
func Keyboard(rows ...tg.KeyboardButtonRow) tg.ReplyMarkupClass {
|
||||
return BuildKeyboard().Build(rows...)
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package markup
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
||||
)
|
||||
|
||||
func TestSingleRow(t *testing.T) {
|
||||
a := require.New(t)
|
||||
|
||||
buttons := []tg.KeyboardButtonClass{
|
||||
Button("gotd"),
|
||||
URL("Google!", "https://google.com?q=gotd"),
|
||||
RequestPhone("phone"),
|
||||
RequestGeoLocation("geo"),
|
||||
SwitchInline("inline", "query", true),
|
||||
Game("game"),
|
||||
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)
|
||||
a.True(ok)
|
||||
a.Len(v.Rows, 1)
|
||||
row := v.Rows[0]
|
||||
|
||||
a.Len(row.Buttons, len(buttons))
|
||||
for i, b := range buttons {
|
||||
a.Equal(b, row.Buttons[i])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user