Skip to content

Commit

Permalink
remove deprecated mailgun-js in favor of axios (temporalio#130)
Browse files Browse the repository at this point in the history
* remove deprecated mailgun-js in favor of axios

Fix temporalio#123

* add a note about ADMIN_EMAIL

* run prettier

* clean up README, add .env.example, simplify createActivities()
  • Loading branch information
vkarpov15 authored Apr 14, 2022
1 parent d72ab5b commit 283e275
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
3 changes: 3 additions & 0 deletions timer-examples/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MAILGUN_API=
MAILGUN_DOMAIN=
ADMIN_EMAIL=
2 changes: 1 addition & 1 deletion timer-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions timer-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 21 additions & 15 deletions timer-examples/src/activities.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
await Context.current().sleep(sleepMS);
},

async sendNotificationEmail(): Promise<void> {
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);
}
},
});
10 changes: 3 additions & 7 deletions timer-examples/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { Worker } from '@temporalio/worker';
import dotenv from 'dotenv';
import mailgun from 'mailgun-js';
import { createActivities } from './activities';

dotenv.config();

const { MAILGUN_API: apiKey, MAILGUN_DOMAIN: domain, ADMIN_EMAIL: to } = process.env;

async function run(): Promise<void> {
let mg;
if (apiKey && domain) {
mg = mailgun({ apiKey, domain });
}

const activities = createActivities(mg, {
const activities = createActivities({
apiKey,
domain,
to,
from: `Temporal Bot <temporal@${domain}>`,
});
Expand Down

0 comments on commit 283e275

Please sign in to comment.