From 7d82083b039394e83ef7319853e29a3d4ef66b60 Mon Sep 17 00:00:00 2001 From: stdkhai Date: Wed, 5 Jul 2023 11:23:14 +0300 Subject: [PATCH 1/2] BOT API 6.7 implementation --- configs.go | 71 ++++++++++++++++++++++++++++++++++++++++++------- helpers.go | 24 +++++++++++++++++ helpers_test.go | 19 +++++++++++++ types.go | 47 +++++++++++++++++++++++++++++--- types_test.go | 2 ++ 5 files changed, 150 insertions(+), 13 deletions(-) diff --git a/configs.go b/configs.go index 5ca9e43..d2b22d9 100644 --- a/configs.go +++ b/configs.go @@ -1271,15 +1271,29 @@ func (config DeleteWebhookConfig) params() (Params, error) { return params, nil } +// InlineQueryResultsButton represents a button to be shown above inline query results. You must use exactly one of the optional fields. +type InlineQueryResultsButton struct { + //Label text on the button + // + Text string `json:"text"` + //Description of the Web App that will be launched when the user presses the button. The Web App will be able to switch back to the inline mode using the method switchInlineQuery inside the Web App. + // + //Optional + WebApp *WebAppInfo `json:"web_app,omitempty"` + // Deep-linking parameter for the /start message sent to the bot when a user presses the button. 1-64 characters, only A-Z, a-z, 0-9, _ and - are allowed. + // + //Optional + StartParam string `json:"start_parameter,omitempty"` +} + // InlineConfig contains information on making an InlineQuery response. type InlineConfig struct { - InlineQueryID string `json:"inline_query_id"` - Results []interface{} `json:"results"` - CacheTime int `json:"cache_time"` - IsPersonal bool `json:"is_personal"` - NextOffset string `json:"next_offset"` - SwitchPMText string `json:"switch_pm_text"` - SwitchPMParameter string `json:"switch_pm_parameter"` + InlineQueryID string `json:"inline_query_id"` + Results []interface{} `json:"results"` + CacheTime int `json:"cache_time"` + IsPersonal bool `json:"is_personal"` + NextOffset string `json:"next_offset"` + Button *InlineQueryResultsButton `json:"button,omitempty"` } func (config InlineConfig) method() string { @@ -1293,9 +1307,11 @@ func (config InlineConfig) params() (Params, error) { params.AddNonZero("cache_time", config.CacheTime) params.AddBool("is_personal", config.IsPersonal) params.AddNonEmpty("next_offset", config.NextOffset) - params.AddNonEmpty("switch_pm_text", config.SwitchPMText) - params.AddNonEmpty("switch_pm_parameter", config.SwitchPMParameter) - err := params.AddInterface("results", config.Results) + err := params.AddInterface("button", config.Button) + if err != nil { + return params, err + } + err = params.AddInterface("results", config.Results) return params, err } @@ -2795,6 +2811,41 @@ func (config DeleteMyCommandsConfig) params() (Params, error) { return params, err } +// SetMyNameConfig change the bot's name +type SetMyNameConfig struct { + Name string + LanguageCode string +} + +func (config SetMyNameConfig) method() string { + return "setMyName" +} + +func (config SetMyNameConfig) params() (Params, error) { + params := make(Params) + + params.AddNonEmpty("name", config.Name) + params.AddNonEmpty("language_code", config.LanguageCode) + + return params, nil +} + +type GetMyNameConfig struct { + LanguageCode string +} + +func (config GetMyNameConfig) method() string { + return "getMyName" +} + +func (config GetMyNameConfig) params() (Params, error) { + params := make(Params) + + params.AddNonEmpty("language_code", config.LanguageCode) + + return params, nil +} + // GetMyDescriptionConfig get the current bot description for the given user language type GetMyDescriptionConfig struct { LanguageCode string diff --git a/helpers.go b/helpers.go index 7902839..4c7a074 100644 --- a/helpers.go +++ b/helpers.go @@ -723,6 +723,15 @@ func NewInlineKeyboardButtonWebApp(text string, webapp WebAppInfo) InlineKeyboar } } +// NewInlineKeyboardButtonSwitchInlineQueryChoosenChat creates an inline keyboard button with text +// which goes to a SwitchInlineQueryChosenChat. +func NewInlineKeyboardButtonSwitchInlineQueryChoosenChat(text string, switchInlineQueryChosenChat SwitchInlineQueryChosenChat) InlineKeyboardButton { + return InlineKeyboardButton{ + Text: text, + SwitchInlineQueryChosenChat: &switchInlineQueryChosenChat, + } +} + // NewInlineKeyboardButtonLoginURL creates an inline keyboard button with text // which goes to a LoginURL. func NewInlineKeyboardButtonLoginURL(text string, loginURL LoginURL) InlineKeyboardButton { @@ -961,6 +970,21 @@ func NewGetMyShortDescription(languageCode string) GetMyShortDescriptionConfig { } } +// NewGetMyName get the current bot name for the given user language +func NewGetMyName(languageCode string) GetMyNameConfig { + return GetMyNameConfig{ + LanguageCode: languageCode, + } +} + +// NewSetMyName change the bot's name +func NewSetMyName(languageCode, name string) SetMyNameConfig { + return SetMyNameConfig{ + Name: name, + LanguageCode: languageCode, + } +} + // NewGetMyCommandsWithScope allows you to set the registered commands for a // given scope. func NewGetMyCommandsWithScope(scope BotCommandScope) GetMyCommandsConfig { diff --git a/helpers_test.go b/helpers_test.go index 9119543..fe8098e 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -178,6 +178,25 @@ func TestNewInlineKeyboardButtonLoginURL(t *testing.T) { } } +func TestNewInlineKeyboardButtonSwitchInlineQueryChoosenChat(t *testing.T) { + result := NewInlineKeyboardButtonSwitchInlineQueryChoosenChat("text", SwitchInlineQueryChosenChat{ + Query: "query", + AllowUserChats: false, + AllowBotChats: false, + AllowGroupChats: false, + AllowChannelChats: false, + }) + + if result.Text != "text" || + result.SwitchInlineQueryChosenChat.Query != "query" || + result.SwitchInlineQueryChosenChat.AllowUserChats != false || + result.SwitchInlineQueryChosenChat.AllowBotChats != false || + result.SwitchInlineQueryChosenChat.AllowGroupChats != false || + result.SwitchInlineQueryChosenChat.AllowChannelChats != false { + t.Fail() + } +} + func TestNewEditMessageText(t *testing.T) { edit := NewEditMessageText(ChatID, ReplyToMessageID, "new text") diff --git a/types.go b/types.go index 780fd6e..c97b176 100644 --- a/types.go +++ b/types.go @@ -1349,10 +1349,14 @@ type ChatShared struct { ChatID int64 `json:"chat_id"` } -// WriteAccessAllowed represents a service message about a user -// allowing a bot added to the attachment menu to write messages. -// Currently holds no information. +// WriteAccessAllowed represents a service message about a user allowing a bot +// to write messages after adding the bot to the attachment menu or launching +// a Web App from a link. type WriteAccessAllowed struct { + //Name of the Web App which was launched from a link + // + // Optional + WebAppName string `json:"web_app_name"` } // VideoChatScheduled represents a service message about a voice chat scheduled @@ -1677,6 +1681,12 @@ type InlineKeyboardButton struct { // // optional SwitchInlineQueryCurrentChat *string `json:"switch_inline_query_current_chat,omitempty"` + //SwitchInlineQueryChosenChat If set, pressing the button will prompt the user to + //select one of their chats of the specified type, open that chat and insert the bot's + //username and the specified inline query in the input field + // + //optional + SwitchInlineQueryChosenChat *SwitchInlineQueryChosenChat `json:"switch_inline_query_chosen_chat,omitempty"` // CallbackGame description of the game that will be launched when the user presses the button. // // optional @@ -2056,6 +2066,11 @@ type ChatMemberUpdated struct { // // optional InviteLink *ChatInviteLink `json:"invite_link,omitempty"` + // ViaChatFolderInviteLink is True, if the user joined the chat + // via a chat folder invite link + // + // optional + ViaChatFolderInviteLink bool `json:"via_chat_folder_invite_link,omitempty"` } // ChatJoinRequest represents a join request sent to a chat. @@ -2573,6 +2588,32 @@ type GameHighScore struct { // CallbackGame is for starting a game in an inline keyboard button. type CallbackGame struct{} +// SwitchInlineQueryChosenChat represents an inline button that switches the current +// user to inline mode in a chosen chat, with an optional default inline query. +type SwitchInlineQueryChosenChat struct { + // Query is default inline query to be inserted in the input field. + // If left empty, only the bot's username will be inserted + // + // optional + Query string `json:"query,omitempty"` + // AllowUserChats is True, if private chats with users can be chosen + // + // optional + AllowUserChats bool `json:"allow_user_chats,omitempty"` + // AllowBotChats is True, if private chats with bots can be chosen + // + // optional + AllowBotChats bool `json:"allow_bot_chats,omitempty"` + // AllowGroupChats is True, if group and supergroup chats can be chosen + // + // optional + AllowGroupChats bool `json:"allow_group_chats,omitempty"` + // AllowChannelChats is True, if channel chats can be chosen + // + // optional + AllowChannelChats bool `json:"allow_channel_chats,omitempty"` +} + // WebhookInfo is information about a currently set webhook. type WebhookInfo struct { // URL webhook URL, may be empty if webhook is not set up. diff --git a/types_test.go b/types_test.go index e4e3da1..69c0593 100644 --- a/types_test.go +++ b/types_test.go @@ -369,6 +369,8 @@ var ( _ Chattable = SetMyDescriptionConfig{} _ Chattable = GetMyShortDescriptionConfig{} _ Chattable = SetMyShortDescriptionConfig{} + _ Chattable = GetMyNameConfig{} + _ Chattable = SetMyNameConfig{} ) // Ensure all Fileable types are correct. From 45f563879fbcfee2a821f03039e4eaf16074033e Mon Sep 17 00:00:00 2001 From: OvyFlash Date: Wed, 5 Jul 2023 12:13:57 +0300 Subject: [PATCH 2/2] Add omitempty to optional fields --- configs.go | 1 - types.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/configs.go b/configs.go index d2b22d9..87e1633 100644 --- a/configs.go +++ b/configs.go @@ -1274,7 +1274,6 @@ func (config DeleteWebhookConfig) params() (Params, error) { // InlineQueryResultsButton represents a button to be shown above inline query results. You must use exactly one of the optional fields. type InlineQueryResultsButton struct { //Label text on the button - // Text string `json:"text"` //Description of the Web App that will be launched when the user presses the button. The Web App will be able to switch back to the inline mode using the method switchInlineQuery inside the Web App. // diff --git a/types.go b/types.go index c97b176..7cf3b06 100644 --- a/types.go +++ b/types.go @@ -1356,7 +1356,7 @@ type WriteAccessAllowed struct { //Name of the Web App which was launched from a link // // Optional - WebAppName string `json:"web_app_name"` + WebAppName string `json:"web_app_name,omitempty"` } // VideoChatScheduled represents a service message about a voice chat scheduled