diff --git a/bot.go b/bot.go index 8da821c..f7d0f29 100644 --- a/bot.go +++ b/bot.go @@ -409,6 +409,10 @@ func (b *Bot) ProcessUpdate(u Update) { return } + if m.WebAppData != nil { + b.handle(OnWebApp, c) + } + if m.ProximityAlert != nil { b.handle(OnProximityAlert, c) return @@ -1087,6 +1091,29 @@ func (b *Bot) Respond(c *Callback, resp ...*CallbackResponse) error { 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) + + data, err := b.Raw("answerWebAppQuery", resp) + + if err != nil { + return SentWebAppMessage{}, err + } + + var response struct { + Result SentWebAppMessage + } + + if err := json.Unmarshal(data, &response); err != nil { + return SentWebAppMessage{}, wrapError(err) + } + + return response.Result, err +} + // FileByID returns full file object including File.FilePath, allowing you to // download the file from the server. // diff --git a/context.go b/context.go index 6bd9ec1..549b4f1 100644 --- a/context.go +++ b/context.go @@ -145,6 +145,10 @@ type Context interface { // See Respond from bot.go. Respond(resp ...*CallbackResponse) error + // AnswerWebApp sends a response to the Web App query. + // See AnswerWebApp from bot.go. + AnswerWebApp(resp *WebAppQueryResponse) error + // Get retrieves data from the context. Get(key string) interface{} @@ -454,6 +458,14 @@ func (c *nativeContext) Respond(resp ...*CallbackResponse) error { return c.b.Respond(c.u.Callback, resp...) } +func (c *nativeContext) AnswerWebApp(resp *WebAppQueryResponse) error { + if c.u.Query == nil { + return errors.New("telebot: context inline query is nil") + } + _, err := c.b.AnswerWebApp(c.u.Query, resp) + return err +} + func (c *nativeContext) Set(key string, value interface{}) { c.lock.Lock() defer c.lock.Unlock() diff --git a/inline.go b/inline.go index b369139..6b66523 100644 --- a/inline.go +++ b/inline.go @@ -137,3 +137,15 @@ 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"` +} diff --git a/telebot.go b/telebot.go index b5cb932..34f0815 100644 --- a/telebot.go +++ b/telebot.go @@ -134,6 +134,9 @@ const ( // Will fire on auto delete timer set. OnAutoDeleteTimer = "\amessage_auto_delete_timer_changed" + + // Will fire on the web app data. + OnWebApp = "\aweb_app" ) // ChatAction is a client-side status indicating bot activity.