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

示例代码如下:

import EventBridge, * as $EventBridge from '@alicloud/eventbridge';
import Util from '@alicloud/tea-util';
import Console from '@alicloud/tea-console';
import * as $tea from '@alicloud/tea-typescript';

export default class Client {

    /**
     * Create client初始化公共请求参数。
     */
    static async createClient(): Promise<EventBridge> {
        let config = new $EventBridge.Config({ });
        // 您的AccessKey ID。
        config.accessKeyId = "<accessKeyId>";
        // 您的AccessKey Secret。
        config.accessKeySecret = "<accessKeySecret>";
        // 您的接入点。
        config.endpoint = "<endpoint>";
        return new EventBridge(config);
    }

    static async createEventRuleSample(client: EventBridge): Promise<void> {
        try {
            let createEventRuleRequest = new $EventBridge.CreateRuleRequest({ });
            let targetEntry = new $EventBridge.TargetEntry({ });
            targetEntry.id = "dingtalk.target";
            targetEntry.endpoint = "https://oapi.dingtalk.com/robot/send?access_token=1019d4a19e2ef6b2f***********396fc5e94814ed8460";
            targetEntry.type = "acs.dingtalk";
            let param1 = new $EventBridge.EBTargetParam({ });
            param1.resourceKey = "URL";
            param1.form = "CONSTANT";
            param1.value = "https://oapi.dingtalk.com/robot/send?access_token=1019d4a19e2ef6b2f***********396fc5e94814ed8460";
            let param2 = new $EventBridge.EBTargetParam({ });
            param2.resourceKey = "SecretKey";
            param2.form = "CONSTANT";
            param2.value = "SEC121a71ff304a65b4f7c**************1f4d9f6c1ca514300d15234";
            let param3 = new $EventBridge.EBTargetParam({ });
            param3.resourceKey = "Body";
            param3.form = "TEMPLATE";
            param3.value = `{\n    \"key\":\"$.source\",\n    \"value\":\"$.data\"\n}`;
            param3.template = "{\"msgtype\": \"text\",\"text\": {\"content\": \"Hello:${key}\"}}";
            let paramList = [
                param1,
                param2,
                param3
            ];
            targetEntry.paramList = paramList;
            let targetEntryList = [
                targetEntry
            ];
            createEventRuleRequest.targets = targetEntryList;
            createEventRuleRequest.ruleName = "myRule";
            createEventRuleRequest.eventBusName = "demo-bus";
            createEventRuleRequest.filterPattern = `{\"source\":[\"acs.oss\"],\"type\":[\"oss:ObjectCreated:UploadPart\"]}`;
            createEventRuleRequest.status = "enable";
            let resp = await client.createRule(createEventRuleRequest);
            Console.log("--------------------create rule success--------------------");
            Console.log(Util.toJSONString($tea.toMap(resp)));
        } catch (error) {
            Console.log(error.message);
        }
    }

    static async main(args: string[]): Promise<void> {
        let client = await Client.createClient();
        await Client.createEventRuleSample(client);
    }

}

Client.main(process.argv.slice(2));