telebot: refactor web apps

pull/558/head
Demian 2 years ago
parent c7959ca48c
commit cee9ad2ddd

@ -1071,20 +1071,6 @@ func (b *Bot) Accept(query *PreCheckoutQuery, errorMessage ...string) error {
return err
}
// Answer sends a response for a given inline query. A query can only
// be responded to once, subsequent attempts to respond to the same query
// will result in an error.
func (b *Bot) Answer(query *Query, resp *QueryResponse) error {
resp.QueryID = query.ID
for _, result := range resp.Results {
result.Process(b)
}
_, err := b.Raw("answerInlineQuery", resp)
return err
}
// Respond sends a response for a given callback query. A callback can
// only be responded to once, subsequent attempts to respond to the same callback
// will result in an error.
@ -1107,27 +1093,43 @@ func (b *Bot) Respond(c *Callback, resp ...*CallbackResponse) error {
return err
}
// Answer sends a response for a given inline query. A query can only
// be responded to once, subsequent attempts to respond to the same query
// will result in an error.
func (b *Bot) Answer(query *Query, resp *QueryResponse) error {
resp.QueryID = query.ID
for _, result := range resp.Results {
result.Process(b)
}
_, err := b.Raw("answerInlineQuery", resp)
return err
}
// AnswerWebApp sends a response for a query from Web App and returns
// information about an inline message sent by a Web App on behalf of a user
func (b *Bot) AnswerWebApp(query *Query, resp *WebAppQueryResponse) (SentWebAppMessage, error) {
resp.WebAppQueryID = query.ID
resp.Result.Process(b)
func (b *Bot) AnswerWebApp(query *Query, r Result) (*WebAppMessage, error) {
r.Process(b)
data, err := b.Raw("answerWebAppQuery", resp)
params := map[string]interface{}{
"web_app_query_id": query.ID,
"result": r,
}
data, err := b.Raw("answerWebAppQuery", params)
if err != nil {
return SentWebAppMessage{}, err
return nil, err
}
var response struct {
Result SentWebAppMessage
var resp struct {
Result *WebAppMessage
}
if err := json.Unmarshal(data, &response); err != nil {
return SentWebAppMessage{}, wrapError(err)
if err := json.Unmarshal(data, &resp); err != nil {
return nil, wrapError(err)
}
return response.Result, err
return resp.Result, err
}
// FileByID returns full file object including File.FilePath, allowing you to
@ -1425,6 +1427,7 @@ func (b *Bot) ChatByID(id int64) (*Chat, error) {
return b.ChatByUsername(strconv.FormatInt(id, 10))
}
// ChatByUsername fetches chat info by its username.
func (b *Bot) ChatByUsername(name string) (*Chat, error) {
params := map[string]string{
"chat_id": name,

@ -80,13 +80,30 @@ type InlineButton struct {
// It will be used as a callback endpoint.
Unique string `json:"unique,omitempty"`
Text string `json:"text"`
URL string `json:"url,omitempty"`
Data string `json:"callback_data,omitempty"`
InlineQuery string `json:"switch_inline_query,omitempty"`
InlineQueryChat string `json:"switch_inline_query_current_chat"`
WebApp *WebAppInfo `json:"web_app,omitempty"`
Login *Login `json:"login_url,omitempty"`
Text string `json:"text"`
URL string `json:"url,omitempty"`
Data string `json:"callback_data,omitempty"`
InlineQuery string `json:"switch_inline_query,omitempty"`
InlineQueryChat string `json:"switch_inline_query_current_chat"`
WebApp *WebApp `json:"web_app,omitempty"`
Login *Login `json:"login_url,omitempty"`
}
// MarshalJSON implements json.Marshaler interface.
// It needed to avoid InlineQueryChat and Login fields conflict.
// If you have Login field in your button, InlineQueryChat must be skipped.
func (t *InlineButton) MarshalJSON() ([]byte, error) {
type IB InlineButton
if t.Login != nil {
return json.Marshal(struct {
IB
InlineQueryChat string `json:"switch_inline_query_current_chat,omitempty"`
}{
IB: IB(*t),
})
}
return json.Marshal(IB(*t))
}
// With returns a copy of the button with data.
@ -129,26 +146,3 @@ type Login struct {
Username string `json:"bot_username,omitempty"`
WriteAccess bool `json:"request_write_access,omitempty"`
}
// WebAppInfo represents a parameter of the inline keyboard button
// or the keyboard button used to launch Web App.
type WebAppInfo struct {
URL string `json:"url"`
}
// MarshalJSON implements json.Marshaler interface.
// It needed to avoid InlineQueryChat and Login fields conflict.
// If you have Login field in your button, InlineQueryChat must be skipped.
func (t *InlineButton) MarshalJSON() ([]byte, error) {
type InlineButtonJSON InlineButton
if t.Login != nil {
return json.Marshal(struct {
InlineButtonJSON
InlineQueryChat string `json:"switch_inline_query_current_chat,omitempty"`
}{
InlineButtonJSON: InlineButtonJSON(*t),
})
}
return json.Marshal(InlineButtonJSON(*t))
}

@ -145,9 +145,9 @@ type Context interface {
// See Respond from bot.go.
Respond(resp ...*CallbackResponse) error
// AnswerWebApp sends a response to the Web App query.
// AnswerWebApp sends a response to web app query.
// See AnswerWebApp from bot.go.
AnswerWebApp(resp *WebAppQueryResponse) error
AnswerWebApp(result Result) error
// Get retrieves data from the context.
Get(key string) interface{}
@ -444,13 +444,6 @@ func (c *nativeContext) Accept(errorMessage ...string) error {
return c.b.Accept(c.u.PreCheckoutQuery, errorMessage...)
}
func (c *nativeContext) Answer(resp *QueryResponse) error {
if c.u.Query == nil {
return errors.New("telebot: context inline query is nil")
}
return c.b.Answer(c.u.Query, resp)
}
func (c *nativeContext) Respond(resp ...*CallbackResponse) error {
if c.u.Callback == nil {
return errors.New("telebot: context callback is nil")
@ -458,11 +451,18 @@ func (c *nativeContext) Respond(resp ...*CallbackResponse) error {
return c.b.Respond(c.u.Callback, resp...)
}
func (c *nativeContext) AnswerWebApp(resp *WebAppQueryResponse) error {
func (c *nativeContext) Answer(resp *QueryResponse) error {
if c.u.Query == nil {
return errors.New("telebot: context inline query is nil")
}
_, err := c.b.AnswerWebApp(c.u.Query, resp)
return c.b.Answer(c.u.Query, resp)
}
func (c *nativeContext) AnswerWebApp(result Result) error {
if c.u.Message == nil || c.u.Message.WebAppData == nil {
return errors.New("telebot: context web app is nil")
}
_, err := c.b.AnswerWebApp(c.u.Query, result)
return err
}

@ -137,15 +137,3 @@ func inferIQR(result Result) error {
return nil
}
// WebAppQueryResponse builds a response to an web app query.
type WebAppQueryResponse struct {
// (Required) Unique identifier for the query to be answered
WebAppQueryID string `json:"web_app_query_id"`
// (Required) A object describing the message to be sent
Result Result `json:"result"`
}
type SentWebAppMessage struct {
InlineMessageID string `json:"inline_message_id"`
}

@ -334,12 +334,6 @@ type Venue struct {
GooglePlaceType string `json:"google_place_type,omitempty"`
}
// WebAppData object represents a data sent from a Web App to the bot
type WebAppData struct {
Data string `json:"data"`
ButtonText string `json:"button_text"`
}
// Dice object represents a dice with a random value
// from 1 to 6 for currently supported base emoji.
type Dice struct {

@ -238,7 +238,7 @@ type Message struct {
// For a service message, a voice chat schedule in the chat.
VoiceChatScheduled *VoiceChatScheduled `json:"voice_chat_scheduled,omitempty"`
// For a data sent by a Web App
// For a data sent by a Web App.
WebAppData *WebAppData `json:"web_app_data,omitempty"`
// For a service message, represents the content of a service message,

@ -169,10 +169,10 @@ func (r *ReplyMarkup) copy() *ReplyMarkup {
type ReplyButton struct {
Text string `json:"text"`
Contact bool `json:"request_contact,omitempty"`
Location bool `json:"request_location,omitempty"`
Poll PollType `json:"request_poll,omitempty"`
WebApp WebAppInfo `json:"request_web_app,omitempty"`
Contact bool `json:"request_contact,omitempty"`
Location bool `json:"request_location,omitempty"`
Poll PollType `json:"request_poll,omitempty"`
WebApp *WebApp `json:"web_app,omitempty"`
}
// MarshalJSON implements json.Marshaler. It allows to pass

@ -18,7 +18,7 @@
// return
// }
//
// b.Handle(tele.OnText, func(c tele.Context) error {
// b.Handle("/start", func(c tele.Context) error {
// return c.Send("Hello world!")
// })
//

@ -0,0 +1,18 @@
package telebot
// WebApp represents a parameter of the inline keyboard button
// or the keyboard button used to launch Web App.
type WebApp struct {
URL string `json:"url"`
}
// WebAppMessage describes an inline message sent by a Web App on behalf of a user.
type WebAppMessage struct {
InlineMessageID string `json:"inline_message_id"`
}
// WebAppData object represents a data sent from a Web App to the bot
type WebAppData struct {
Data string `json:"data"`
Text string `json:"button_text"`
}
Loading…
Cancel
Save