3
votes
const accountSid = 'account_sid';
const authToken = 'auth_token';

const client = require('twilio')(accountSid, authToken);

client.messages
.create({
    to: '+94716220786',
    from: '+13022519234',
    body: 'Your verification code is ' + Math.floor(1000 + Math.random() * 
9000)
})
.then(message => console.log(message.sid));
console.log('receive');
console.log(Math.floor(1000 + Math.random() * 9000));

I'm trying to send sms using twilio. I have installed twilio latest version in my angular project.

But when the project runs, it says that "Cannot read property 'isTTY' of undefined"

1
what is the version of nodejs you are using? - PaulShovan
Can you show your full code? At which point do you call isTTY method? Also, are you aware that the verification code in the message-body is not the same than in the console.log() - ochs.tobi
@gypsyCoder I'm using nodejs version 8.9.4 - Yasiru Randeepa
@ochs.tobi this is my full code. This code is inside a function. I called that function when a button is clicked. consol.log is ok. I'm aware of that. My issue when requiring twilio and calling the method from the client variable. - Yasiru Randeepa
@YasiruRandeepa are you using it from the client side? You can have a look at this issue : github.com/twilio/twilio-node/issues/332 - PaulShovan

1 Answers

1
votes

Twilio developer evangelist here.

The Twilio Node library is not designed to be used on the client side. Mainly because that would expose your credentials and an attacker could then use your account. JavaScript is also editable on the client side, so sending a verification code could easily be edited and abused by an unscrupulous user.

Instead, you need to build a server side component that will make the requests for your client side. If you are building phone verification, then can I recommend you follow this tutorial on account verification with Twilio.

If you want to start a bit more basic, then I recommend the quick start guide to sending and receiving SMS with Twilio in Node.