-
Notifications
You must be signed in to change notification settings - Fork 44
/
helper_call.go
135 lines (115 loc) · 4.5 KB
/
helper_call.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
130
131
132
133
134
135
/*
* 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"
"strings"
"github.com/percipia/eslgo/command"
"github.com/percipia/eslgo/command/call"
)
// Leg This struct is used to specify the individual legs of a call for the originate helpers
type Leg struct {
CallURL string
LegVariables map[string]string
}
// OriginateCall - Calls the originate function in FreeSWITCH. If you want variables for each leg independently set them in the aLeg and bLeg
// Arguments: ctx context.Context for supporting context cancellation, background bool should we wait for the origination to complete
// aLeg, bLeg Leg The aLeg and bLeg of the call respectively
// vars map[string]string, channel variables to be passed to originate for both legs, contained in {}
func (c *Conn) OriginateCall(ctx context.Context, background bool, aLeg, bLeg Leg, vars map[string]string) (*RawResponse, error) {
if vars == nil {
vars = make(map[string]string)
}
if _, ok := vars["origination_uuid"]; ok {
// We cannot set origination uuid globally
delete(vars, "origination_uuid")
}
response, err := c.SendCommand(ctx, command.API{
Command: "originate",
Arguments: fmt.Sprintf("%s%s %s", BuildVars("{%s}", vars), aLeg.String(), bLeg.String()),
Background: background,
})
return response, err
}
// EnterpriseOriginateCall - Calls the originate function in FreeSWITCH using the enterprise method for calling multiple legs ":_:"
// If you want variables for each leg independently set them in the aLeg and bLeg strings
// Arguments: ctx context.Context for supporting context cancellation, background bool should we wait for the origination to complete
// vars map[string]string, channel variables to be passed to originate for both legs, contained in <>
// bLeg string The bLeg of the call
// aLegs ...string variadic argument for each aLeg to call
func (c *Conn) EnterpriseOriginateCall(ctx context.Context, background bool, vars map[string]string, bLeg Leg, aLegs ...Leg) (*RawResponse, error) {
if len(aLegs) == 0 {
return nil, errors.New("no aLeg specified")
}
if vars == nil {
vars = make(map[string]string)
}
if _, ok := vars["origination_uuid"]; ok {
// We cannot set origination uuid globally
delete(vars, "origination_uuid")
}
var aLeg strings.Builder
for i, leg := range aLegs {
if i > 0 {
aLeg.WriteString(":_:")
}
aLeg.WriteString(leg.String())
}
response, err := c.SendCommand(ctx, command.API{
Command: "originate",
Arguments: fmt.Sprintf("%s%s %s", BuildVars("<%s>", vars), aLeg.String(), bLeg.String()),
Background: background,
})
return response, err
}
// BackgroundOriginateCall - Calls the originate function in FreeSWITCH asynchronously. If you want variables for each leg independently set them in the aLeg and bLeg
// Arguments: ctx context.Context for supporting context cancellation, background bool should we wait for the origination to complete
// aLeg, bLeg Leg The aLeg and bLeg of the call respectively
// vars map[string]string, channel variables to be passed to originate for both legs, contained in {}
func (c *Conn) BackgroundOriginateCall(ctx context.Context, background bool, aLeg, bLeg Leg, vars map[string]string) (*RawResponse, error) {
if vars == nil {
vars = make(map[string]string)
}
if _, ok := vars["origination_uuid"]; ok {
// We cannot set origination uuid globally
delete(vars, "origination_uuid")
}
response, err := c.SendCommand(ctx, command.API{
Command: "bgapi originate",
Arguments: fmt.Sprintf("%s%s %s", BuildVars("{%s}", vars), aLeg.String(), bLeg.String()),
Background: background,
})
return response, err
}
// HangupCall - A helper to hangup a call asynchronously
func (c *Conn) HangupCall(ctx context.Context, uuid, cause string) error {
_, err := c.SendCommand(ctx, call.Hangup{
UUID: uuid,
Cause: cause,
Sync: false,
})
return err
}
// HangupCall - A helper to answer a call synchronously
func (c *Conn) AnswerCall(ctx context.Context, uuid string) error {
_, err := c.SendCommand(ctx, &call.Execute{
UUID: uuid,
AppName: "answer",
Sync: true,
})
return err
}
// String - Build the Leg string for passing to Bridge/Originate functions
func (l Leg) String() string {
return fmt.Sprintf("%s%s", BuildVars("[%s]", l.LegVariables), l.CallURL)
}