VcoiceCall.go 2.9 KB

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