如何在博览会项目中使用Twilio发送消息React Native

问题描述

我试图在我的本机应用程序中按下按钮时向用户发送SMS通知。但是,这会产生错误。这是我的代码

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',})
}
@H_502_5@

请注意,这是一个博览会项目。

电脑错误

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.
@H_502_5@

手机错误

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

知道为什么会这样吗?

解决方法

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);
        });
    });
  }

希望您也已经安装了twilio软件包。让我知道这是否有效。