github.com/grokify/go-ringcentral-client@v0.3.31/office/v0/ringout.go (about)

     1  package legacy
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http"
     6  	"net/url"
     7  	"strconv"
     8  	"strings"
     9  
    10  	uu "github.com/grokify/mogo/net/urlutil"
    11  	scu "github.com/grokify/mogo/strconv/strconvutil"
    12  )
    13  
    14  const (
    15  	DocumentationURL = "https://success.ringcentral.com/articles/RC_Knowledge_Article/2080"
    16  	CmdCall          = "call"
    17  )
    18  
    19  var RingOutURL = "https://service.ringcentral.com/ringout.asp"
    20  
    21  func Call(params CallRequestInfo) (*CallResponseInfo, *http.Response, error) {
    22  	params.Command = CmdCall
    23  
    24  	roURL, err := uu.URLStringAddQuery(RingOutURL, params.Values(), false)
    25  	if err != nil {
    26  		return nil, nil, err
    27  	}
    28  	resp, err := http.Get(roURL.String())
    29  	if err != nil {
    30  		return nil, resp, err
    31  	}
    32  
    33  	body, err := ioutil.ReadAll(resp.Body)
    34  	if err != nil {
    35  		return nil, resp, err
    36  	}
    37  
    38  	resInfo := &CallResponseInfo{}
    39  	parts := strings.Split(string(body), " ")
    40  	if len(parts) == 3 {
    41  		resInfo.Status = strings.TrimSpace(parts[0])
    42  		resInfo.SessionID = strings.TrimSpace(parts[1])
    43  		resInfo.WS = strings.TrimSpace(parts[2])
    44  	}
    45  	return resInfo, resp, nil
    46  }
    47  
    48  type CallRequestInfoStrings struct {
    49  	Command   string `url:"cmd,omitempty"`
    50  	Username  string `url:"username,omitempty"`
    51  	Extension string `url:"ext,omitempty"`
    52  	Password  string `url:"password,omitempty"`
    53  	To        string `url:"to,omitempty"`
    54  	From      string `url:"from,omitempty"`
    55  	CallerID  string `url:"clid,omitempty"`
    56  	Prompt    string `url:"prompt,omitempty"`
    57  }
    58  
    59  func (s *CallRequestInfoStrings) ToCanonical() CallRequestInfo {
    60  	can := CallRequestInfo{
    61  		Command:  s.Command,
    62  		Password: s.Password,
    63  	}
    64  	if len(strings.TrimSpace(s.Username)) > 0 {
    65  		can.Username = scu.MustParseE164ToInt(s.Username)
    66  	}
    67  	if len(strings.TrimSpace(s.To)) > 0 {
    68  		can.To = scu.MustParseE164ToInt(s.To)
    69  	}
    70  	if len(strings.TrimSpace(s.From)) > 0 {
    71  		can.From = scu.MustParseE164ToInt(s.From)
    72  	}
    73  	if len(strings.TrimSpace(s.CallerID)) > 0 {
    74  		can.CallerID = scu.MustParseE164ToInt(s.CallerID)
    75  	}
    76  	if len(strings.TrimSpace(s.Extension)) > 0 {
    77  		can.Extension = scu.MustParseInt(s.Extension)
    78  	}
    79  	s.Prompt = strings.ToLower(strings.TrimSpace(s.Prompt))
    80  	if s.Prompt == "true" || s.Prompt == "1" {
    81  		can.Prompt = 1
    82  	}
    83  	return can
    84  }
    85  
    86  type CallRequestInfo struct {
    87  	Command   string `url:"cmd,omitempty"`
    88  	Username  int    `url:"username,omitempty"`
    89  	Extension int    `url:"ext,omitempty"`
    90  	Password  string `url:"password,omitempty"`
    91  	To        int    `url:"to,omitempty"`
    92  	From      int    `url:"from,omitempty"`
    93  	CallerID  int    `url:"clid,omitempty"`
    94  	Prompt    int    `url:"prompt,omitempty"`
    95  }
    96  
    97  func (req CallRequestInfo) Values() url.Values {
    98  	v := url.Values{}
    99  	if strings.TrimSpace(req.Command) != "" {
   100  		v.Add("cmd", req.Command)
   101  	}
   102  	if strings.TrimSpace(req.Command) != "" {
   103  		v.Add("cmd", req.Command)
   104  	}
   105  	if req.Username != 0 {
   106  		v.Add("username", strconv.Itoa(req.Username))
   107  	}
   108  	if req.Extension != 0 {
   109  		v.Add("extension", strconv.Itoa(req.Extension))
   110  	}
   111  	if req.To != 0 {
   112  		v.Add("to", strconv.Itoa(req.To))
   113  	}
   114  	if req.From != 0 {
   115  		v.Add("from", strconv.Itoa(req.From))
   116  	}
   117  	if req.CallerID != 0 {
   118  		v.Add("clid", strconv.Itoa(req.CallerID))
   119  	}
   120  	if req.Prompt != 0 {
   121  		v.Add("prompt", strconv.Itoa(req.Prompt))
   122  	}
   123  	return v
   124  }
   125  
   126  type CallResponseInfo struct {
   127  	Status    string
   128  	SessionID string
   129  	WS        string // can be .11
   130  }