async function main() {
// ====== Replace the variables below as needed ======
const tableId = "tblOrderxxxxxxxx"; // Orders table ID
const fldTrackingNo = "fldTrackingNoxxxx"; // Tracking number field ID
const wxWebhookUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=YOUR_KEY";
// 1. Create automation
const wf = await createWorkflow(
"WeCom notification for shipped orders",
"When order status becomes Shipped, send a message to the WeCom group"
);
const workflowId = wf.id;
// 2. Create a trigger node (listen to record updates)
// Simplified condition: current status == Shipped (if you need a transition check like Paid -> Shipped,
// configure previous/current according to what your API supports)
const triggerCfg = {
tableId,
filter: {
// Example expression: trigger only when the current status is "Shipped"
filterSet: [{
fieldId: fldStatus,
operator: "is",
value: "Shipped"
}],
conjunction: "and"
},
};
const trigger = await createTriggerNode(workflowId, triggerCfg);
// 3. Test the trigger to generate variables for downstream nodes
const outputVariable = await testNode(workflowId, trigger.id);
// 4. Create HTTP request action node (send to WeCom robot)
// Use variable placeholders: read fields from the trigger's record context
const httpCfg = {
method: "post",
url: {
resolvable: true,
type: "literal",
value: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=YOUR_KEY"
},
headers: [{
key: {
resolvable: true,
type: "literal",
value: "Content-Type"
},
value: {
resolvable: true,
type: "literal",
value: "application/json"
}
}],
bodyMode: "json",
body: {
type: "array",
resolvable: true,
nodes: [
{
resolvable: true,
type: "literal",
value: `{`
},
{
resolvable: true,
type: "literal",
value: "\"msgtype\": \"markdown\","
},
{
resolvable: true,
type: "literal",
value: "\"markdown\": {"
},
{
resolvable: true,
type: "literal",
value: "\"content\": \"Tracking No: "
},
{
// NodeId of the trigger; which node this variable comes from
"fact: "cmh8xxx",
// Variable hierarchy, taken from the variables returned by the test; see the test node response
"keyStack": ["$.record", "$.record.fields"],
params: [pipes: []],
// Placeholder for the tracking number variable
path: "$.record.fields.fldTrackingNoxxxx"
resolvable: true,
type: "fact"
},
{
resolvable: true,
type: "literal",
value: "\""
},
{
resolvable: true,
type: "literal",
value: "}"
},
{
resolvable: true,
type: "literal",
value: "}"
},
]
},
};
const httpNode = await createHttpNode(workflowId, trigger.id, httpCfg);
// 5. Test the HTTP node (will not actually listen to the trigger; just a one-time simulation/send of the current configuration)
// Note: Some environments may require providing a test record context; pass it according to your API
await testNode(workflowId, httpNode.id);
// 6. Enable automation
await toggleWorkflow(workflowId, true);
}
main().catch((e) => {
console.error("Failed to create automation", e);
});