client.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "os"
  11. "strings"
  12. )
  13. // CreateClient 使用AK&SK初始化账号Client
  14. // @return Client
  15. // @throws Exception
  16. func CreateClient() (_result *avatar20220130.Client, _err error) {
  17. // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
  18. // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html。
  19. config := &openapi.Config{
  20. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
  21. AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
  22. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  23. AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
  24. }
  25. // Endpoint 请参考 https://api.aliyun.com/product/avatar
  26. config.Endpoint = tea.String("avatar.cn-zhangjiakou.aliyuncs.com")
  27. _result = &avatar20220130.Client{}
  28. _result, _err = avatar20220130.NewClient(config)
  29. return _result, _err
  30. }
  31. // userId=23423 bizId=xxxxxxx
  32. func startInstance(userId, bizId string) (*avatar20220130.StartInstanceResponseBodyData, error) {
  33. client, _err := CreateClient()
  34. if _err != nil {
  35. return nil, _err
  36. }
  37. tenantId := int64(9185)
  38. appId := "ALIPUB5748B17121450"
  39. startInstanceRequest := &avatar20220130.StartInstanceRequest{
  40. TenantId: tea.Int64(tenantId),
  41. App: &avatar20220130.StartInstanceRequestApp{
  42. AppId: tea.String(appId),
  43. },
  44. User: &avatar20220130.StartInstanceRequestUser{
  45. UserId: tea.String(userId),
  46. },
  47. BizId: tea.String(bizId),
  48. }
  49. _result, _err := client.StartInstance(startInstanceRequest)
  50. if _err != nil {
  51. return nil, _err
  52. }
  53. if *_result.Body.Success {
  54. fmt.Println(_result.Body.Data)
  55. return _result.Body.Data, nil
  56. }
  57. return nil, errors.New(*_result.Body.Message)
  58. }
  59. func _main(args []*string) (_err error) {
  60. client, _err := CreateClient()
  61. if _err != nil {
  62. return _err
  63. }
  64. sendMessageRequest := &avatar20220130.SendMessageRequest{
  65. SessionId: tea.String("your_value"),
  66. TenantId: tea.Int64(1),
  67. Feedback: tea.Bool(false),
  68. }
  69. tryErr := func() (_e error) {
  70. defer func() {
  71. if r := tea.Recover(recover()); r != nil {
  72. _e = r
  73. }
  74. }()
  75. // 复制代码运行请自行打印 API 的返回值
  76. _, _err = client.SendMessageWithOptions(sendMessageRequest, &util.RuntimeOptions{})
  77. if _err != nil {
  78. return _err
  79. }
  80. return nil
  81. }()
  82. if tryErr != nil {
  83. var error = &tea.SDKError{}
  84. if _t, ok := tryErr.(*tea.SDKError); ok {
  85. error = _t
  86. } else {
  87. error.Message = tea.String(tryErr.Error())
  88. }
  89. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  90. // 错误 message
  91. fmt.Println(tea.StringValue(error.Message))
  92. // 诊断地址
  93. var data interface{}
  94. d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
  95. d.Decode(&data)
  96. if m, ok := data.(map[string]interface{}); ok {
  97. recommend, _ := m["Recommend"]
  98. fmt.Println(recommend)
  99. }
  100. _, _err = util.AssertAsString(error.Message)
  101. if _err != nil {
  102. return _err
  103. }
  104. }
  105. return _err
  106. }
  107. //func main() {
  108. // err := _main(tea.StringSlice(os.Args[1:]))
  109. // if err != nil {
  110. // panic(err)
  111. // }
  112. //}