init_data.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package initdata
  2. import (
  3. "time"
  4. )
  5. // InitData contains init data.
  6. // https://docs.telegram-mini-apps.com/launch-parameters/init-data#parameters-list
  7. type InitData struct {
  8. // The date the initialization data was created. Is a number representing a
  9. // Unix timestamp.
  10. AuthDateRaw int `json:"auth_date"`
  11. // Optional. The number of seconds after which a message can be sent via
  12. // the method answerWebAppQuery.
  13. // https://core.telegram.org/bots/api#answerwebappquery
  14. CanSendAfterRaw int `json:"can_send_after"`
  15. // Optional. An object containing information about the chat with the bot in
  16. // which the Mini Apps was launched. It is returned only for Mini Apps
  17. // opened through the attachment menu.
  18. Chat Chat `json:"chat"`
  19. // Optional. The type of chat from which the Mini Apps was opened.
  20. // Returned only for applications opened by direct link.
  21. ChatType ChatType `json:"chat_type"`
  22. // Optional. A global identifier indicating the chat from which the Mini
  23. // Apps was opened. Returned only for applications opened by direct link.
  24. ChatInstance int64 `json:"chat_instance"`
  25. // Initialization data signature.
  26. // https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app
  27. Hash string `json:"hash"`
  28. // Optional. The unique session ID of the Mini App. Used in the process of
  29. // sending a message via the method answerWebAppQuery.
  30. // https://core.telegram.org/bots/api#answerwebappquery
  31. QueryID string `json:"query_id"`
  32. // Optional. An object containing data about the chat partner of the current
  33. // user in the chat where the bot was launched via the attachment menu.
  34. // Returned only for private chats and only for Mini Apps launched via the
  35. // attachment menu.
  36. Receiver User `json:"receiver"`
  37. // Optional. The value of the startattach or startapp query parameter
  38. // specified in the link. It is returned only for Mini Apps opened through
  39. // the attachment menu.
  40. StartParam string `json:"start_param"`
  41. // Optional. An object containing information about the current user.
  42. User User `json:"user"`
  43. }
  44. // AuthDate returns AuthDateRaw as time.Time.
  45. func (d *InitData) AuthDate() time.Time {
  46. return time.Unix(int64(d.AuthDateRaw), 0)
  47. }
  48. // CanSendAfter returns computed time which depends on CanSendAfterRaw and
  49. // AuthDate. Originally, CanSendAfterRaw means time in seconds, after which
  50. // `answerWebAppQuery` method can be called and that's why this value could
  51. // be computed as time.
  52. func (d *InitData) CanSendAfter() time.Time {
  53. return d.AuthDate().Add(time.Duration(d.CanSendAfterRaw) * time.Second)
  54. }