本文介绍如何查询实时记录的任务状态和结果。
基于上一步提交任务后返回的TaskId来查询处理结果。
如通过此方式轮询查询结果,注意轮询频率不要过高,以免被限流。比如您可以按每1分钟或每5分钟的频率持续查询。
使用回调通知的方式来获取结果,不同于主动轮询,您可以在提交任务后,等待服务端处理完成时主动地通知您任务状态。
当前我们支持通过HTTP或MQ的形式将任务处理状态通知到您。
查询任务状态和结果
查询任务时,您需要将“提交任务”时返回的TaskId作为输入,发起查询请求,根据返回的状态判断任务是否已完成。
请求参数
参数名 | 类型 | 是否必填 | 说明 |
TaskId | string | 是 | 您提交任务时返回的TaskId信息 |
示例代码
#!/usr/bin/env python
#coding=utf-8
import os
import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from aliyunsdkcore.auth.credentials import AccessKeyCredential
def create_common_request(domain, version, protocolType, method, uri):
request = CommonRequest()
request.set_accept_format('json')
request.set_domain(domain)
request.set_version(version)
request.set_protocol_type(protocolType)
request.set_method(method)
request.set_uri_pattern(uri)
request.add_header('Content-Type', 'application/json')
return request
# TODO 请通过环境变量设置您的 AccessKeyId 和 AccessKeySecret
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
client = AcsClient(region_id='cn-beijing', credential=credentials)
uri = '/openapi/tingwu/v2/tasks' + '/' + '请输入您提交任务时返回的TaskId'
request = create_common_request('tingwu.cn-beijing.aliyuncs.com', '2023-09-30', 'https', 'GET', uri)
response = client.do_action_with_exception(request)
print("response: \n" + json.dumps(json.loads(response), indent=4, ensure_ascii=False))
package com.alibaba.tingwu.client.demo.common;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import org.junit.Test;
/**
* @author tingwu2023
* @desc 演示了通过OpenAPI 根据TaskId查询任务状态和结果 的调用方式。
*/
public class GetTaskInfoTest {
@Test
public void getTaskInfo() throws ClientException {
String taskId = "请输入创建任务(含离线转写、实时会议)的TaskId";
String queryUrl = String.format("/openapi/tingwu/v2/tasks" + "/%s", taskId);
CommonRequest request = createCommonRequest("tingwu.cn-beijing.aliyuncs.com", "2023-09-30", ProtocolType.HTTPS, MethodType.GET, queryUrl);
// TODO 请通过环境变量设置您的AccessKeyId、AccessKeySecret
DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
IAcsClient client = new DefaultAcsClient(profile);
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
}
public static CommonRequest createCommonRequest(String domain, String version, ProtocolType protocolType, MethodType method, String uri) {
// 创建API请求并设置参数
CommonRequest request = new CommonRequest();
request.setSysDomain(domain);
request.setSysVersion(version);
request.setSysProtocol(protocolType);
request.setSysMethod(method);
request.setSysUriPattern(uri);
request.setHttpContentType(FormatType.JSON);
return request;
}
}
package main
import (
"encoding/json"
"log"
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)
type GetTaskInfoResponse struct {
RequestId string `json:"RequestId"`
Code string `json:"Code"`
Message string `json:"Message"`
Data struct {
TaskId string `json:"TaskId"`
TaskKey string `json:"TaskKey"`
TaskStatus string `json:"TaskStatus"`
} `json:"Data"`
}
func get_task_info(taskid string, akkey string, aksecret string) (*GetTaskInfoResponse, string, error) {
client, err := sdk.NewClientWithAccessKey("cn-beijing", akkey, aksecret)
if err != nil {
return nil, "", err
}
request := requests.NewCommonRequest()
request.Method = "GET"
request.Domain = "tingwu.cn-beijing.aliyuncs.com"
request.Version = "2023-09-30"
request.PathPattern = "/openapi/tingwu/v2/tasks/" + taskid
request.SetScheme("https")
log.Default().Print("request task:", taskid)
response, err := client.ProcessCommonRequest(request)
if err != nil {
return nil, "", err
}
log.Default().Print("response body:\n", string(response.GetHttpContentBytes()))
resp := new(GetTaskInfoResponse)
err = json.Unmarshal(response.GetHttpContentBytes(), resp)
if err != nil {
return nil, response.GetHttpContentString(), err
}
return resp, response.GetHttpContentString(), nil
}
func main() {
akkey := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
aksecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
_, raw, _ := get_task_info("请输入创建任务(含离线转写、实时会议)的TaskId", akkey, aksecret)
log.Default().Println("response :", raw)
}
#include <cstdlib>
#include <iostream>
#include <string>
#include <alibabacloud/core/AlibabaCloud.h>
#include <alibabacloud/core/CommonRequest.h>
#include <alibabacloud/core/CommonClient.h>
#include <alibabacloud/core/CommonResponse.h>
/**
* @author tingwu2023
* @desc 演示了通过OpenAPI 根据TaskId查询任务状态和结果 的调用方式。
*/
int main( int argc, char** argv ) {
std::string taskId = "请输入创建任务(含离线转写、实时记录)的TaskId";
AlibabaCloud::InitializeSdk();
AlibabaCloud::ClientConfiguration configuration( "cn-beijing" );
// specify timeout when create client.
configuration.setConnectTimeout(1500);
configuration.setReadTimeout(4000);
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
AlibabaCloud::Credentials credential( getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") );
AlibabaCloud::CommonClient client( credential, configuration );
AlibabaCloud::CommonRequest request(AlibabaCloud::CommonRequest::RequestPattern::RoaPattern);
request.setHttpMethod(AlibabaCloud::HttpRequest::Method::Get);
request.setDomain("tingwu.cn-beijing.aliyuncs.com");
request.setVersion("2023-09-30");
request.setHeaderParameter("Content-Type", "application/json");
request.setResourcePath("/openapi/tingwu/v2/tasks/" + taskId);
auto response = client.commonResponse(request);
if (response.isSuccess()) {
printf("request success.\n");
printf("result: %s\n", response.result().payload().c_str());
} else {
printf("error: %s\n", response.error().errorMessage().c_str());
printf("request id: %s\n", response.error().requestId().c_str());
}
AlibabaCloud::ShutdownSdk();
return 0;
}
示例输出
当任务仍在运行中时:
{
"Code":"0",
"Data":{
"TaskId":"e8adc0b3bc4b42d898fcadb0********",
"TaskStatus":"ONGOING"
},
"Message":"success",
"RequestId":"5fa32fc6-441f-4dd1-bb86-c030********"
}
当任务在运行中但有部分结果时:
{
"Code":"0",
"Data":{
"TaskId":"e8adc0b3bc4b42d898fcadb0********",
"TaskStatus":"ONGOING",
"OutputMp3Path":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_20231101141801.mp3?ExLTAI4G4uXHLPwQHj6oX8****AccessKeyId=LTAI4G4uXHLPwQHj6oX8****&Signature=********JBMijH7wLq0xX6aivHc%3D",
"Result":{
"Transcription":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu_data/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_Transcription_20231101141926.json?Expires=1698906034&OSSAcceLTAI4G4uXHLPwQHj6oX8****6oX8nt5S&Signature=********NJlqSEWJxfkMwjwsHCA%3D"
}
},
"Message":"success",
"RequestId":"1b20e0d9-c55c-4cc3-85af-80b4********"
}
当任务已完成时:
{
"Code":"0",
"Data":{
"TaskId":"e8adc0b3bc4b42d898fcadb0********",
"TaskStatus":"COMPLETED",
"OutputMp3Path":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_20231101141801.mp3?ExLTAI4G4uXHLPwQHj6oX8****AccessKeyId=LTAI4G4uXHLPwQHj6oX8****&Signature=********JBMijH7wLq0xX6aivHc%3D",
"Result":{
"AutoChapters":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu_data/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_AutoChapters_20231101141955.json?Expires=1698906034&OSSAcceLTAI4G4uXHLPwQHj6oX8****6oX8nt5S&Signature=********Ax9FvifYAO8dj4qzWg%3D",
"Transcription":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu_data/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_Transcription_20231101141926.json?Expires=1698906034&OSSAccessKeyId=LTAI4G4uXHLPwQHj6oX8****&Signature=********NJlqSEWJxfkMwjwsHCA%3D"
}
},
"Message":"success",
"RequestId":"1b20e0d9-c55c-4cc3-85af-80b4********"
}
当任务失败时:
{
"Code":"0",
"Data":{
"TaskId":"b76389677b1441fa82165cb1********",
"TaskStatus":"FAILED",
"ErrorCode":"TSC.AudioFileLink",
"ErrorMessage":"Audio file link invalid."
},
"Message":"success",
"RequestId":"d181d898-b627-4040-b7c9-9563********"
}
回调通知任务状态和结果
如果您希望通过回调方式来获取任务状态和结果,需要在控制台配置好回调类型和地址,创建实时记录任务时将参数AppKey配置为对应的项目AppKey,同时将参数Input.ProgressiveCallbacksEnabled设为true。
当您选择http回调方式时,我们将设置一个最大请求时间限制。这意味着,如果我们在发起请求后的超过5秒钟内没有收到您的回复,我们将不再等待返回结果,系统判断回调请求发送失败,稍后将会进行重试。
我们建议您审核和调整您的系统,以确保能够在5秒钟内响应我们的回调请求。这可能包括但不限于:
优化您的处理逻辑,确保高效执行;
对不必要的长操作进行异步处理,不在主回调响应路径中等待;
实施错误处理和重试机制,以应对超时或失败的情况。
回调重试机制说明:
当回调消息发送失败时,听悟服务端会在5秒钟后重新发送一次回调消息,如果第二次发送仍然失败,则会在之后的每5分钟再发送一次,重试次数最多为3次。
示例输出
当单个AI模型子任务完成时:
{
"Code":"0",
"Data":{
"TaskId":"e8adc0b3bc4b42d898fcadb0********",
"TaskStatus":"ONGOING",
"TaskKey":"TingwuDemo",
"Result":{
"Transcription":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu_data/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_Transcription_20231101141926.json?Expires=1698906034&OSSAcceLTAI4G4uXHLPwQHj6oX8****6oX8nt5S&Signature=********NJlqSEWJxfkMwjwsHCA%3D"
}
},
"Message":"success",
"RequestId":"5cf2b587ac6e41e6a1f06ca1********"
}
当任务完成时:
{
"Code":"0",
"Data":{
"TaskId":"e8adc0b3bc4b42d898fcadb0********",
"TaskStatus":"COMPLETED",
"TaskKey":"TingwuDemo",
"Result":{
"AutoChapters":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu_data/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_AutoChapters_20231101141955.json?Expires=1698906034&OSSAcceLTAI4G4uXHLPwQHj6oX8****6oX8nt5S&Signature=********Ax9FvifYAO8dj4qzWg%3D",
"Transcription":"http://speech-swap.oss-cn-zhangjiakou.aliyuncs.com/tingwu_data/output/1738248129743478/e8adc0b3bc4b42d898fcadb0a1710635/e8adc0b3bc4b42d898fcadb0a1710635_Transcription_20231101141926.json?Expires=1698906034&OSSAccessKeyId=LTAI4G4uXHLPwQHj6oX8****&Signature=********NJlqSEWJxfkMwjwsHCA%3D"
}
},
"Message":"success",
"RequestId":"9c60b07f152445349eaa6161********"
}
当任务失败时:
{
"Code":"0",
"Data":{
"TaskId":"e8adc0b3bc4b42d898fcadb0********",
"TaskStatus":"FAILED",
"TaskKey":"TingwuDemo",
"ErrorCode":"TSC.AudioFileLink",
"ErrorMessage":"Audio file link invalid."
},
"Message":"success",
"RequestId":"5e1c0babe36844e49f1b7bee********"
}