webhook.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package anaconda
  2. import (
  3. "net/url"
  4. )
  5. //GetActivityWebhooks represents the twitter account_activity webhook
  6. //Returns all URLs and their statuses for the given app. Currently,
  7. //only one webhook URL can be registered to an application.
  8. //https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference/get-webhook-config
  9. func (a TwitterApi) GetActivityWebhooks(v url.Values) (u []WebHookResp, err error) {
  10. responseCh := make(chan response)
  11. a.queryQueue <- query{a.baseUrl + "/account_activity/webhooks.json", v, &u, _GET, responseCh}
  12. return u, (<-responseCh).err
  13. }
  14. //WebHookResp represents the Get webhook responses
  15. type WebHookResp struct {
  16. ID string
  17. URL string
  18. Valid bool
  19. CreatedAt string
  20. }
  21. //SetActivityWebhooks represents to set twitter account_activity webhook
  22. //Registers a new webhook URL for the given application context.
  23. //The URL will be validated via CRC request before saving. In case the validation fails,
  24. //a comprehensive error is returned. message to the requester.
  25. //Only one webhook URL can be registered to an application.
  26. //https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference/new-webhook-config
  27. func (a TwitterApi) SetActivityWebhooks(v url.Values) (u []WebHookResp, err error) {
  28. responseCh := make(chan response)
  29. a.queryQueue <- query{a.baseUrl + "/account_activity/webhooks.json", v, &u, _POST, responseCh}
  30. return u, (<-responseCh).err
  31. }
  32. //DeleteActivityWebhooks Removes the webhook from the provided application’s configuration.
  33. //https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference/delete-webhook-config
  34. func (a TwitterApi) DeleteActivityWebhooks(v url.Values, webhookID string) (u interface{}, err error) {
  35. responseCh := make(chan response)
  36. a.queryQueue <- query{a.baseUrl + "/account_activity/webhooks/" + webhookID + ".json", v, &u, _DELETE, responseCh}
  37. return u, (<-responseCh).err
  38. }
  39. //PutActivityWebhooks update webhook which reenables the webhook by setting its status to valid.
  40. //https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference/validate-webhook-config
  41. func (a TwitterApi) PutActivityWebhooks(v url.Values, webhookID string) (u interface{}, err error) {
  42. responseCh := make(chan response)
  43. a.queryQueue <- query{a.baseUrl + "/account_activity/webhooks/" + webhookID + ".json", v, &u, _PUT, responseCh}
  44. return u, (<-responseCh).err
  45. }
  46. //SetWHSubscription Subscribes the provided app to events for the provided user context.
  47. //When subscribed, all DM events for the provided user will be sent to the app’s webhook via POST request.
  48. //https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference/new-subscription
  49. func (a TwitterApi) SetWHSubscription(v url.Values, webhookID string) (u interface{}, err error) {
  50. responseCh := make(chan response)
  51. a.queryQueue <- query{a.baseUrl + "/account_activity/webhooks/" + webhookID + "/subscriptions.json", v, &u, _POST, responseCh}
  52. return u, (<-responseCh).err
  53. }
  54. //GetWHSubscription Provides a way to determine if a webhook configuration is
  55. //subscribed to the provided user’s Direct Messages.
  56. //https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference/get-subscription
  57. func (a TwitterApi) GetWHSubscription(v url.Values, webhookID string) (u interface{}, err error) {
  58. responseCh := make(chan response)
  59. a.queryQueue <- query{a.baseUrl + "/account_activity/webhooks/" + webhookID + "/subscriptions.json", v, &u, _GET, responseCh}
  60. return u, (<-responseCh).err
  61. }
  62. //DeleteWHSubscription Deactivates subscription for the provided user context and app. After deactivation,
  63. //all DM events for the requesting user will no longer be sent to the webhook URL..
  64. //https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference/delete-subscription
  65. func (a TwitterApi) DeleteWHSubscription(v url.Values, webhookID string) (u interface{}, err error) {
  66. responseCh := make(chan response)
  67. a.queryQueue <- query{a.baseUrl + "/account_activity/webhooks/" + webhookID + "/subscriptions.json", v, &u, _DELETE, responseCh}
  68. return u, (<-responseCh).err
  69. }