本文介绍如何使用C++ SDK创建一个事件规则。

示例代码如下:

#include <alibabacloud/event_bridge.hpp>
#include <alibabacloud/sample.hpp>
#include <darabonba/console.hpp>
#include <darabonba/core.hpp>
#include <darabonba/util.hpp>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

using namespace Alibabacloud_Sample;

Alibabacloud_EventBridge::Client Alibabacloud_Sample::Client::createClient() {
  shared_ptr<Alibabacloud_EventBridge::Config> config =
      make_shared<Alibabacloud_EventBridge::Config>();
  //AccessKey ID。
  config->accessKeyId = make_shared<string>("<accessKeyId>");
  //AccessKey Secret。
  config->accessKeySecret = make_shared<string>("<accessKeySecret>");
  //接入点。
  config->endpoint = make_shared<string>("<endpoint>");
  return Alibabacloud_EventBridge::Client(config);
}

void Alibabacloud_Sample::Client::createEventRuleSample(
    shared_ptr<Alibabacloud_EventBridge::Client> client) {
  try {
    shared_ptr<Alibabacloud_EventBridge::CreateRuleRequest>
        createEventRuleRequest =
            make_shared<Alibabacloud_EventBridge::CreateRuleRequest>();
    shared_ptr<Alibabacloud_EventBridge::TargetEntry> targetEntry =
        make_shared<Alibabacloud_EventBridge::TargetEntry>();
    targetEntry->id = make_shared<string>("dingtalk.target");
    targetEntry->endpoint = make_shared<string>(
        "https://oapi.dingtalk.com/robot/"
        "send?access_token=1019d4a19e2ef6b2f***********396fc5e94814ed8460");
    targetEntry->type = make_shared<string>("acs.dingtalk");
    shared_ptr<Alibabacloud_EventBridge::EBTargetParam> param1 =
        make_shared<Alibabacloud_EventBridge::EBTargetParam>();
    param1->resourceKey = make_shared<string>("URL");
    param1->form = make_shared<string>("CONSTANT");
    param1->value = make_shared<string>(
        "https://oapi.dingtalk.com/robot/"
        "send?access_token=1019d4a19e2ef6b2f***********396fc5e94814ed8460");
    shared_ptr<Alibabacloud_EventBridge::EBTargetParam> param2 =
        make_shared<Alibabacloud_EventBridge::EBTargetParam>();
    param2->resourceKey = make_shared<string>("SecretKey");
    param2->form = make_shared<string>("CONSTANT");
    param2->value = make_shared<string>(
        "SEC121a71ff304a65b4f7c**************1f4d9f6c1ca514300d15234");
    shared_ptr<Alibabacloud_EventBridge::EBTargetParam> param3 =
        make_shared<Alibabacloud_EventBridge::EBTargetParam>();
    param3->resourceKey = make_shared<string>("Body");
    param3->form = make_shared<string>("TEMPLATE");
    param3->value = make_shared<string>(
        string("{\n    \"key\":\"$.source\",\n    \"value\":\"$.data\"\n}"));
    param3->template_ =
        make_shared<string>("{" msgtype ": " text "," text ": {" content
                            ": " Hello:$ { key } "}}");
    shared_ptr<vector<Alibabacloud_EventBridge::EBTargetParam>> paramList =
        make_shared<vector<Alibabacloud_EventBridge::EBTargetParam>>(
            vector<Alibabacloud_EventBridge::EBTargetParam>(
                {param1, param2, param3}));
    targetEntry->paramList = paramList;
    shared_ptr<vector<Alibabacloud_EventBridge::TargetEntry>> targetEntryList =
        make_shared<vector<Alibabacloud_EventBridge::TargetEntry>>(
            vector<Alibabacloud_EventBridge::TargetEntry>({targetEntry}));
    createEventRuleRequest->targets = targetEntryList;
    createEventRuleRequest->ruleName = make_shared<string>("myRule");
    createEventRuleRequest->eventBusName = make_shared<string>("demo-bus");
    createEventRuleRequest->filterPattern =
        make_shared<string>(string("{\"source\":[\"acs.oss\"],\"type\":[\"oss:"
                                   "ObjectCreated:UploadPart\"]}"));
    createEventRuleRequest->status = make_shared<string>("enable");
    shared_ptr<Alibabacloud_EventBridge::CreateRuleResponse> resp =
        make_shared<Alibabacloud_EventBridge::CreateRuleResponse>(
            client->createRule(createEventRuleRequest));
    Darabonba_Console::Client::log(make_shared<string>(
        "--------------------create rule success--------------------"));
    Darabonba_Console::Client::log(
        make_shared<string>(Darabonba_Util::Client::toJSONString(
            make_shared<map<string, boost::any>>(resp->toMap()))));
  } catch (std::exception &error) {
    Darabonba_Console::Client::log(error.message);
  }
}

void Alibabacloud_Sample::Client::main(shared_ptr<vector<string>> args) {
  shared_ptr<Alibabacloud_EventBridge::Client> client =
      make_shared<Alibabacloud_EventBridge::Client>(Client::createClient());
  Client::createEventRuleSample(client);
}