whatsapp.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2018 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package whatsapp_ext
  17. import (
  18. "fmt"
  19. "encoding/json"
  20. "github.com/Rhymen/go-whatsapp"
  21. "net/http"
  22. "io/ioutil"
  23. "io"
  24. "strings"
  25. )
  26. type ExtendedConn struct {
  27. *whatsapp.Conn
  28. }
  29. func ExtendConn(conn *whatsapp.Conn) *ExtendedConn {
  30. return &ExtendedConn{
  31. Conn: conn,
  32. }
  33. }
  34. type GroupInfo struct {
  35. JID string `json:"jid"`
  36. OwnerJID string `json:"owner"`
  37. Name string `json:"subject"`
  38. NameSetTime int64 `json:"subjectTime"`
  39. NameSetBy string `json:"subjectOwner"`
  40. Topic string `json:"desc"`
  41. TopicID string `json:"descId"`
  42. TopicSetAt int64 `json:"descTime"`
  43. TopicSetBy string `json:"descOwner"`
  44. GroupCreated int64 `json:"creation"`
  45. Participants []struct {
  46. JID string `json:"id"`
  47. IsAdmin bool `json:"isAdmin"`
  48. IsSuperAdmin bool `json:"isSuperAdmin"`
  49. } `json:"participants"`
  50. }
  51. func (ext *ExtendedConn) GetGroupMetaData(jid string) (*GroupInfo, error) {
  52. data, err := ext.Conn.GetGroupMetaData(jid)
  53. if err != nil {
  54. return nil, fmt.Errorf("failed to get group metadata: %v", err)
  55. }
  56. content := <-data
  57. fmt.Println("GROUP METADATA", content)
  58. info := &GroupInfo{}
  59. err = json.Unmarshal([]byte(content), info)
  60. if err != nil {
  61. return info, fmt.Errorf("failed to unmarshal group metadata: %v", err)
  62. }
  63. for index, participant := range info.Participants {
  64. info.Participants[index].JID = strings.Replace(participant.JID, "@c.us", "@s.whatsapp.net", 1)
  65. }
  66. return info, nil
  67. }
  68. type ProfilePicInfo struct {
  69. URL string `json:"eurl"`
  70. Tag string `json:"tag"`
  71. }
  72. func (ppi *ProfilePicInfo) Download() (io.ReadCloser, error) {
  73. resp, err := http.Get(ppi.URL)
  74. if err != nil {
  75. return nil, err
  76. }
  77. return resp.Body, nil
  78. }
  79. func (ppi *ProfilePicInfo) DownloadBytes() ([]byte, error) {
  80. body, err := ppi.Download()
  81. if err != nil {
  82. return nil, err
  83. }
  84. defer body.Close()
  85. data, err := ioutil.ReadAll(body)
  86. return data, err
  87. }
  88. func (ext *ExtendedConn) GetProfilePicThumb(jid string) (*ProfilePicInfo, error) {
  89. data, err := ext.Conn.GetProfilePicThumb(jid)
  90. if err != nil {
  91. return nil, fmt.Errorf("failed to get avatar: %v", err)
  92. }
  93. content := <-data
  94. info := &ProfilePicInfo{}
  95. err = json.Unmarshal([]byte(content), info)
  96. if err != nil {
  97. return info, fmt.Errorf("failed to unmarshal avatar info: %v", err)
  98. }
  99. return info, nil
  100. }