简体   繁体   中英

how to send a message with Twilio in an expo project (React Native)

I am trying to send an SMS notification to a user when a button is pressed in my react native application. However, it is yielding in an error. here is my code:

const account = '*********************************';
const auth = '*******************************';

const client = require('twilio')(account,auth);

handlePress(phone, name){
    client.messages.create({
        to: 'phone',
        from: '************',
        body: 'Hi' + name , this is Hangtime Sports Academy',
    })
}

note that this is an expo project.

error on pc:

The package at "node_modules\twilio\lib\rest\Twilio.js" attempted to import the Node standard library module "url". It failed because React Native does not include the Node standard library.

error on phone:

unable to resolve 'url' from node_modules\twilio\lib\rest\Twilio.js

Any idea why this is happening?

import twilio from 'twilio';
import { hotp } from 'otplib';
import jwt from 'jsonwebtoken';
import { AUTH_KIND } from '../../utils/app.constants';

export default class AuthService {
  twAccountSid: string = config.twAccountSid as string;
  twToken: string = config.twToken as string;
  totpSecret: string = config.totpSecret as string;

  constructor() {}

  public async sendOTP(contactNumber: string, token: string) {
    const twClient = twilio(this.twAccountSid, this.twToken);
    return new Promise((resolve, reject) => {
      twClient.messages
        .create({
          from: config.twPrimaryFromNumber,
          to: contactNumber,
          body: `Your code is ${token}`,
        })
        .then((message) => {
          resolve(message);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

Hope you have installed twilio package also. Let me know if this works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM