抖音私信回复图片接口-企业号授权到开放平台-调用上传图片并发送私信消息
抖音私信回复图片接口
企业号授权到开放平台
调用上传图片并发送私信消息
这样用户就可以在客服后台,直接给私信用户发送图片了
感兴趣的+ \/ : llike620
golang代码
// 获取 ClientToken func (this *Douyin) GetClientToken() (string, error) { url := "https://open.douyin.com/oauth/client_token/" // 判断access_token是否过期,如果未过期则直接返回 cacheKey := "douyin_client_token" + this.ClientKey if clientToken, ok := douyinCache.Get(cacheKey); ok { return clientToken.(string), nil } // 构建请求参数 params := map[string]interface{}{ "grant_type": "client_credential", "client_key": this.ClientKey, "client_secret": this.ClientSecret, } // 创建HTTP请求的body jsonParams, err := json.Marshal(params) requestBody := bytes.NewBuffer(jsonParams) // 创建POST请求 req, err := http.NewRequest("POST", url, requestBody) if err != nil { fmt.Println("创建请求失败:", err) return "", err } // 设置请求头 req.Header.Set("Content-Type", "application/json") // 发送请求 client := http.Client{} response, err := client.Do(req) if err != nil { fmt.Println("发送请求失败:", err) return "", err } defer response.Body.Close() // 读取响应 responseBody, err := ioutil.ReadAll(response.Body) log.Println("抖音获取client_token:", string(responseBody)) if err != nil { return "", err } clientToken := gjson.Get(string(responseBody), "data.access_token").String() if clientToken != "" { douyinCache.Set(cacheKey, clientToken, 30*time.Minute) } return clientToken, nil }
// 上传图片 func (this *Douyin) ReplyImageMessage(access_token, eventType, open_id, to_user_id, conversation_id, msg_id, filePath string) (string, error) { url := "https://open.douyin.com/tool/imagex/client_upload/" clientToken, _ := this.GetClientToken() // 打开文件 file, err := os.Open(filePath) if err != nil { log.Println("Error opening file:", err) return "", err } defer file.Close() // 创建一个buffer来存储表单数据 body := &bytes.Buffer{} writer := multipart.NewWriter(body) // 添加文件到表单数据 part, err := writer.CreateFormFile("image", "filename.jpg") // 假设文件名为filename.jpg if err != nil { log.Println("Error creating form file:", err) return "", err } _, err = io.Copy(part, file) // 结束表单编码 err = writer.Close() if err != nil { log.Println("Error closing multipart writer:", err) return "", err } // 创建请求 req, err := http.NewRequest("POST", url, body) if err != nil { log.Println("Error creating request:", err) return "", err } // 设置请求头 req.Header.Add("Content-Type", writer.FormDataContentType()) req.Header.Add("access-token", clientToken) // 发送请求 client := &http.Client{} resp, err := client.Do(req) if err != nil { log.Println("Error sending request:", err) return "", err } defer resp.Body.Close() bodyBytes, _ := ioutil.ReadAll(resp.Body) log.Println("抖音上传图片:", string(bodyBytes)) image_id := gjson.Get(string(bodyBytes), "image_id").String() if image_id == "" { return "", errors.New("抖音上传图片失败") } url2 := "https://open.douyin.com/im/send/msg/?open_id=" + open_id // 构建请求参数 params := map[string]interface{}{ "content": map[string]interface{}{ "msg_type": 2, "image": map[string]interface{}{ "media_id": image_id, }, }, "scene": eventType, "conversation_id": conversation_id, "msg_id": msg_id, "to_user_id": to_user_id, } // 创建HTTP请求的body jsonParams, err := json.Marshal(params) requestBody := bytes.NewBuffer(jsonParams) log.Println("调用抖音私信图片接口:", string(jsonParams)) // 创建POST请求 req, err = http.NewRequest("POST", url2, requestBody) if err != nil { fmt.Println("创建请求失败:", err) return "", err } // 设置请求头 req.Header.Set("Content-Type", "application/json") req.Header.Set("access-token", access_token) // 发送请求 client = &http.Client{} response, err := client.Do(req) if err != nil { fmt.Println("发送请求失败:", err) return "", err } defer response.Body.Close() // 读取响应 responseBody, err := ioutil.ReadAll(response.Body) log.Println("调用抖音私信图片接口:", string(responseBody)) if err != nil { return "", err } return string(responseBody), nil }
十年开发经验程序员,离职全心创业中,历时三年开发出的产品《唯一客服系统》
一款基于Golang+Vue开发的在线客服系统,软件著作权编号:2021SR1462600。一套可私有化部署的网站在线客服系统,编译后的二进制文件可直接使用无需搭开发环境,下载zip解压即可,仅依赖MySQL数据库,是一个开箱即用的全渠道在线客服系统,致力于帮助广大开发者/公司快速部署整合私有化客服功能。
开源地址:唯一客服(开源学习版)
官网地址:唯一客服官网