user.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package initdata
  2. import (
  3. jsoniter "github.com/json-iterator/go"
  4. mhayaLogger "github.com/mhaya/logger"
  5. "net/url"
  6. )
  7. // User describes user information:
  8. // https://docs.telegram-mini-apps.com/launch-parameters/init-data#user
  9. type User struct {
  10. // Optional. True, if this user added the bot to the attachment menu.
  11. AddedToAttachmentMenu bool `json:"added_to_attachment_menu"`
  12. // Optional. True, if this user allowed the bot to message them.
  13. AllowsWriteToPm bool `json:"allows_write_to_pm"`
  14. // First name of the user or bot.
  15. FirstName string `json:"first_name"`
  16. // A unique identifier for the user or bot.
  17. ID int64 `json:"id"`
  18. // Optional. True, if this user is a bot. Returned in the `receiver` field
  19. // only.
  20. IsBot bool `json:"is_bot"`
  21. // Optional. True, if this user is a Telegram Premium user.
  22. IsPremium bool `json:"is_premium"`
  23. // Optional. Last name of the user or bot.
  24. LastName string `json:"last_name"`
  25. // Optional. Username of the user or bot.
  26. Username string `json:"username"`
  27. // Optional. IETF language tag of the user's language. Returns in user
  28. // field only.
  29. // https://en.wikipedia.org/wiki/IETF_language_tag
  30. LanguageCode string `json:"language_code"`
  31. // Optional. URL of the user’s profile photo. The photo can be in .jpeg or
  32. // .svg formats. Only returned for Web Apps launched from the
  33. // attachment menu.
  34. PhotoURL string `json:"photo_url"`
  35. }
  36. func GetUserInfo(str string) *User {
  37. dam, err := url.ParseQuery(str)
  38. if err != nil {
  39. mhayaLogger.Warnf("[GetUserInfo] if err. params=%s", str)
  40. return nil
  41. }
  42. var user User
  43. if d, ok := dam["user"]; ok {
  44. if len(d) > 0 {
  45. jsoniter.Unmarshal([]byte(dam["user"][0]), &user)
  46. }
  47. }
  48. return &user
  49. }