diff --git a/timer-examples/.env.example b/timer-examples/.env.example new file mode 100644 index 00000000..89de3a8b --- /dev/null +++ b/timer-examples/.env.example @@ -0,0 +1,3 @@ +MAILGUN_API= +MAILGUN_DOMAIN= +ADMIN_EMAIL= \ No newline at end of file diff --git a/timer-examples/README.md b/timer-examples/README.md index e2a1d521..c216277d 100644 --- a/timer-examples/README.md +++ b/timer-examples/README.md @@ -9,7 +9,7 @@ This example shows how to use [`sleep`](https://typescript.temporal.io/api/names 1. Make sure Temporal Server is running locally (see the [quick install guide](https://docs.temporal.io/docs/server/quick-install/)). 1. `npm install` to install dependencies. -1. _Optional_: Set up an account with [Mailgun](https://www.mailgun.com/) and create a `.env` file with the following environment variables: `MAILGUN_API`, `MAILGUN_DOMAIN`, and `ADMIN_EMAIL` +1. _Optional_: Set up an account with [Mailgun](https://www.mailgun.com/) and create a `.env` file with the following environment variables: `MAILGUN_API`, `MAILGUN_DOMAIN`, and `ADMIN_EMAIL`. `ADMIN_EMAIL` is the email address your Mailgun emails will be sent to. You can use the `.env.example` file as a template. 1. `npm run start.watch` to start the Worker. ### Email notification diff --git a/timer-examples/package.json b/timer-examples/package.json index 41f2fa33..614c87af 100644 --- a/timer-examples/package.json +++ b/timer-examples/package.json @@ -22,13 +22,12 @@ ] }, "dependencies": { + "axios": "0.26.1", "dotenv": "^10.0.0", - "mailgun-js": "^0.22.0", "temporalio": "0.20.x" }, "devDependencies": { "@tsconfig/node16": "^1.0.0", - "@types/mailgun-js": "0.22.12", "@typescript-eslint/eslint-plugin": "^5.0.0", "@typescript-eslint/parser": "^5.0.0", "eslint": "^7.32.0", diff --git a/timer-examples/src/activities.ts b/timer-examples/src/activities.ts index 58b2f356..1342c5d3 100644 --- a/timer-examples/src/activities.ts +++ b/timer-examples/src/activities.ts @@ -1,31 +1,37 @@ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { Context } from '@temporalio/activity'; -import mailgun from 'mailgun-js'; +import axios from 'axios'; -export interface EmailSettings { - to: string | undefined; +interface MailgunSettings { + apiKey?: string; + domain?: string; + to?: string; from: string; } +const mailgunAPI = 'https://api.mailgun.net/v3'; +const subject = 'Order processing taking longer than expected'; const html = `Order processing is taking longer than expected, but don't worry—the job is still running!`; -export const createActivities = (mg: mailgun.Mailgun | undefined, { to, from }: EmailSettings) => ({ +export const createActivities = ({ apiKey, domain, to, from }: MailgunSettings) => ({ async processOrder(sleepMS = 1000): Promise { await Context.current().sleep(sleepMS); }, async sendNotificationEmail(): Promise { - console.log('Sending email:', html); - - if (mg && to) { - const data = { - to, - from, - subject: 'Order processing taking longer than expected', - html, - }; - - await mg.messages().send(data); + if (apiKey && domain && to) { + console.log('Sending email:', html); + await axios({ + url: `${mailgunAPI}/${domain}/messages`, + method: 'post', + params: { to, from, subject, html }, + auth: { + username: 'api', + password: apiKey, + }, + }); + } else { + console.log('Skipping sending email:', html); } }, }); diff --git a/timer-examples/src/worker.ts b/timer-examples/src/worker.ts index 07533ba0..4f6292f6 100644 --- a/timer-examples/src/worker.ts +++ b/timer-examples/src/worker.ts @@ -1,6 +1,5 @@ import { Worker } from '@temporalio/worker'; import dotenv from 'dotenv'; -import mailgun from 'mailgun-js'; import { createActivities } from './activities'; dotenv.config(); @@ -8,12 +7,9 @@ dotenv.config(); const { MAILGUN_API: apiKey, MAILGUN_DOMAIN: domain, ADMIN_EMAIL: to } = process.env; async function run(): Promise { - let mg; - if (apiKey && domain) { - mg = mailgun({ apiKey, domain }); - } - - const activities = createActivities(mg, { + const activities = createActivities({ + apiKey, + domain, to, from: `Temporal Bot `, });