VcoiceCall.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package http
  2. import (
  3. "FollowUp_Notice/conf"
  4. "FollowUp_Notice/lib"
  5. "FollowUp_Notice/logs"
  6. "crypto/hmac"
  7. "crypto/sha256"
  8. "encoding/base64"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "github.com/go-resty/resty/v2"
  13. uuid "github.com/satori/go.uuid"
  14. "strings"
  15. "time"
  16. )
  17. var (
  18. //appKey = conf.VoiceCall_AppKey // 语音通知应用的appKey,购买服务时下发,请替换为实际值
  19. //appSecret = conf.VoiceCall_AppSecret // 语音通知应用的appSecret,购买服务时下发,请替换为实际值
  20. baseUrl = "https://rtccall.cn-north-1.myhuaweicloud.cn:443"
  21. appKey = "Zy8539Sa61Iv5fo5iGNWLcV24KI1"
  22. appSecret = "KNZ3OFv8eVF7zO5smbr0160WS4Az"
  23. template = "1101dc0ebc154a5a9e7c4e5f11bfdb41"
  24. phone = "+8675536362854"
  25. )
  26. func buildAKSKHeader(appKey string, appSecret string) string {
  27. now := time.Now().UTC().Format("2006-01-02T15:04:05Z") // Created
  28. nonce := strings.ReplaceAll(fmt.Sprintf("%s", uuid.NewV1()), "-", "") // Nonce
  29. digest := hmac.New(sha256.New, []byte(appSecret))
  30. digest.Write([]byte(nonce + now))
  31. digestBase64 := base64.StdEncoding.EncodeToString(digest.Sum(nil)) // PasswordDigest
  32. return fmt.Sprintf(`UsernameToken Username="%s",PasswordDigest="%s",Nonce="%s",Created="%s"`, appKey, digestBase64, nonce, now)
  33. }
  34. type PlayInfo struct {
  35. TemplateID string `json:"templateId"`
  36. TemplateParas []string `json:"templateParas"`
  37. CollectInd int `json:"collectInd"`
  38. }
  39. type VoiceNotifyRequest struct {
  40. DisplayNbr string `json:"displayNbr"`
  41. CalleeNbr string `json:"calleeNbr"`
  42. PlayInfoList []PlayInfo `json:"playInfoList"`
  43. }
  44. type VoiceRes struct {
  45. Resultcode string `json:"resultcode"`
  46. Resultdesc string `json:"resultdesc"`
  47. SessionId string `json:"sessionId"`
  48. }
  49. func VoiceNotifyAPI(displayNbr string, calleeNbr string, playInfoList []PlayInfo) (VoiceRes, error) {
  50. if len(displayNbr) < 1 || len(calleeNbr) < 1 || playInfoList == nil {
  51. return VoiceRes{}, errors.New("参数错误")
  52. }
  53. apiUri := "/rest/httpsessions/callnotify/v2.0" // v1.0 or v2.0
  54. //requestUrl := conf.VoiceCall_BaseUrl + apiUri
  55. requestUrl := baseUrl + apiUri
  56. jsonData := VoiceNotifyRequest{
  57. DisplayNbr: displayNbr,
  58. CalleeNbr: calleeNbr,
  59. PlayInfoList: playInfoList,
  60. }
  61. //jsonBytes, _ := json.Marshal(jsonData)
  62. client := resty.New()
  63. resp, err := client.R().
  64. SetHeader("Content-Type", "application/json;charset=UTF-8").
  65. SetHeader("Authorization", `AKSK realm="SDP",profile="UsernameToken",type="Appkey"`).
  66. SetHeader("X-AKSK", buildAKSKHeader(appKey, appSecret)).
  67. SetBody(jsonData).
  68. Post(requestUrl)
  69. if err != nil {
  70. return VoiceRes{}, err
  71. }
  72. temp := VoiceRes{}
  73. if err = json.Unmarshal(resp.Body(), &temp); err != nil {
  74. logs.Error(lib.FuncName(), err)
  75. return VoiceRes{}, err
  76. }
  77. return temp, nil
  78. }
  79. func GetPlayInfoList(templateId string, templateParas []string) []PlayInfo {
  80. playInfoList := []PlayInfo{
  81. {
  82. TemplateID: templateId,
  83. TemplateParas: templateParas,
  84. CollectInd: 0,
  85. },
  86. }
  87. return playInfoList
  88. }
  89. // 满意度调查
  90. func GetSatisfactionPlayInfoList(templateParas []string) []PlayInfo {
  91. playInfoList := []PlayInfo{
  92. {
  93. TemplateID: conf.VoiceCall_Satisfaction_Template1,
  94. TemplateParas: templateParas,
  95. CollectInd: 1,
  96. },
  97. {
  98. TemplateID: conf.VoiceCall_Satisfaction_Template2,
  99. TemplateParas: []string{""},
  100. CollectInd: 1,
  101. },
  102. {
  103. TemplateID: conf.VoiceCall_Satisfaction_Template3,
  104. TemplateParas: []string{""},
  105. CollectInd: 1,
  106. },
  107. {
  108. TemplateID: conf.VoiceCall_Satisfaction_Template4,
  109. TemplateParas: []string{""},
  110. CollectInd: 1,
  111. },
  112. {
  113. TemplateID: conf.VoiceCall_Satisfaction_Template5,
  114. TemplateParas: []string{""},
  115. CollectInd: 1,
  116. },
  117. }
  118. return playInfoList
  119. }