增加翻译功能
This commit is contained in:
@@ -70,6 +70,7 @@ else()
|
||||
/opt/lua54/lib/liblua.a
|
||||
/usr/local/lib/libtencentcloud-sdk-cpp-core.so
|
||||
/usr/local/lib/libtencentcloud-sdk-cpp-trtc.so
|
||||
/usr/local/lib/libtencentcloud-sdk-cpp-tmt.so
|
||||
|
||||
hpsocket
|
||||
ylib
|
||||
|
||||
3
build.sh
3
build.sh
@@ -3,6 +3,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
# 编译 SDK
|
||||
mkdir 3rdparty
|
||||
cd "$SCRIPT_DIR/3rdparty"
|
||||
if [ ! -f tencentcloud-sdk-cpp-master.zip ]; then
|
||||
wget https://download.fwlua.com/other/tencentcloud-sdk-cpp-master.zip
|
||||
@@ -12,7 +13,7 @@ rm -rf tencentcloud-sdk-cpp-master
|
||||
unzip tencentcloud-sdk-cpp-master.zip
|
||||
cd tencentcloud-sdk-cpp-master
|
||||
# 后面加入了哪个功能再修改
|
||||
./build.sh build -DBUILD_MODULES="trtc"
|
||||
./build.sh build -DBUILD_MODULES="trtc;tmt"
|
||||
./build.sh install
|
||||
|
||||
# 安装
|
||||
|
||||
15
src/tencent_sdk.cpp
Normal file
15
src/tencent_sdk.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "basemodule.h"
|
||||
#include "tencent_sdk_tmt.h"
|
||||
#include "tencent_sdk_trtc.h"
|
||||
extern "C" {
|
||||
#ifdef _WIN32
|
||||
DLL_EXPORT
|
||||
#endif
|
||||
int fastweb_module_regist(void* sol2, void* lua)
|
||||
{
|
||||
sol::state* state = static_cast<sol::state*>(sol2);
|
||||
module::tencent_sdk_tmt::regist(state);
|
||||
module::tencent_sdk_trtc::regist(state);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
83
src/tencent_sdk_tmt.cpp
Normal file
83
src/tencent_sdk_tmt.cpp
Normal file
@@ -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;
|
||||
}
|
||||
35
src/tencent_sdk_tmt.h
Normal file
35
src/tencent_sdk_tmt.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
#include "basemodule.h"
|
||||
#include <utility>
|
||||
namespace module
|
||||
{
|
||||
class tencent_sdk_tmt : public module::base {
|
||||
public:
|
||||
tencent_sdk_tmt();
|
||||
~tencent_sdk_tmt() override;
|
||||
static void regist(sol::state* lua);
|
||||
|
||||
|
||||
/**
|
||||
* @brief 文本翻译
|
||||
* @param secret_id 密钥ID
|
||||
* @param secret_key 密钥
|
||||
* @param source_text 源文本
|
||||
* @param source_language 源语言
|
||||
* @param target_language 目标语言
|
||||
* @return 是否成功
|
||||
*/
|
||||
std::pair<bool,std::string> 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
|
||||
);
|
||||
|
||||
private:
|
||||
virtual void regist_global(const char* name, sol::state* lua);
|
||||
virtual void delete_global() { delete this; }
|
||||
};
|
||||
}
|
||||
|
||||
@@ -17,18 +17,6 @@ using namespace TencentCloud;
|
||||
using namespace TencentCloud::Trtc::V20190722;
|
||||
using namespace TencentCloud::Trtc::V20190722::Model;
|
||||
|
||||
extern "C" {
|
||||
#ifdef _WIN32
|
||||
DLL_EXPORT
|
||||
#endif
|
||||
int fastweb_module_regist(void* sol2, void* lua)
|
||||
{
|
||||
sol::state* state = static_cast<sol::state*>(sol2);
|
||||
module::tencent_sdk_trtc::regist(state);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
module::tencent_sdk_trtc::tencent_sdk_trtc()
|
||||
{
|
||||
|
||||
|
||||
24
target/tencent_sdk_tmt.lua
Normal file
24
target/tencent_sdk_tmt.lua
Normal file
@@ -0,0 +1,24 @@
|
||||
local tencent_sdk_tmt = {}
|
||||
tencent_sdk_tmt.__index = tencent_sdk_tmt
|
||||
|
||||
--[[
|
||||
创建一个新的 tencent_tmt 对象
|
||||
@return 返回一个新的 tencent_sdk_tmt 对象
|
||||
]]
|
||||
function tencent_sdk_tmt.new(db)
|
||||
local instance = setmetatable({}, tencent_sdk_tmt)
|
||||
if db == nil then
|
||||
instance.module = fw_tencent_sdk_tmt.new()
|
||||
else
|
||||
instance.module = db
|
||||
end
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
|
||||
function tencent_sdk_tmt:text(secret_id,secret_key,source_text,source_language,target_language)
|
||||
return self.module:text(secret_id,secret_key,source_text,source_language,target_language)
|
||||
end
|
||||
|
||||
return tencent_sdk_tmt
|
||||
Reference in New Issue
Block a user