avatar.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package aliyun
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. avatar20220130 "github.com/alibabacloud-go/avatar-20220130/v2/client"
  7. openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  8. util "github.com/alibabacloud-go/tea-utils/v2/service"
  9. "github.com/alibabacloud-go/tea/tea"
  10. "strconv"
  11. "strings"
  12. "wechat-api/internal/utils/jwt"
  13. )
  14. // CreateClient 使用AK&SK初始化账号Client
  15. // @return Client
  16. // @throws Exception
  17. func CreateClient(accessKeyId, accessKeySecret string) (_result *avatar20220130.Client, _err error) {
  18. // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
  19. // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html。
  20. config := &openapi.Config{
  21. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
  22. AccessKeyId: tea.String(accessKeyId),
  23. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  24. AccessKeySecret: tea.String(accessKeySecret),
  25. }
  26. // Endpoint 请参考 https://api.aliyun.com/product/avatar
  27. config.Endpoint = tea.String("avatar.cn-zhangjiakou.aliyuncs.com")
  28. _result = &avatar20220130.Client{}
  29. _result, _err = avatar20220130.NewClient(config)
  30. return _result, _err
  31. }
  32. // StartInstance 启动数字人 userId=1234 bizId=xxoo
  33. func StartInstance(userId uint64, jsonData AiTypes) (*avatar20220130.StartInstanceResponseBodyData, error) {
  34. accessKeyId := jsonData.AccessKeyId
  35. accessKeySecret := jsonData.AccessKeySecret
  36. tenantId := jsonData.TenantId
  37. appId := jsonData.AppId
  38. bizId := GenerateBizID(userId)
  39. userIdStr := strconv.Itoa(int(userId))
  40. client, _err := CreateClient(accessKeyId, accessKeySecret)
  41. if _err != nil {
  42. return nil, _err
  43. }
  44. startInstanceRequest := &avatar20220130.StartInstanceRequest{
  45. TenantId: tea.Int64(int64(tenantId)),
  46. App: &avatar20220130.StartInstanceRequestApp{
  47. AppId: tea.String(appId),
  48. },
  49. User: &avatar20220130.StartInstanceRequestUser{
  50. UserId: tea.String(userIdStr),
  51. },
  52. BizId: tea.String(bizId),
  53. }
  54. _result, _err := client.StartInstance(startInstanceRequest)
  55. if _err != nil {
  56. return nil, _err
  57. }
  58. if *_result.Body.Success {
  59. fmt.Println(_result.Body.Data)
  60. return _result.Body.Data, nil
  61. }
  62. return nil, errors.New(*_result.Body.Message)
  63. }
  64. func _main(args []*string) (_err error) {
  65. client, _err := CreateClient("", "")
  66. if _err != nil {
  67. return _err
  68. }
  69. sendMessageRequest := &avatar20220130.SendMessageRequest{
  70. SessionId: tea.String("your_value"),
  71. TenantId: tea.Int64(1),
  72. Feedback: tea.Bool(false),
  73. }
  74. tryErr := func() (_e error) {
  75. defer func() {
  76. if r := tea.Recover(recover()); r != nil {
  77. _e = r
  78. }
  79. }()
  80. // 复制代码运行请自行打印 API 的返回值
  81. _, _err = client.SendMessageWithOptions(sendMessageRequest, &util.RuntimeOptions{})
  82. if _err != nil {
  83. return _err
  84. }
  85. return nil
  86. }()
  87. if tryErr != nil {
  88. var error = &tea.SDKError{}
  89. if _t, ok := tryErr.(*tea.SDKError); ok {
  90. error = _t
  91. } else {
  92. error.Message = tea.String(tryErr.Error())
  93. }
  94. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  95. // 错误 message
  96. fmt.Println(tea.StringValue(error.Message))
  97. // 诊断地址
  98. var data interface{}
  99. d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
  100. d.Decode(&data)
  101. if m, ok := data.(map[string]interface{}); ok {
  102. recommend, _ := m["Recommend"]
  103. fmt.Println(recommend)
  104. }
  105. _, _err = util.AssertAsString(error.Message)
  106. if _err != nil {
  107. return _err
  108. }
  109. }
  110. return _err
  111. }
  112. // GenerateBizID 业务请求id,最大支持 64 位字符
  113. // (注意 可以使用该字段做业务启动数字人的幂等,服务端确保同一个 bizId 多次调用只会开启一路实例)
  114. // 鉴于以上原因这里确保每个用户只启动一个实例
  115. func GenerateBizID(userId uint64) string {
  116. return jwt.HashidsEncode(int(userId))
  117. }
  118. // GetAccessKeyId 获取AccessKeyId
  119. func GetAccessKeyId() string {
  120. return "LTAI5tET2mMQVTcMDftuM8Cp"
  121. }
  122. // GetAccessKeySecret 获取AccessKeySecret
  123. func GetAccessKeySecret() string {
  124. return "AmGsD8VzwEuj8njZpUCUYnK7uUBTJa"
  125. }
  126. // GetAppId 获取AppId
  127. func GetAppId() string {
  128. return "3dt553bl52"
  129. }
  130. // GetTenantId 租户ID
  131. func GetTenantId() int64 {
  132. return int64(27174)
  133. }
  134. //func main() {
  135. // err := _main(tea.StringSlice(os.Args[1:]))
  136. // if err != nil {
  137. // panic(err)
  138. // }
  139. //}