-
Notifications
You must be signed in to change notification settings - Fork 44
/
helper.go
129 lines (116 loc) · 3.53 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (c) 2020 Percipia
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Contributor(s):
* Andrew Querol <[email protected]>
*/
package eslgo
import (
"context"
"errors"
"fmt"
"io"
"log"
"time"
"github.com/percipia/eslgo/command"
"github.com/percipia/eslgo/command/call"
)
func (c *Conn) EnableEvents(ctx context.Context) error {
var err error
if c.outbound {
_, err = c.SendCommand(ctx, command.MyEvents{
Format: "plain",
})
} else {
_, err = c.SendCommand(ctx, command.Event{
Format: "plain",
Listen: []string{"all"},
})
}
return err
}
// DebugEvents - A helper that will output all events to a logger
func (c *Conn) DebugEvents(w io.Writer) string {
logger := log.New(w, "EventLog: ", log.LstdFlags|log.Lmsgprefix)
return c.RegisterEventListener(EventListenAll, func(event *Event) {
logger.Println(event)
})
}
func (c *Conn) DebugOff(id string) {
c.RemoveEventListener(EventListenAll, id)
}
// Phrase - Executes the mod_dptools phrase app
func (c *Conn) Phrase(ctx context.Context, uuid, macro string, times int, wait bool) (*RawResponse, error) {
return c.audioCommand(ctx, "phrase", uuid, macro, times, wait)
}
// PhraseWithArg - Executes the mod_dptools phrase app with arguments
func (c *Conn) PhraseWithArg(ctx context.Context, uuid, macro string, argument interface{}, times int, wait bool) (*RawResponse, error) {
return c.audioCommand(ctx, "phrase", uuid, fmt.Sprintf("%s,%v", macro, argument), times, wait)
}
// Playback - Executes the mod_dptools playback app
func (c *Conn) Playback(ctx context.Context, uuid, audioArgs string, times int, wait bool) (*RawResponse, error) {
return c.audioCommand(ctx, "playback", uuid, audioArgs, times, wait)
}
// Say - Executes the mod_dptools say app
func (c *Conn) Say(ctx context.Context, uuid, audioArgs string, times int, wait bool) (*RawResponse, error) {
return c.audioCommand(ctx, "say", uuid, audioArgs, times, wait)
}
// Speak - Executes the mod_dptools speak app
func (c *Conn) Speak(ctx context.Context, uuid, audioArgs string, times int, wait bool) (*RawResponse, error) {
return c.audioCommand(ctx, "speak", uuid, audioArgs, times, wait)
}
// WaitForDTMF, waits for a DTMF event. Requires events to be enabled!
func (c *Conn) WaitForDTMF(ctx context.Context, uuid string) (byte, error) {
done := make(chan byte, 1)
listenerID := c.RegisterEventListener(uuid, func(event *Event) {
if event.GetName() == "DTMF" {
dtmf := event.GetHeader("DTMF-Digit")
if len(dtmf) > 0 {
select {
case done <- dtmf[0]:
default:
}
} else {
select {
case done <- 0:
default:
}
}
time.Sleep(10 * time.Millisecond)
}
})
defer func() {
c.RemoveEventListener(uuid, listenerID)
close(done)
}()
select {
case digit := <-done:
if digit != 0 {
return digit, nil
}
return digit, errors.New("invalid DTMF digit received")
case <-ctx.Done():
return 0, ctx.Err()
}
}
// Helper for mod_dptools apps since they are very similar in invocation
func (c *Conn) audioCommand(ctx context.Context, command, uuid, audioArgs string, times int, wait bool) (*RawResponse, error) {
response, err := c.SendCommand(ctx, &call.Execute{
UUID: uuid,
AppName: command,
AppArgs: audioArgs,
Loops: times,
Sync: wait,
})
if err != nil {
return response, err
}
if !response.IsOk() {
return response, errors.New(command + " response is not okay")
}
return response, nil
}