|
@@ -0,0 +1,83 @@
|
|
|
|
|
+#include "tencent_sdk_tmt.h"
|
|
|
|
|
+#include "dll_interface.h"
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+#include <tencentcloud/core/Credential.h>
|
|
|
|
|
+#include <tencentcloud/core/profile/ClientProfile.h>
|
|
|
|
|
+#include <tencentcloud/core/profile/HttpProfile.h>
|
|
|
|
|
+#include <tencentcloud/tmt/v20180321/TmtClient.h>
|
|
|
|
|
+#include <tencentcloud/tmt/v20180321/model/TextTranslateRequest.h>
|
|
|
|
|
+#include <tencentcloud/tmt/v20180321/model/TextTranslateResponse.h>
|
|
|
|
|
+#include <iostream>
|
|
|
|
|
+#include <string>
|
|
|
|
|
+#include <vector>
|
|
|
|
|
+
|
|
|
|
|
+using namespace TencentCloud;
|
|
|
|
|
+using namespace TencentCloud::Tmt::V20180321;
|
|
|
|
|
+using namespace TencentCloud::Tmt::V20180321::Model;
|
|
|
|
|
+using namespace std;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+module::tencent_sdk_tmt::tencent_sdk_tmt()
|
|
|
|
|
+{
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+module::tencent_sdk_tmt::~tencent_sdk_tmt()
|
|
|
|
|
+{
|
|
|
|
|
+ // cos_http_io_deinitialize();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+std::pair<bool,std::string> module::tencent_sdk_tmt::text(const std::string& secret_id,const std::string& secret_key,const std::string& source_text,const std::string& source_language,const std::string& target_language)
|
|
|
|
|
+{
|
|
|
|
|
+// 密钥信息从环境变量读取,需要提前在环境变量中设置 TENCENTCLOUD_SECRET_ID 和 TENCENTCLOUD_SECRET_KEY
|
|
|
|
|
+ // 使用环境变量方式可以避免密钥硬编码在代码中,提高安全性
|
|
|
|
|
+ // 生产环境建议使用更安全的密钥管理方案,如密钥管理系统(KMS)、容器密钥注入等
|
|
|
|
|
+ // 请参见:https://cloud.tencent.com/document/product/1278/85305
|
|
|
|
|
+ // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
|
|
|
|
|
+ Credential cred = Credential(secret_id.c_str(), secret_key.c_str());
|
|
|
|
|
+ // 使用临时密钥示例
|
|
|
|
|
+ // Credential cred = Credential("SecretId", "SecretKey", "Token");
|
|
|
|
|
+ // 实例化一个http选项,可选的,没有特殊需求可以跳过
|
|
|
|
|
+ HttpProfile httpProfile = HttpProfile();
|
|
|
|
|
+ httpProfile.SetEndpoint("tmt.tencentcloudapi.com");
|
|
|
|
|
+
|
|
|
|
|
+ // 实例化一个client选项,可选的,没有特殊需求可以跳过
|
|
|
|
|
+ ClientProfile clientProfile = ClientProfile();
|
|
|
|
|
+ clientProfile.SetHttpProfile(httpProfile);
|
|
|
|
|
+ // 实例化要请求产品的client对象,clientProfile是可选的
|
|
|
|
|
+ TmtClient client = TmtClient(cred, "ap-beijing", clientProfile);
|
|
|
|
|
+
|
|
|
|
|
+ // 实例化一个请求对象,每个接口都会对应一个request对象
|
|
|
|
|
+ TextTranslateRequest req = TextTranslateRequest();
|
|
|
|
|
+ req.SetSourceText(source_text.c_str());
|
|
|
|
|
+ req.SetSource(source_language.c_str());
|
|
|
|
|
+ req.SetTarget(target_language.c_str());
|
|
|
|
|
+ req.SetProjectId(0);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 返回的resp是一个TextTranslateResponse的实例,与请求对象对应
|
|
|
|
|
+ auto outcome = client.TextTranslate(req);
|
|
|
|
|
+ if (!outcome.IsSuccess())
|
|
|
|
|
+ {
|
|
|
|
|
+ return std::make_pair(false, outcome.GetError().PrintAll());
|
|
|
|
|
+ }
|
|
|
|
|
+ TextTranslateResponse resp = outcome.GetResult();
|
|
|
|
|
+ // 输出json格式的字符串回包
|
|
|
|
|
+ return std::make_pair(true, resp.ToJsonString());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void module::tencent_sdk_tmt::regist(sol::state* lua)
|
|
|
|
|
+{
|
|
|
|
|
+ lua->new_usertype<module::tencent_sdk_tmt>("fw_tencent_sdk_tmt",
|
|
|
|
|
+ "new", sol::constructors<module::tencent_sdk_tmt()>(),
|
|
|
|
|
+ "text", &module::tencent_sdk_tmt::text
|
|
|
|
|
+ );
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void module::tencent_sdk_tmt::regist_global(const char* name, sol::state* lua)
|
|
|
|
|
+{
|
|
|
|
|
+ lua->registry()[name] = this;
|
|
|
|
|
+ (*lua)[name] = this;
|
|
|
|
|
+}
|