Keep getting thrown Error: Username/Password required - twilio

Good afternoon,
Trying to create an SMS booking bot and am running into an issue trying to call the main.js file I have in .src and I'm a bit lost on why.
I have my .env file formatted as needed
node src/main.js
>main.js<
require("dotenv").config();
const from = process.env.PHONE_NUMBER
const to = process.env.MY_NUMBER
const twilio = require('twilio')();
process.env.TOKEN_SID,
process.env.TOKEN_SECRET,
{
accountSid: process.env.TOKEN_SID
}
;
twilio.messages.create({
from,
to,
body: "Hello from Twilio!"
})
.then(message => console.log('Message sent with SID ${message.sid}'))
.catch(error => console.error(error));
>Error output<:
Error: username is required
at new Twilio (/Users/*/Twilio/sms-booking/backend/node_modules/twilio/lib/rest/Twilio.js:139:11)
at initializer (/Users/*/Twilio/sms-booking/backend/node_modules/twilio/lib/index.js:10:10)
at Object.<anonymous> (/Users/*/Twilio/sms-booking/backend/src/main.js:6:33)
Error: Password is required
at new Twilio (/Users/*/Twilio/sms-booking/backend/node_modules/twilio/lib/rest/Twilio.js:139:11)
at initializer (/Users/*/Twilio/sms-booking/backend/node_modules/twilio/lib/index.js:10:10)
at Object.<anonymous> (/Users/*/Twilio/sms-booking/backend/src/main.js:6:33)

Related

Twilio Verify called from Twilio Functions returns resource not found

I am making a simple to the verify api from a Twilio Function triggered in a studio flow.
The error I am recieving is "verifications resource not found".
I assumed it was an authentication issue, but I have the correct SIDS and have checked that a number of times.
Not sure what I am missing.
const accountSid = process.env.ACCOUNT_SID;
const authToken = process.env.AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);
const functions = Runtime.getFunctions();
const formatPhone = require(functions['formatters/formatPhone'].path);
exports.handler = function (context, event, callback) {
const verifyPhone = formatPhone.toSearch(event.verifyPhone);
const verifyType = event.verifyType;
client.verify
.services('<Verify SID>')
.verifications.create({ to: '+1' + verifyPhone, channel: verifyType })
.then((verification) => {
console.log(verification.sid);
callback(null, 'done');
})
.catch((err) => callback(err, null));
};
I can run this code from my machine and hit it from studio using NGROK and an http widget instead of a function.
SAME CODE.
I feel that it may be the work of Legacy Systems!
So as strange as it sounds it ended up being a library versioning issue.
npm i 'twilio'
and then pushed again to the server
twilio serverless:deploy --override-existing-project
I also realize that I didn't list the error originally. I have edited it to make more sense as to the problem I was having.

How can I send Email from gmail to another email account

I think that this's an issue many coders are facing at the moment.
So let's find a solution:
I already tried ->
mailer2: ^1.2.5
var options = GmailSmtpOptions()
..username = ''
..password = '';
var emailTransport = SmtpTransport(options);
var envelope = Envelope()
..from = ''
..recipients.add(email)
..subject = ''
..html = "<h1>Title </h1>\n\n"
emailTransport.send(envelope)
.then((envelope) => print('Email sent!'))
.catchError((e) => print('Error occurred: $e'));
but the following error message came up:
Unsupported operation: Platform._localHostname
at Object.throw_ [as throw] (http://localhost:63913/dart_sdk.js:4463:11)
at Function._localHostname (http://localhost:63913/dart_sdk.js:54754:17)
at Function.get localHostname [as localHostname] (http://localhost:63913/dart_sdk.js:54810:33)
at get _localHostname (http://localhost:63913/dart_sdk.js:54713:27)
at Function.desc.get [as _localHostname] (http://localhost:63913/dart_sdk.js:4944:15)
at Function.get localHostname [as localHostname] (http://localhost:63913/dart_sdk.js:54666:26)
at mailer.GmailSmtpOptions.new.mailer.SmtpOptions.new
(http://localhost:63913/packages/mailer2/mailer.dart.lib.js:1178:30)
at new mailer.GmailSmtpOptions.new (http://localhost:63913/packages/mailer2/mailer.dart.lib.js:1229:43)
at http://localhost:63913/packages/bestbite_native/web/screens/screen_after_order.dart.lib.js:12394:37
at Generator.next (<anonymous>)
at runBody (http://localhost:63913/dart_sdk.js:37195:34)
at Object._async [as async] (http://localhost:63913/dart_sdk.js:37226:7)
at http://localhost:63913/packages/bestbite_native/web/screens/screen_after_order.dart.lib.js:12392:389
at _RootZone.runUnary (http://localhost:63913/dart_sdk.js:37029:58)
at _FutureListener.then.handleValue (http://localhost:63913/dart_sdk.js:32116:29)
at handleValueCallback (http://localhost:63913/dart_sdk.js:32663:49)
at Function._propagateToListeners (http://localhost:63913/dart_sdk.js:32701:17)
at _Future.new.[_completeWithValue] (http://localhost:63913/dart_sdk.js:32544:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:63913/dart_sdk.js:32566:35)
at Object._microtaskLoop (http://localhost:63913/dart_sdk.js:37290:13)
at _startMicrotaskLoop (http://localhost:63913/dart_sdk.js:37296:13)
at http://localhost:63913/dart_sdk.js:32918:9Error: Unsupported operation: Platform._localHostname
Does anybody has an idea how to send emails in flutter web applications?
(Without opening an email tab)
"mailer2"(and the original package "mailer") imports dart:io which is not available on flutter web and therefore you cannot use this package.
A solution I have used in the past for sending emails is to create an http-triggered cloud function(e.g. Firebase functions, AWS Lambda, netlify functions) that sends the email. You can pass any data you need through the http request and have the cloud function send the email through something like nodemailer in Node.js.
There also may be other services or APIs available that already do what the cloud function does, though I have not seen many and they are not my preferred method

twilio node voice outgoing quickstart fails silently

I know this is a complete n00b question but I'm stumped.
I've created an account and cut/pasted the node quickstart code into a make_call.js file. I've put in my account info and the correct from (my twilio number) and to (my home) phone numbers.
when I run via 'node make_call.js' there is a slight pause and then a shell prompt, no output, no phone call.
if I modify the account fields so they are wrong, either one, I get the same results so it seems like this code is not communicating with the twilio servers at all?
any pointers on how to figure out what's going on?
here's my code, literally copied/pasted from the example with 4 fields changed.
// Download the Node helper library from twilio.com/docs/node/install
// These consts are your accountSid and authToken from twilio.com/user/account
const accountSid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const authToken = '0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const Twilio = require('twilio');
const client = new Twilio(accountSid, authToken);
client.api.calls
.create({
url: 'http://demo.twilio.com/docs/voice.xml',
to: '+1212xxxxxxx',
from: '+1929xxxxxxx',
})
.then((call) => console.log(call.sid));
Looks like the quick start guide and the REST API docs for node are different. What happens if you try the code for an outbound call from the REST docs?
// Download the Node helper library from twilio.com/docs/node/install
// These identifiers are your accountSid and authToken from
// https://www.twilio.com/console
const accountSid = 'accountSid';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);
client.calls.create({
url: 'http://demo.twilio.com/docs/voice.xml',
to: '+14155551212',
from: '+15017250604',
})
.then((call) => process.stdout.write(call.sid));
I have no idea why this code wasn't working and silently failing yesterday but I just ran the (unchanged) code again today and it worked (and output the sid at the end.)
The problem with the example make_call.js is that it does not catch any errors. Here is a slightly modified version with error catching that greatly helps debugging.
const accountSid = 'Your account sid';
const authToken = 'Your auth token';
const Twilio = require('twilio');
const client = require('twilio')(accountSid, authToken);
client.api.calls
.create({
url: 'http://demo.twilio.com/docs/voice.xml',
to: 'your mobile',
from: 'your twilio number',
}, function(err, call){
if(err) {
console.log(err);
} else {
console.log(call.sid);
}
})

Twilio blacklist rule fatal error

I am using twilio to send bulk sms messages. Let's say some customer decided that they don't want to receive messages anymore so they reply with "stop" and that will add them to the black list. I am hard coding the phone numbers because I am still testing on my own cell phones. I noticed that when I do not remove the numbers on the black list from my code, I am getting an error message and my script stops at that point.
In the future, I will probably be using numbers stored in a database or a file. In that case, how do I overcome this problem if it happened. Basically what I want to do is: If a number is in the black list, move on to the next number and avoid that error using an exception or something.
The error message and code is below.
Thanks,
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Send SMS</title>
<?php
/* Send an SMS using Twilio. You can run this file 3 different ways:
*
* 1. Save it as sendnotifications.php and at the command line, run
* php sendnotifications.php
*
* 2. Upload it to a web host and load mywebhost.com/sendnotifications.php
* in a web browser.
*
* 3. Download a local server like WAMP, MAMP or XAMPP. Point the web root
* directory to the folder containing this file, and load
* localhost:8888/sendnotifications.php in a web browser.
*/
// Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php,
// following the instructions to install it with Composer.
//require_once "vendor/autoload.php";
require __DIR__ . '/twilio-php-master/Twilio/autoload.php';
use Twilio\Rest\Client;
// Step 2: set our AccountSid and AuthToken from https://twilio.com/console
$AccountSid = "something";
$AuthToken = "something";
// Step 3: instantiate a new Twilio Rest Client
$client = new Client($AccountSid, $AuthToken);
// Step 4: make an array of people we know, to send them a message.
// Feel free to change/add your own phone number and name here.
$people = array(
"+17570123456" => "Chris",
"+17571234568" => "Hussam"
);
// Step 5: Loop over all our friends. $number is a phone number above, and
// $name is the name next to it
foreach ($people as $number => $name) {
$sms = $client->account->messages->create(
// the number we are sending to - Any phone number
$number,
array(
// Step 6: Change the 'From' number below to be a valid Twilio number
// that you've purchased
'from' => "+184444444444",
// the sms body
'body' => "Hey $name, this is Hussam. Testing Twilio SMS API!"
)
);
// Display a confirmation message on the screen
echo "Sent message to $name.\n";
}
?>
( ! ) Fatal error: Uncaught exception 'Twilio\Exceptions\RestException' with message '[HTTP 400] Unable to create record: The message From/To pair violates a blacklist rule.' in C:\wamp64\www\Twilio\twilio-php-master\Twilio\Version.php on line 86 ( ! ) Twilio\Exceptions\RestException: [HTTP 400] Unable to create record: The message From/To pair violates a blacklist rule. in C:\wamp64\www\Twilio\twilio-php-master\Twilio\Version.php on line 86 Call Stack
Time Memory Function Location 1 0.0000 239280 {main}( ) ...\send.php:0 2 0.0156 799016 Twilio\Rest\Api\V2010\Account\MessageList->create( ) ...\send.php:56 3 0.0156 814688 Twilio\Version->create( ) ...\MessageList.php:63
Twilio developer evangelist here.
You need to catch the exception that is thrown from the request to send a message to the blacklisted number. You can do so with try and catch like this:
foreach ($people as $number => $name) {
try {
$sms = $client->account->messages->create(
$number,
array(
'from' => "+18443949780",
'body' => "Hey $name, this is Hussam. Testing Twilio SMS API!"
)
);
echo "Sent message to $name.\n";
} catch (\Twilio\Exceptions\RestException $e) {
echo "Couldn't send message to $number\n";
}
}
When you hook this up to a database, you'll want to use the catch to update a field to mark the number as blocked so that you don't try to send to it again.
Let me know if that helps at all.
This worked for me with Laravel 5. Notice the use of \Twilio\Exceptions\RestException.
try {
$sms = $client->account->messages->create(
$number,
array(
'from' => "+16136543180",
'body' => "Hey $name, Are you still mad at us about your cat!"
)
);
echo "Sent message to $name.\n";
} catch (\Twilio\Exceptions\RestException $e) {
if ($e->getCode() == 20404) {
//this will be false condition
dd('False Result 404');
} else {
//some other exception code
dd($e->getMessage());
}
}

twilio Uncaught exception error

Twilio newbie using test account. I followed the instructions listed here for installing Twilio php:
https://www.twilio.com/docs/quickstart/php/sms
Because I was getting a certificate error, my host provider suggested I changed the CURLOPT_SSL_VERIFYPEER => false (from true). But now I'm getting this error. How to fix?:
Fatal error: Uncaught exception 'Services_Twilio_RestException' with message 'The requested resource /2010-04-01/Accounts//Messages.json was not found' in
require "Services/Twilio.php";
// Step 2: set our AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "ACbxxxxxxx";
$AuthToken = "0cfxxxxxxx";
// Step 3: instantiate a new Twilio Rest Client
//$client = new Services_Twilio($AccountSid, $AuthToken);
$http = new Services_Twilio_TinyHttp(
'https://api.twilio.com',
array('curlopts' => array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
))
);
$client = new Services_Twilio($sid, $token, "2010-04-01", $http);
// Step 4: make an array of people we know, to send them a message.
// Feel free to change/add your own phone number and name here.
$people = array(
"+13121111111" => "Curious George",
// "+14158675311" => "Virgil",
);
// Step 5: Loop over all our friends. $number is a phone number above, and
// $name is the name next to it
foreach ($people as $number => $name) {
$sms = $client->account->messages->sendMessage(
// Step 6: Change the 'From' number below to be a valid Twilio number
// that you've purchased, or the (deprecated) Sandbox number
"929-xxx-xxxx",
// the number we are sending to - Any phone number
$number,
// the sms body
"Hey $name, Monkey Party at 6PM. Bring Bananas!"
);
// Display a confirmation message on the screen
echo "Sent message to $name";
}
Twilio developer evangelist here.
Firstly, you should never set CURLOPT_SSL_VERIFYPEER to false in production. From the curl manual:
WARNING: disabling verification of the certificate allows bad guys to man-in-the-middle the communication without you knowing it. Disabling verification makes the communication insecure. Just having encryption on a transfer is not enough as you cannot be sure that you are communicating with the correct end-point.
For the sake of getting you going with Twilio though your issue is in the variable names you are using.
At the top of the file you set:
$AccountSid = "ACbxxxxxxx";
$AuthToken = "0cfxxxxxxx";
But when you create a Twilio you use $sid and $token.
$client = new Services_Twilio($sid, $token, "2010-04-01", $http);
If you change those to $AccountSid and $AuthToken it should work as you expected.

Resources