github.com/grokify/go-ringcentral-client@v0.3.31/engagedigital/v1/examples/simple_crud/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 "strings" 8 "time" 9 10 "github.com/antihax/optional" 11 "github.com/jessevdk/go-flags" 12 13 engagedigital "github.com/grokify/go-ringcentral-client/engagedigital/v1/client" 14 ex "github.com/grokify/go-ringcentral-client/engagedigital/v1/examples" 15 utils "github.com/grokify/go-ringcentral-client/engagedigital/v1/util" 16 "github.com/grokify/mogo/time/timeutil" 17 ) 18 19 type options struct { 20 Site string `short:"s" long:"site" description:"A site" required:"true"` 21 Token string `short:"t" long:"token" description:"A token" required:"true"` 22 Object string `short:"o" long:"obect" description:"An object" required:"true"` 23 Action string `short:"a" long:"action" description:"An action (create|update|delete)" required:"true"` 24 Name string `short:"n" long:"name" description:"A tag name" required:"false"` 25 Id string `short:"i" long:"id" description:"A tag id" required:"false"` 26 } 27 28 func main() { 29 opts := options{} 30 _, err := flags.Parse(&opts) 31 if err != nil { 32 log.Fatal(err) 33 } 34 35 client := utils.NewApiClient(opts.Site, opts.Token) 36 37 switch strings.ToLower(opts.Object) { 38 case "customfield": 39 handleCustomField(client, opts) 40 case "presencestatus": 41 handlePresenceStatus(client, opts) 42 case "tag": 43 handleTag(client, opts) 44 case "replyassistantentry": 45 handleReplyAssistantEntry(client, opts) 46 case "replyassistantgroup": 47 handleReplyAssistantGroup(client, opts) 48 case "replyassistantversion": 49 handleReplyAssistantVersion(client, opts) 50 case "settings": 51 handleSettings(client, opts) 52 case "timesheet": 53 handleTimeSheet(client, opts) 54 default: 55 log.Fatal(fmt.Sprintf("E_OBJECT_NOT_SUPPORTED [%v]", opts.Object)) 56 } 57 58 fmt.Println("DONE") 59 } 60 61 func formatRespStatusCodeError(statusCode int) string { 62 return fmt.Sprintf("E_API_ERROR [%v]", statusCode) 63 } 64 65 func handleReplyAssistantGroup(client *engagedigital.APIClient, opts options) { 66 switch opts.Action { 67 case "create": 68 dt := time.Now() 69 name := "New Reply Assistant Group Name " + dt.Format(time.RFC3339) 70 ex.HandleApiResponse(client.ReplyAssistantGroupsApi.CreateReplyAssistantGroup(context.Background(), name, nil)) 71 case "read": 72 if len(opts.Id) > 0 { 73 ex.HandleApiResponse(client.ReplyAssistantGroupsApi.GetReplyAssistantGroup(context.Background(), opts.Id)) 74 } else { 75 ex.HandleApiResponse(client.ReplyAssistantGroupsApi.GetAllReplyAssistantGroups(context.Background(), nil)) 76 } 77 case "update": 78 if len(opts.Id) > 0 { 79 dt := time.Now() 80 name := "Updated Reply Assistant Group Name " + dt.Format(time.RFC3339) 81 apiOpts := &engagedigital.UpdateReplyAssistantGroupOpts{ 82 Name: optional.NewString(name)} 83 ex.HandleApiResponse(client.ReplyAssistantGroupsApi.UpdateReplyAssistantGroup(context.Background(), opts.Id, apiOpts)) 84 } else { 85 log.Fatal("E_REPLYASSISTANTENTRY_UPDATE_ID_NOT_PRESENT") 86 } 87 case "delete": 88 if len(opts.Id) > 0 { 89 ex.HandleApiResponse(client.ReplyAssistantGroupsApi.DeleteReplyAssistantGroup(context.Background(), opts.Id)) 90 } else { 91 log.Fatal("E_REPLYASSISTANTGROUP_DELETE_ID_NOT_PRESENT") 92 } 93 } 94 } 95 96 func handleReplyAssistantVersion(client *engagedigital.APIClient, opts options) { 97 switch opts.Action { 98 case "create": 99 if len(opts.Id) > 0 { 100 dt := time.Now() 101 body := "New Reply Assistant Version Body " + dt.Format(time.RFC3339) 102 apiOpts := &engagedigital.CreateReplyAssistantVersionOpts{} 103 ex.HandleApiResponse(client.ReplyAssistantVersionsApi.CreateReplyAssistantVersion(context.Background(), body, opts.Id, apiOpts)) 104 } else { 105 log.Fatal("E_REPLYASSISTANTVERSION_CREATE_NO_ENTRY_ID") 106 } 107 case "read": 108 if len(opts.Id) > 0 { 109 ex.HandleApiResponse(client.ReplyAssistantVersionsApi.GetReplyAssistantVersion(context.Background(), opts.Id)) 110 } else { 111 ex.HandleApiResponse(client.ReplyAssistantVersionsApi.GetAllReplyAssistantVersions(context.Background(), nil)) 112 } 113 case "update": 114 if len(opts.Id) > 0 { 115 dt := time.Now() 116 body := "Updated Reply Assistant Version Body " + dt.Format(time.RFC3339) 117 apiOpts := &engagedigital.UpdateReplyAssistantVersionOpts{ 118 Body: optional.NewString(body)} 119 ex.HandleApiResponse(client.ReplyAssistantVersionsApi.UpdateReplyAssistantVersion(context.Background(), opts.Id, apiOpts)) 120 } else { 121 log.Fatal("E_REPLYASSISTANTENTRY_UPDATE_ID_NOT_PRESENT") 122 } 123 case "delete": 124 if len(opts.Id) > 0 { 125 ex.HandleApiResponse(client.ReplyAssistantVersionsApi.DeleteReplyAssistantVersion(context.Background(), opts.Id)) 126 } else { 127 log.Fatal("E_REPLYASSISTANTVERSION_DELETE_ID_NOT_PRESENT") 128 } 129 } 130 } 131 132 func handleReplyAssistantEntry(client *engagedigital.APIClient, opts options) { 133 switch opts.Action { 134 case "create": 135 dt := time.Now() 136 label := "New Reply Assistant Entry Label " + dt.Format(time.RFC3339) 137 ex.HandleApiResponse(client.ReplyAssistantEntriesApi.CreateReplyAssistantEntry(context.Background(), label)) 138 case "read": 139 if len(opts.Id) > 0 { 140 ex.HandleApiResponse(client.ReplyAssistantEntriesApi.GetReplyAssistantEntry(context.Background(), opts.Id)) 141 } else { 142 ex.HandleApiResponse(client.ReplyAssistantEntriesApi.GetAllReplyAssistantEntries(context.Background(), nil)) 143 } 144 case "update": 145 if len(opts.Id) > 0 { 146 dt := time.Now() 147 label := "Updated Reply Assistant Label " + dt.Format(time.RFC3339) 148 apiOpts := &engagedigital.UpdateReplyAssistantEntryOpts{ 149 Label: optional.NewString(label)} 150 ex.HandleApiResponse(client.ReplyAssistantEntriesApi.UpdateReplyAssistantEntry(context.Background(), opts.Id, apiOpts)) 151 } else { 152 log.Fatal("E_REPLYASSISTANTENTRY_UPDATE_ID_NOT_PRESENT") 153 } 154 case "delete": 155 if len(opts.Id) > 0 { 156 ex.HandleApiResponse(client.ReplyAssistantEntriesApi.DeleteReplyAssistantEntry(context.Background(), opts.Id)) 157 } else { 158 log.Fatal("E_REPLYASSISTANTENTRY_DELETE_ID_NOT_PRESENT") 159 } 160 } 161 } 162 163 func handleTimeSheet(client *engagedigital.APIClient, opts options) { 164 switch opts.Action { 165 case "create": 166 dt := time.Now() 167 label := "TestTimeSheet2-" + dt.Format(timeutil.DT14) 168 opts.Id = strings.TrimSpace(opts.Id) 169 /*if len(opts.Id) == 0 { 170 log.Fatal("E_CREATE_TIMESHEET_NO_SOURCE_ID") 171 }*/ 172 fmt.Println(label) 173 apiOpts := &engagedigital.CreateTimeSheetOpts{ 174 Active: optional.NewBool(false), 175 HolidaysRegion: optional.NewString("us"), 176 SourceIds: optional.NewInterface([]string{opts.Id}), 177 MondayHours: optional.NewString("28800-72000")} 178 ex.HandleApiResponse(client.TimeSheetsApi.CreateTimeSheet(context.Background(), label, apiOpts)) 179 case "read": 180 if len(opts.Id) > 0 { 181 ex.HandleApiResponse(client.TimeSheetsApi.GetTimeSheet(context.Background(), opts.Id)) 182 } else { 183 ex.HandleApiResponse(client.TimeSheetsApi.GetAllTimeSheets(context.Background(), nil)) 184 } 185 case "update": 186 if len(opts.Id) == 0 { 187 log.Fatal("E_UPDATE_TIMESHEET_NO_ID") 188 } 189 info, resp, err := client.TimeSheetsApi.GetTimeSheet(context.Background(), opts.Id) 190 if err != nil { 191 log.Fatal(err) 192 } else if resp.StatusCode != 200 { 193 log.Fatal(fmt.Sprintf("STATUS_CODE [%v]", resp.StatusCode)) 194 } 195 apiOpts := &engagedigital.UpdateTimeSheetOpts{} 196 if info.MondayHours == "28800-72000" { 197 apiOpts.MondayHours = optional.NewString("0-86400") 198 } else { 199 apiOpts.MondayHours = optional.NewString("28800-72000") 200 } 201 ex.HandleApiResponse(client.TimeSheetsApi.UpdateTimeSheet(context.Background(), opts.Id, apiOpts)) 202 case "delete": 203 if len(opts.Id) > 0 { 204 ex.HandleApiResponse(client.TimeSheetsApi.DeleteTimeSheet(context.Background(), opts.Id)) 205 } else { 206 log.Fatal("E_DELETE_TIMESHEET_NO_ID") 207 } 208 } 209 } 210 211 func handleSettings(client *engagedigital.APIClient, opts options) { 212 switch opts.Action { 213 case "read": 214 ex.HandleApiResponse(client.SettingsApi.GetAllSettings(context.Background())) 215 case "update": 216 info, resp, err := client.SettingsApi.GetAllSettings(context.Background()) 217 if err != nil { 218 log.Fatal(err) 219 } else if resp.StatusCode != 200 { 220 log.Fatal(fmt.Sprintf("Status Code [%v]", resp.StatusCode)) 221 } 222 apiOpts := &engagedigital.UpdateSettingsOpts{} 223 if info.Locale == "en" { 224 apiOpts.Locale = optional.NewString("fr") 225 } else { 226 apiOpts.Locale = optional.NewString("en") 227 } 228 ex.HandleApiResponse(client.SettingsApi.UpdateSettings(context.Background(), apiOpts)) 229 } 230 } 231 232 func handleCustomField(client *engagedigital.APIClient, opts options) { 233 switch opts.Action { 234 case "create": 235 fmt.Println("I_CREATING_CUSTOM_FIELD") 236 opts.Name = strings.TrimSpace(opts.Name) 237 if len(opts.Name) == 0 { 238 log.Fatal("E_CREATE_CUSTOM_FIELD_NO_NAME") 239 } 240 ex.HandleApiResponse(client.CustomFieldsApi.CreateCustomField( 241 context.Background(), "Intervention", opts.Name, nil)) 242 case "read": 243 if len(opts.Id) > 0 { 244 ex.HandleApiResponse(client.CustomFieldsApi.GetCustomField(context.Background(), opts.Id)) 245 } else { 246 ex.HandleApiResponse(client.CustomFieldsApi.GetAllCustomFields(context.Background(), nil)) 247 } 248 case "update": 249 opts.Id = strings.TrimSpace(opts.Id) 250 opts.Name = strings.TrimSpace(opts.Name) 251 apiOpts := &engagedigital.UpdateCustomFieldOpts{ 252 Label: optional.NewString(opts.Name)} 253 if len(opts.Name) == 0 || len(opts.Id) == 0 { 254 log.Fatal("E_UPDATE_CUSTOM_FIELD_NO_ID_OR_NAME") 255 } 256 ex.HandleApiResponse(client.CustomFieldsApi.UpdateCustomField( 257 context.Background(), opts.Id, apiOpts)) 258 case "delete": 259 opts.Id = strings.TrimSpace(opts.Id) 260 if len(opts.Id) == 0 { 261 log.Fatal("E_DELETE_CUSTOM_FIELD_NO_ID") 262 } 263 ex.HandleApiResponse(client.CustomFieldsApi.DeleteCustomField( 264 context.Background(), opts.Id)) 265 } 266 } 267 268 func handlePresenceStatus(client *engagedigital.APIClient, opts options) { 269 switch opts.Action { 270 case "create": 271 fmt.Println("I_CREATING_PRESENCE_STATUS") 272 opts.Name = strings.TrimSpace(opts.Name) 273 if len(opts.Name) == 0 { 274 log.Fatal("E_CREATE_PRESENCE_STATUS_NO_NAME") 275 } 276 ex.HandleApiResponse(client.PresenceStatusApi.CreatePresenceStatus( 277 context.Background(), 278 opts.Name)) 279 case "read": 280 if len(opts.Id) > 0 { 281 ex.HandleApiResponse(client.PresenceStatusApi.GetPresenceStatus(context.Background(), opts.Id)) 282 } else { 283 ex.HandleApiResponse(client.PresenceStatusApi.GetAllPresenceStatus(context.Background(), nil)) 284 } 285 case "update": 286 opts.Id = strings.TrimSpace(opts.Id) 287 opts.Name = strings.TrimSpace(opts.Name) 288 if len(opts.Name) == 0 || len(opts.Id) == 0 { 289 log.Fatal("E_UPDATE_PRESENCE_STATUS_NO_ID_OR_NAME") 290 } 291 ex.HandleApiResponse(client.PresenceStatusApi.UpdatePresenceStatus( 292 context.Background(), opts.Id, opts.Name)) 293 case "delete": 294 opts.Id = strings.TrimSpace(opts.Id) 295 if len(opts.Id) == 0 { 296 log.Fatal("E_DELETE_PRESENCE_STATUS_NO_ID") 297 } 298 ex.HandleApiResponse(client.PresenceStatusApi.DeletePresenceStatus( 299 context.Background(), opts.Id)) 300 } 301 } 302 303 func handleTag(client *engagedigital.APIClient, opts options) { 304 switch opts.Action { 305 case "create": 306 fmt.Println("I_CREATING_TAG") 307 opts.Name = strings.TrimSpace(opts.Name) 308 if len(opts.Name) == 0 { 309 log.Fatal("E_CREATE_TAG_NO_NAME") 310 } 311 ex.HandleApiResponse(client.TagsApi.CreateTag( 312 context.Background(), 313 opts.Name)) 314 case "read": 315 if len(opts.Id) > 0 { 316 ex.HandleApiResponse(client.TagsApi.GetTag(context.Background(), opts.Id)) 317 } else { 318 ex.HandleApiResponse(client.TagsApi.GetAllTags(context.Background(), nil)) 319 } 320 case "update": 321 opts.Id = strings.TrimSpace(opts.Id) 322 opts.Name = strings.TrimSpace(opts.Name) 323 if len(opts.Name) == 0 || len(opts.Id) == 0 { 324 log.Fatal("E_UPDATE_TAG_NO_ID_OR_NAME") 325 } 326 ex.HandleApiResponse(client.TagsApi.UpdateTag( 327 context.Background(), opts.Id, opts.Name)) 328 case "delete": 329 opts.Id = strings.TrimSpace(opts.Id) 330 if len(opts.Id) == 0 { 331 log.Fatal("E_DELETE_TAG_NO_ID") 332 } 333 ex.HandleApiResponse(client.TagsApi.DeleteTag( 334 context.Background(), opts.Id)) 335 } 336 } 337 338 /* 339 func handleTopology(client *engagedigital.APIClient, opts options) { 340 logSlug := "TOPOLOGY" 341 switch opts.Action { 342 case "create": 343 all, resp, err := client.TopologiesApi.GetAllTopologies(context.Background(), nil) 344 if err != nil { 345 log.Fatal(err) 346 } else if resp.StatusCode != 200 { 347 log.Fatal(formatRespStatusCodeError(resp.StatusCode)) 348 } 349 defTop, err := utils.FindDefaultTopology(all.Records) 350 if err != nil { 351 log.Fatal(err) 352 } 353 354 fmt.Printf("I_CREATING_%s", logSlug) 355 opts.Name = strings.TrimSpace(opts.Name) 356 if len(opts.Name) == 0 { 357 log.Fatal(fmt.Sprintf("E_CREATE_%s_NO_NAME", logSlug)) 358 } 359 newTop := engagedigital.Topology{Config: defTop.Config} 360 newTopString, err := json.Marshal(newTop) 361 if err != nil { 362 log.Fatal(err) 363 } 364 apiOpts := &engagedigital.CreateTopologyOpts{ 365 JsonConfig: optional.NewString(string(newTopString))} 366 367 ex.HandleApiResponse(client.TopologiesApi.CreateTopology( 368 context.Background(), opts.Name, apiOpts)) 369 case "read": 370 371 if len(opts.Id) > 0 { 372 ex.HandleApiResponse(client.TopologiesApi.GetTopology(context.Background(), opts.Id)) 373 } else { 374 ex.HandleApiResponse(client.TopologiesApi.GetAllTopologies(context.Background(), nil)) 375 } 376 377 case "update": 378 opts.Id = strings.TrimSpace(opts.Id) 379 opts.Name = strings.TrimSpace(opts.Name) 380 apiOpts := &engagedigital.UpdateCustomFieldOpts{ 381 Label: optional.NewString(opts.Name)} 382 if len(opts.Name) == 0 || len(opts.Id) == 0 { 383 log.Fatal(fmt.Sprintf("E_UPDATE_%s_NO_ID_OR_NAME", logSlug)) 384 } 385 ex.HandleApiResponse(client.CustomFieldsApi.UpdateCustomField( 386 context.Background(), opts.Id, apiOpts)) 387 case "delete": 388 opts.Id = strings.TrimSpace(opts.Id) 389 if len(opts.Id) == 0 { 390 log.Fatal(fmt.Sprintf("E_DELETE_%s_NO_ID", logSlug)) 391 } 392 ex.HandleApiResponse(client.CustomFieldsApi.DeleteCustomField( 393 context.Background(), opts.Id)) 394 395 } 396 } 397 */