|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/Cloverhound/five9-cli/internal/soap" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +func init() { |
| 15 | + registerPromptsUpload() |
| 16 | + registerPromptsReplace() |
| 17 | +} |
| 18 | + |
| 19 | +func registerPromptsUpload() { |
| 20 | + var filePath string |
| 21 | + var name string |
| 22 | + var description string |
| 23 | + |
| 24 | + cmd := &cobra.Command{ |
| 25 | + Use: "upload", |
| 26 | + Short: "Upload a WAV file as a new prompt", |
| 27 | + Long: `Upload a WAV file as a new prompt using addPromptWavInline. |
| 28 | +
|
| 29 | +The file is base64-encoded and sent inline in the SOAP body. |
| 30 | +If --name is omitted, the filename without extension is used. |
| 31 | +
|
| 32 | +Examples: |
| 33 | + five9 prompts upload --file greeting.wav |
| 34 | + five9 prompts upload --file greeting.wav --name "Main Greeting" |
| 35 | + five9 prompts upload --file greeting.wav --name "Main Greeting" --description "IVR main menu" |
| 36 | + five9 prompts upload --file greeting.wav --dry-run |
| 37 | + five9 prompts upload --file greeting.wav --debug`, |
| 38 | + RunE: func(c *cobra.Command, args []string) error { |
| 39 | + wavData, err := os.ReadFile(filePath) |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("reading WAV file: %w", err) |
| 42 | + } |
| 43 | + |
| 44 | + if name == "" { |
| 45 | + name = strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath)) |
| 46 | + } |
| 47 | + |
| 48 | + encoded := base64.StdEncoding.EncodeToString(wavData) |
| 49 | + |
| 50 | + req := soap.NewRequest("addPromptWavInline") |
| 51 | + |
| 52 | + promptChildren := []soap.Param{ |
| 53 | + {Name: "name", Value: name}, |
| 54 | + } |
| 55 | + if description != "" { |
| 56 | + promptChildren = append(promptChildren, soap.Param{Name: "description", Value: description}) |
| 57 | + } |
| 58 | + req.SetComplexParam("prompt", promptChildren) |
| 59 | + req.SetParam("wavFile", encoded) |
| 60 | + |
| 61 | + _, err = soap.Do(req) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + fmt.Fprintf(os.Stderr, "Prompt %q uploaded successfully\n", name) |
| 67 | + return nil |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + cmd.Flags().StringVar(&filePath, "file", "", "Path to WAV file to upload") |
| 72 | + cmd.MarkFlagRequired("file") |
| 73 | + cmd.Flags().StringVar(&name, "name", "", "Prompt name (defaults to filename without extension)") |
| 74 | + cmd.Flags().StringVar(&description, "description", "", "Prompt description") |
| 75 | + |
| 76 | + PromptsCmd.AddCommand(cmd) |
| 77 | +} |
| 78 | + |
| 79 | +func registerPromptsReplace() { |
| 80 | + var filePath string |
| 81 | + var name string |
| 82 | + |
| 83 | + cmd := &cobra.Command{ |
| 84 | + Use: "replace", |
| 85 | + Short: "Replace the WAV file on an existing prompt", |
| 86 | + Long: `Replace the audio file of an existing prompt using modifyPromptWavInline. |
| 87 | +
|
| 88 | +The file is base64-encoded and sent inline in the SOAP body. |
| 89 | +
|
| 90 | +Examples: |
| 91 | + five9 prompts replace --file greeting.wav --name "Main Greeting" |
| 92 | + five9 prompts replace --file greeting.wav --name "Main Greeting" --dry-run |
| 93 | + five9 prompts replace --file greeting.wav --name "Main Greeting" --debug`, |
| 94 | + RunE: func(c *cobra.Command, args []string) error { |
| 95 | + wavData, err := os.ReadFile(filePath) |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("reading WAV file: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + encoded := base64.StdEncoding.EncodeToString(wavData) |
| 101 | + |
| 102 | + req := soap.NewRequest("modifyPromptWavInline") |
| 103 | + req.SetComplexParam("prompt", []soap.Param{ |
| 104 | + {Name: "name", Value: name}, |
| 105 | + }) |
| 106 | + req.SetParam("wavFile", encoded) |
| 107 | + |
| 108 | + _, err = soap.Do(req) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + fmt.Fprintf(os.Stderr, "Prompt %q replaced successfully\n", name) |
| 114 | + return nil |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + cmd.Flags().StringVar(&filePath, "file", "", "Path to WAV file to upload") |
| 119 | + cmd.MarkFlagRequired("file") |
| 120 | + cmd.Flags().StringVar(&name, "name", "", "Name of existing prompt to replace") |
| 121 | + cmd.MarkFlagRequired("name") |
| 122 | + |
| 123 | + PromptsCmd.AddCommand(cmd) |
| 124 | +} |
0 commit comments