github.com/grokify/go-ringcentral-client@v0.3.31/engagedigital/v1/examples/webhook_crud/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 "strings" 8 9 "github.com/antihax/optional" 10 "github.com/jessevdk/go-flags" 11 12 engagedigital "github.com/grokify/go-ringcentral-client/engagedigital/v1/client" 13 ex "github.com/grokify/go-ringcentral-client/engagedigital/v1/examples" 14 utils "github.com/grokify/go-ringcentral-client/engagedigital/v1/util" 15 ) 16 17 type options struct { 18 Site string `short:"s" long:"site" description:"A site" required:"true"` 19 Token string `short:"t" long:"token" description:"A token" required:"true"` 20 Object string `short:"o" long:"object" description:"A object" required:"false"` 21 Action string `short:"a" long:"action" description:"An action (create|update|delete)" required:"true"` 22 Id string `short:"i" long:"id" description:"An object id" required:"false"` 23 Name string `short:"n" long:"name" description:"A tag name" required:"false"` 24 URL string `short:"u" long:"url" description:"A webhook URL" required:"false"` 25 } 26 27 func main() { 28 opts := options{} 29 _, err := flags.Parse(&opts) 30 if err != nil { 31 log.Fatal(err) 32 } 33 34 client := utils.NewApiClient(opts.Site, opts.Token) 35 36 opts.Object = "webhook" 37 38 switch opts.Object { 39 case "webhook": 40 handleWebhooks(client, opts) 41 default: 42 log.Fatal(fmt.Sprintf("E_OBJECT_NOT_SUPPORTED [%v]", opts.Object)) 43 } 44 45 fmt.Println("DONE") 46 } 47 48 func formatRespStatusCodeError(statusCode int) string { 49 return fmt.Sprintf("E_API_ERROR [%v]", statusCode) 50 } 51 52 func handleWebhooks(client *engagedigital.APIClient, opts options) { 53 switch opts.Action { 54 case "create": 55 fmt.Println("I_CREATING_CUSTOM_FIELD") 56 opts.Name = strings.TrimSpace(opts.Name) 57 if len(opts.Name) == 0 { 58 log.Fatal("E_CREATE_CUSTOM_FIELD_NO_NAME") 59 } 60 opts.URL = strings.TrimSpace(opts.URL) 61 if len(opts.URL) == 0 { 62 log.Fatal("E_CREATE_CUSTOM_FIELD_NO_URL") 63 } 64 apiOpts := &engagedigital.CreateWebhookOpts{ 65 Active: optional.NewBool(true), 66 StagingUse: optional.NewBool(false), 67 VerifyToken: optional.NewString(opts.Token), 68 Secret: optional.NewString("ABC")} 69 ex.HandleApiResponse(client.WebhooksApi.CreateWebhook( 70 context.Background(), opts.Token, opts.Name, opts.URL, []string{ 71 "content.imported", 72 "intervention.assigned", 73 "intervention.canceled", 74 "intervention.closed", 75 "intervention.custom_fields_updated", 76 "intervention.deferred", 77 "intervention.opened", 78 "intervention.reactivated", 79 "intervention.recategorized", 80 "intervention.reopened", 81 "intervention.user_updated", 82 "push_agent.availability_change"}, apiOpts)) 83 case "read": 84 if len(opts.Id) > 0 { 85 ex.HandleApiResponse(client.WebhooksApi.GetWebhook(context.Background(), opts.Id, opts.Token)) 86 } else { 87 ex.HandleApiResponse(client.WebhooksApi.GetAllWebhooks(context.Background(), opts.Token, nil)) 88 } 89 case "update": 90 opts.Id = strings.TrimSpace(opts.Id) 91 if len(opts.Id) == 0 { 92 log.Fatal("E_UPDATE_CUSTOM_FIELD_NO_ID") 93 } 94 apiOpts := &engagedigital.UpdateWebhookOpts{ 95 RegisteredEvents: optional.NewInterface( 96 []string{ 97 "content.imported", 98 "intervention.assigned"})} 99 ex.HandleApiResponse(client.WebhooksApi.UpdateWebhook( 100 context.Background(), opts.Id, opts.Token, apiOpts)) 101 case "delete": 102 opts.Id = strings.TrimSpace(opts.Id) 103 if len(opts.Id) == 0 { 104 log.Fatal("E_DELETE_CUSTOM_FIELD_NO_ID") 105 } 106 ex.HandleApiResponse(client.WebhooksApi.DeleteWebhook( 107 context.Background(), opts.Id, opts.Token)) 108 } 109 }