Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/notification-twilio #9648

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

420coupe
Copy link
Contributor

Attempting to extend notification service to add twilio as an sms/mms provider. However i am having issues trying to test locally. Carlos has provided the package.json resolutions as an option but it does not seem to be pulling all local changes to my cloned medusa repo.

Copy link

changeset-bot bot commented Oct 17, 2024

⚠️ No Changeset found

Latest commit: 169683a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Oct 17, 2024

@420coupe is attempting to deploy a commit to the medusajs Team on Vercel.

A member of the Team first needs to authorize it.

@420coupe
Copy link
Contributor Author

420coupe commented Oct 19, 2024

So here's a working notification-twilio integration.

Pre-reqs:

  • Have a twilio account
  • Purchase a sms/mms capable phone number
  • create messaging service (optional)

There is also a process to register your Brand, get A2P10DC verified, and finally submit for campaign approval for your use case in production.

TwilioServiceConfig has 3 required properties 1 of which has 2 options

  • accountSid: string (required)
  • authToken: string (required)
  • from?: string (either from phone# (+13055555555) or messagingServiceSid required)
  • messagingServiceSid?: string (either messageingServiceSid or from required)

You can provide both messagingServiceSid and from, if phone number is in messaging_service_sid's sender pool. and you want to use a specific phone number, otherwise allow messaging service to use senderpool.

.env example

TWILIO_ACCOUNT_SID=
TWILIO_AUTH_TOKEN=
TWILIO_FROM=
TWILIO_MESSAGING_SERVICE_SID=

medusa-config.js

//imports...
module.exports = defineConfig({
// projectConfig settings
modules: {
[Modules.NOTIFICATION]: {
      resolve: "@medusajs/medusa/notification",
      options: {
        providers: [
          {
            resolve: "@medusajs/medusa/notification-twilio",
            id: "twilio",
            options: {
              channels: ["sms"],
              account_sid: process.env.TWILIO_ACCOUNT_SID,
              auth_token: process.env.TWILIO_AUTH_TOKEN,
              messaging_service_sid: process.env.TWILIO_MESSAGING_SERVICE_SID,
              from: process.env.TWILIO_FROM,
            },
          },
        ],
      },
    },
   }) 

send an SMS message using Twilio example subscriber for product created below

subscriber/product-created.ts

import type { SubscriberArgs, SubscriberConfig } from "@medusajs/framework";
import { Modules } from "@medusajs/framework/utils";
import { INotificationModuleService } from "@medusajs/framework/types";

export default async function productCreateHandler({
  event: { data },
  container,
}: SubscriberArgs<{ id: string }>) {
  const notificationModuleService: INotificationModuleService =
    container.resolve(Modules.NOTIFICATION);

  // SMS/MMS Notification Content
  const smsData = {
    mediaUrls: [], // array of mediaUrls to send MMS
  };

  const smsContent = {
    text: `Test text: product with ID ${data.id} has been created`,
  };

  await notificationModuleService.createNotifications({
    to: "3055555555",
    channel: "sms",
    data: smsData,
    content: smsContent,
    template: "sms-test",
  });

export const config: SubscriberConfig = {
  event: "product.created",
};

Next create a product and viola you should have received a sms notification.
Template is not used on twilio's api side only internally for medusa notification table.

@420coupe 420coupe marked this pull request as ready for review October 19, 2024 02:22
@420coupe 420coupe requested a review from a team as a code owner October 19, 2024 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant