github.com/CyCoreSystems/ari@v4.8.4+incompatible/ext/play/example_test.go (about) 1 package play 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/CyCoreSystems/ari" 9 "github.com/CyCoreSystems/ari/client/arimocks" 10 ) 11 12 func ExamplePlay() { 13 c := &arimocks.Client{} 14 key := ari.NewKey(ari.ChannelKey, "exampleChannel") 15 h := ari.NewChannelHandle(key, c.Channel(), nil) 16 17 res, err := Play(context.TODO(), h, 18 URI("sound:tt-monkeys", "sound:vm-goodbye"), 19 ).Result() 20 if err != nil { 21 fmt.Println("Failed to play audio", err) 22 return 23 } 24 25 if len(res.DTMF) > 0 { 26 fmt.Println("Got a DTMF during playback:", res.DTMF) 27 } 28 } 29 30 func ExamplePlay_async() { 31 c := &arimocks.Client{} 32 key := ari.NewKey(ari.ChannelKey, "exampleChannel") 33 h := ari.NewChannelHandle(key, c.Channel(), nil) 34 35 bridgeSub := h.Subscribe(ari.Events.ChannelEnteredBridge) 36 defer bridgeSub.Cancel() 37 38 sess := Play(context.TODO(), h, 39 URI("characters:ded", "sound:tt-monkeys", 40 "number:192846", "digits:43"), 41 ) 42 43 select { 44 case <-bridgeSub.Events(): 45 fmt.Println("Channel entered bridge during playback") 46 case <-sess.Done(): 47 if sess.Err() != nil { 48 fmt.Println("Prompt failed", sess.Err()) 49 } else { 50 fmt.Println("Prompt complete") 51 } 52 } 53 } 54 55 func ExamplePrompt() { 56 c := &arimocks.Client{} 57 key := ari.NewKey(ari.ChannelKey, "exampleChannel") 58 h := ari.NewChannelHandle(key, c.Channel(), nil) 59 60 res, err := Prompt(context.TODO(), h, 61 URI("tone:1004/250", "sound:vm-enter-num-to-call", 62 "sound:astcc-followed-by-pound"), 63 MatchHash(), // match any digits until hash 64 Replays(3), // repeat prompt up to three times, if no match 65 ).Result() 66 if err != nil { 67 fmt.Println("Failed to play", err) 68 return 69 } 70 71 if res.MatchResult == Complete { 72 fmt.Println("Got valid, terminated DTMF entry", res.DTMF) 73 // hash is automatically trimmed from res.DTMF 74 } 75 } 76 77 func ExamplePrompt_custom() { 78 db := mockDB{} 79 c := &arimocks.Client{} 80 key := ari.NewKey(ari.ChannelKey, "exampleChannel") 81 h := ari.NewChannelHandle(key, c.Channel(), nil) 82 83 res, err := Prompt(context.TODO(), h, 84 URI("sound:agent-user"), 85 MatchFunc(func(in string) (string, MatchResult) { 86 // This is a custom match function which will 87 // be run each time a DTMF digit is received 88 pat := strings.TrimSuffix(in, "#") 89 90 user := db.Lookup(pat) 91 if user == "" { 92 if pat != in { 93 // pattern was hash-terminated but no match 94 // was found, so there is no match possible 95 return pat, Invalid 96 } 97 return in, Incomplete 98 } 99 return pat, Complete 100 }), 101 ).Result() 102 if err != nil { 103 fmt.Println("Failed to play prompt", err) 104 return 105 } 106 107 if res.MatchResult == Complete { 108 fmt.Println("Got valid user", res.DTMF) 109 } 110 } 111 112 type mockDB struct{} 113 114 func (m *mockDB) Lookup(user string) string { 115 return "" 116 }