繁体   English   中英

如何在 expo 项目中使用 Twilio 发送消息(React Native)

[英]how to send a message with Twilio in an expo project (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',
    })
}

请注意,这是一个 expo 项目。

电脑报错:

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.

手机报错:

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

知道为什么会这样吗?

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 package。 让我知道这个是否奏效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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