Skip to content

Commit 8367c2f

Browse files
committed
feat: 新增 MyWorkflow 類別並整合至事件路由,更新 wrangler 設定以支援工作流程
1 parent 37bb1ee commit 8367c2f

5 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/_index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { router } from "./router";
2+
import { MyWorkflow } from "./workflows/message";
23

34
export default {
45
async fetch(request: Request, env: Env, ctx: ExecutionContext) {

src/eventRouter.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { version } from "../package.json";
44
import type { webhook } from "@line/bot-sdk";
55
import type { Message } from "./types";
66

7+
import { MyWorkflow } from "./workflows/message";
8+
79
export async function eventRouter(event: webhook.Event, channelAccessToken: string, ctx: ExecutionContext): Promise<Message[]> {
810
// Log
911
let eventType: string = event.type;
@@ -79,6 +81,15 @@ export async function eventRouter(event: webhook.Event, channelAccessToken: stri
7981
},
8082
];
8183
}
84+
case "workflow": {
85+
const instance = await env.MY_WORKFLOW.create();
86+
return [
87+
{
88+
type: "text",
89+
text: `Instance ID: ${instance.id}`,
90+
},
91+
];
92+
}
8293
default: {
8394
return [
8495
{

src/workflows/message.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from "cloudflare:workers";
2+
type Params = {};
3+
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
4+
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
5+
step.do("my first step", async () => {
6+
console.log("first step");
7+
});
8+
step.do("my second step", async () => {
9+
console.log("second step");
10+
});
11+
}
12+
}

worker-configuration.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/* eslint-disable */
2-
// Generated by Wrangler by running `wrangler types` (hash: d0e3937095cad28325a958a570d1d1c6)
2+
// Generated by Wrangler by running `wrangler types` (hash: bc6cbc783ec64491f2d887c020fc9e3e)
33
// Runtime types generated with workerd@1.20260526.1 2026-01-28 nodejs_compat
44
interface __BaseEnv_Env {
55
CF_VERSION_METADATA: WorkerVersionMetadata;
66
ASSETS: Fetcher;
77
timezone: "Asia/Taipei";
88
LINE_CHANNEL_SECRET: string;
99
LINE_CHANNEL_ACCESS_TOKEN: string;
10+
MY_WORKFLOW: Workflow /* MyWorkflow */;
1011
}
1112
declare namespace Cloudflare {
1213
interface GlobalProps {

wrangler.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"vars": {
1010
"timezone": "Asia/Taipei",
1111
},
12+
"workflows": [{ "binding": "MY_WORKFLOW", "class_name": "MyWorkflow", "name": "MyWorkflow" }],
1213
"workers_dev": false,
1314
"preview_urls": true,
1415
"routes": [{ "custom_domain": true, "pattern": "lwt.ccepr.dev" }],

0 commit comments

Comments
 (0)