From 7a9286b2653b09942e4ee98f6cc4f50a97ae4878 Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Sun, 14 Jan 2024 18:35:21 +0700 Subject: [PATCH] :recycle: refactor: updated function relating to CDR #14 --- pkg/ami/ami_cdr.go | 29 +++++++++-------- pkg/ami/ami_model.go | 49 +++++++++++++++-------------- pkg/ami/ami_originate.go | 67 +++++++++++++++++++++++----------------- 3 files changed, 78 insertions(+), 67 deletions(-) diff --git a/pkg/ami/ami_cdr.go b/pkg/ami/ami_cdr.go index 9da4dc2..0af01de 100644 --- a/pkg/ami/ami_cdr.go +++ b/pkg/ami/ami_cdr.go @@ -2,7 +2,6 @@ package ami import ( "fmt" - "log" "strconv" "strings" "time" @@ -213,13 +212,13 @@ func (r *AMICdr) SetType(value string) *AMICdr { return r } -func (r *AMICdr) SetUserExtension(value string) *AMICdr { - r.UserExtension = TrimStringSpaces(value) +func (r *AMICdr) SetExtension(value string) *AMICdr { + r.Extension = TrimStringSpaces(value) return r } -func (r *AMICdr) SetPhoneNumber(value string) *AMICdr { - r.PhoneNumber = TrimStringSpaces(value) +func (r *AMICdr) SetNumber(value string) *AMICdr { + r.Number = TrimStringSpaces(value) return r } @@ -228,8 +227,8 @@ func (r *AMICdr) SetSymbol(value string) *AMICdr { return r } -func (r *AMICdr) SetPlaybackUrl(value string) *AMICdr { - r.PlaybackUrl = value +func (r *AMICdr) SetMediaLink(value string) *AMICdr { + r.MediaLink = value return r } @@ -351,8 +350,8 @@ func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr { r.SetDesc(flow) r.SetDirection(config.AmiOutboundDirection) r.SetType(config.AmiTypeOutboundNormalDirection) - r.SetUserExtension(strings.Split(r.Channel, r.symbol)[0]) - r.SetPhoneNumber(phone) + r.SetExtension(strings.Split(r.Channel, r.symbol)[0]) + r.SetNumber(phone) } else { var inCase bool = false // from outbound chan-spy @@ -362,7 +361,7 @@ func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr { r.SetDesc(flow) r.SetType(config.AmiTypeChanSpyDirection) r.SetDirection(config.AmiOutboundDirection) - r.SetUserExtension(strings.Split(r.Channel, r.symbol)[0]) + r.SetExtension(strings.Split(r.Channel, r.symbol)[0]) } // from inbound dial if strings.EqualFold(r.LastApplication, config.AmiLastApplicationDial) { @@ -371,8 +370,8 @@ func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr { r.SetDesc(flow) r.SetDirection(config.AmiInboundDirection) r.SetType(config.AmiTypeInboundDialDirection) - r.SetUserExtension(strings.Split(r.DestinationChannel, r.symbol)[0]) - r.SetPhoneNumber(r.Source) + r.SetExtension(strings.Split(r.DestinationChannel, r.symbol)[0]) + r.SetNumber(r.Source) } // from inbound queue if strings.EqualFold(r.LastApplication, config.AmiLastApplicationQueue) { @@ -381,11 +380,11 @@ func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr { r.SetDesc(flow) r.SetDirection(config.AmiInboundDirection) r.SetType(config.AmiTypeInboundQueueDirection) - r.SetUserExtension(strings.Split(r.Channel, r.symbol)[0]) - r.SetPhoneNumber(r.Source) + r.SetExtension(strings.Split(r.Channel, r.symbol)[0]) + r.SetNumber(r.Source) } if !inCase { - log.Printf("ParseCdr, CDR got an error exception case:: %v", JsonString(r)) + D().Error("ParseCdr, CDR got an error exception case:: %v", JsonString(r)) } } return r diff --git a/pkg/ami/ami_model.go b/pkg/ami/ami_model.go index 4de80f5..2389c63 100644 --- a/pkg/ami/ami_model.go +++ b/pkg/ami/ami_model.go @@ -195,21 +195,21 @@ type AMIPayloadQueue struct { // ChannelId - Channel UniqueId to be set on the channel. // OtherChannelId - Channel UniqueId to be set on the second local channel. type AMIOriginate struct { - Channel string `ami:"Channel,omitempty"` - Exten string `ami:"Exten,omitempty"` - Context string `ami:"Context,omitempty"` - Priority int `ami:"Priority,omitempty"` - Application string `ami:"Application,omitempty"` - Data string `ami:"Data,omitempty"` - Timeout int `ami:"Timeout,omitempty"` - CallerID string `ami:"CallerID,omitempty"` - Variable []string `ami:"Variable,omitempty"` - Account string `ami:"Account,omitempty"` - EarlyMedia string `ami:"EarlyMedia,omitempty"` - Async string `ami:"Async,omitempty"` - Codecs string `ami:"Codecs,omitempty"` - ChannelID string `ami:"ChannelId,omitempty"` - OtherChannelID string `ami:"OtherChannelId,omitempty"` + Channel string `ami:"Channel,omitempty"` + Exten string `ami:"Exten,omitempty"` + Context string `ami:"Context,omitempty"` + Priority int `ami:"Priority,omitempty"` + Application string `ami:"Application,omitempty"` + Data string `ami:"Data,omitempty"` + Timeout int `ami:"Timeout,omitempty"` + CallerID string `ami:"CallerID,omitempty"` + Variable string `ami:"Variable,omitempty"` + Account string `ami:"Account,omitempty"` + EarlyMedia string `ami:"EarlyMedia,omitempty"` + Async string `ami:"Async,omitempty"` + Codecs string `ami:"Codecs,omitempty"` + ChannelID string `ami:"ChannelId,omitempty"` + OtherChannelID string `ami:"OtherChannelId,omitempty"` } // AMIPayloadCall holds the call data to transfer. @@ -339,12 +339,13 @@ type AMIPayloadDb struct { } type AMIDialCall struct { - Telephone string `json:"telephone" binding:"required"` // like customer phone number or no. extension internal from all routes - ChannelProtocol string `json:"channel_protocol" binding:"required"` // protocols include: SIP, H323, IAX... - Extension int `json:"extension" binding:"required"` // like current user using no. extension - DebugMode bool `json:"debug_mode"` // allow to trace log - Timeout int `json:"timeout"` // set timeout while calling - ExtensionExists bool `json:"extension_exists"` // allow validate the extension and channel + ChannelProtocol string `json:"channel_protocol" binding:"required"` // protocols include: SIP, H323, IAX... + Telephone string `json:"telephone" binding:"required"` // just customer phone number or no. extension internal from all routes + Extension int `json:"extension" binding:"required"` // just current user using no. extension + Timeout int `json:"timeout"` // set timeout while calling + Variables map[string]string `json:"variables,omitempty"` // external variables configuration which returned by event + DebugMode bool `json:"debug_mode"` // allow to trace log + ExtensionExists bool `json:"extension_exists"` // allow validate the extension and channel } type AMIExtensionStatesConf struct { @@ -444,9 +445,9 @@ type AMICdr struct { Direction string `json:"direction"` // inbound, outbound Desc string `json:"desc"` Type string `json:"type"` - UserExtension string `json:"user_extension,omitempty"` - PhoneNumber string `json:"phone_number,omitempty"` - PlaybackUrl string `json:"playback_url,omitempty"` // the only cdr has status answered + Extension string `json:"extension,omitempty"` + Number string `json:"number,omitempty"` + MediaLink string `json:"media_link,omitempty"` // the only cdr has status answered symbol string // default extension splitter symbol: -, example: SIP/1000-00098fec then split by - } diff --git a/pkg/ami/ami_originate.go b/pkg/ami/ami_originate.go index de3c0c6..a714a49 100644 --- a/pkg/ami/ami_originate.go +++ b/pkg/ami/ami_originate.go @@ -76,33 +76,14 @@ func (o *AMIOriginate) SetCallerId(value string) *AMIOriginate { return o } -func (o *AMIOriginate) SetVar(value ...string) *AMIOriginate { - o.Variable = append(o.Variable, value...) - return o +// "custom_parameter1=value1" +func (o *AMIOriginate) SetVariable(value string) { + o.Variable = value } -func (c *AMIOriginate) SetVars(delimiter string, variables ...string) *AMIOriginate { - if strings.EqualFold(delimiter, "") { - delimiter = ";" - } - vars := strings.Join(variables, delimiter) - c.SetVar(vars) - return c -} - -func (c *AMIOriginate) SetVarsMap(delimiter string, variables map[string]interface{}) *AMIOriginate { - if len(variables) <= 0 { - return c - } - _vars := make([]string, len(variables)) - - for k, v := range variables { - str := fmt.Sprintf("%s=%v", k, v) - _vars = append(_vars, str) - } - - c.SetVars(delimiter, _vars...) - return c +// "custom_parameter1=value1" & "custom_parameter2=value2" +func (o *AMIOriginate) SetMultipleVariables(variables ...string) { + o.Variable = strings.Join(variables, "&") } func (o *AMIOriginate) SetAccount(value string) *AMIOriginate { @@ -168,11 +149,39 @@ func (o *AMIDialCall) SetExtensionExists(value bool) *AMIDialCall { return o } +func (o *AMIDialCall) SetVariables(values map[string]string) *AMIDialCall { + o.Variables = values + return o +} + func (o *AMIDialCall) Json() string { return JsonString(o) } -// DialCall +func (o *AMIDialCall) OfVars() []string { + var vars []string + if len(o.Variables) == 0 { + return vars + } + for k, v := range o.Variables { + vars = append(vars, fmt.Sprintf("%v=%v", k, v)) + } + return vars +} + +func GetAmiDialCallSample() *AMIDialCall { + a := NewAmiDialCall() + a.SetDebugMode(true).SetExtensionExists(true) + a.SetChannelProtocol("SIP"). + SetExtension(1001). + SetTelephone("012345678"). + SetTimeout(30000). + SetVariables(map[string]string{ + "key": "123", + }) + return a +} + func DialCall(ctx context.Context, s AMISocket, originate AMIOriginate) (AmiReply, error) { return Originate(ctx, s, originate) } @@ -196,6 +205,7 @@ func DialOut(ctx context.Context, s AMISocket, d AMIDialCall) (AmiReply, bool, e SetContext(config.AmiContextOutbound). SetExtension(strings.TrimSpace(d.Telephone)). SetChannel(channel.JoinChannelWith(channel.ChannelProtocol, fmt.Sprintf("%v", d.Extension))) + o.SetMultipleVariables(d.OfVars()...) if d.ExtensionExists { peer, err := SIPPeerStatusShort(ctx, s, fmt.Sprintf("%v", d.Extension)) @@ -209,7 +219,7 @@ func DialOut(ctx context.Context, s AMISocket, d AMIDialCall) (AmiReply, bool, e } if d.DebugMode { D().Info("DialOut, an outgoing call with originate request body: %v", o.Json()) - D().Info("DialOut, an outgoing call with original request body (setter): %v", d.Json()) + D().Info("DialOut, an outgoing call with original request body: %v", d.Json()) } response, err := DialCall(ctx, s, *o) return response, IsSuccess(response), err @@ -234,6 +244,7 @@ func DialIn(ctx context.Context, s AMISocket, d AMIDialCall) (AmiReply, bool, er SetContext(config.AmiContextFromInternal). SetExtension(strings.TrimSpace(d.Telephone)). SetChannel(channel.JoinChannelWith(channel.ChannelProtocol, fmt.Sprintf("%v", d.Extension))) + o.SetMultipleVariables(d.OfVars()...) if d.ExtensionExists { peer, err := SIPPeerStatusShort(ctx, s, fmt.Sprintf("%v", d.Extension)) @@ -247,7 +258,7 @@ func DialIn(ctx context.Context, s AMISocket, d AMIDialCall) (AmiReply, bool, er } if d.DebugMode { D().Info("DialIn, an internal call with originate request body: %v", o.Json()) - D().Info("DialIn, an internal call with original request body (setter): %v", d.Json()) + D().Info("DialIn, an internal call with original request body: %v", d.Json()) } response, err := DialCall(ctx, s, *o) return response, IsSuccess(response), err