The user will click on a button that will invoke the Parse Cloud function sendText()
I've tried both Live Twilio and Testing Twilio accSID and authToken
I first initialize my Twilio by:
var Twilio = require('twilio');
Twilio.initialize('accountSid', 'authToken'); //put in my corresponding <<
then I set the Parse function by:
Parse.Cloud.define('sendText', function(request, response) {
Twilio.sendSMS({
From: '+1234567890', //From Number
To: "+0987654321", //To Number
Body: "Start using Parse and Twilio!" //Message <<
}, {
success: function(httpResponse) { response.success("SMS sent!"); },
error: function(httpResponse) { response.error("Uh oh, something went wrong"); }
});
}
It would be great to have someone tell me if something here is wrong or if there are other approaches in sending SMS through Twilio via Parse Cloud.
On the SMS Summary on Twilio, it does not even know any SMS being sent out.
Going on...
The button that calls this cloud function is:
<button type="button" class="page-scroll btn btn-xl" onclick="saveData()">CONFIRM</button>
and the js function that is called saveData() is:
function saveData() {
booking.save({
something: something,
}, {
success: function (booking) {
window.location.href = 'final.php';
Parse.Cloud.run('sendText',
{
something: something
});
},
error: function (booking, error) {
alert('Failed to save');
}
});
}
NO ERROR LOG
Twilio developer evangelist here.
You seem to be using an old Parse module which is no longer supported by us. The new module however uses a newer version of our Node module.
Some documentation for it can be found here
It also has some sample code to do what you're trying to do.
// Require and initialize the Twilio module with your credentials
var client = require('twilio')('ACCOUNT_SID', 'AUTH_TOKEN');
// Send an SMS message
client.sendSms({
to:'+0987654321',
from: '+1234567890',
body: 'Hello world!'
}, function(err, responseData) {
if (err) {
console.log(err);
} else {
console.log(responseData.from);
console.log(responseData.body);
}
}
);
I think you will find your SMS will be sent using this version of the code. Notice how the initialization is different.
Related
I have set the project up as described in the blog post Twilio WhatsApp API and Flex in Minutes.
I followed the instructions and even after repeating the process twice, on two different accounts, when I send the message (through sandbox), the following error occurs:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'Enqueue'. One of '{Sms, Message, Redirect}' is expected.
The task does not show in Flex.
I have tried checking what WhatsApp sends vs what is sent when SMS arrives. This is what a simple function says about trigger.message for SMS:
{
EventType=onMessageSent,
InstanceSid=IS5aa457db5a2d44049c95f2d0b2699f56,
Attributes={\"proxied\":true},
DateCreated=2019-01-16T12:37:43.664Z,
Index=3,
From=sms_g13aggbhlwpgk7tbah3llj2kyjpsvvju,
MessageSid=IM9123e909eeda41ac92890201a6c3f1b4,
Source=API,
AccountSid=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX,
ChannelSid=CHbc79143368db4680a66c6c504103619f,
RetryCount=0,
ClientIdentity=sms_g13aggbhlwpgk7tbah3llj2kyjpsvvju,
WebhookType=studio,
To=CHbc79143368db4680a66c6c504103619f,
Body=Test Message,
ChannelAttributes={
status=ACTIVE,
forwarding=true,
serviceNumber=sms_g13aggbhlwpgk7tbah3llj2kyjpsvvju,
twilioNumber=+1228xxxxxxx,
from=+44798xxxxxxx,
channel_type=sms,
proxySession=KC1be96a19ed42aca39f9b6a9f06c26997
},
WebhookSid=WHcfc83a43a7c2424693fd0053b8b00a01
}
This is what is shows for WhatsApp message:
{
ApiVersion=2010-04-01,
SmsSid=SMb44cfdb4854c41e9ee255032a5449199,
SmsStatus=received,
SmsMessageSid=SMb44cfdb4854c41e9ee255032a5449199,
NumSegments=1,
From=whatsapp:+44798xxxxxxx,
To=whatsapp:+1228xxxxxxx,
MessageSid=SMb44cfdb4854c41e9ee255032a5449199,
Body=Test Message,
AccountSid=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX,
NumMedia=0
}
As you can see, the ChannelAttributes and ChannelSid are missing, and this is what Flex uses.
I then tried to transform the response with a function:
exports.handler = function(context, event, callback) {
let response;
if (!event.message.ChannelAttributes) {
response = {
name: event.message.From,
channelType: 'sms',
channelSid: 'CHbc79143368db4680a66c6c504103619f'
};
}
else {
response = {
name: "WhatsApp_" + event.message.ChannelAttributes.from,
channelType: event.message.ChannelAttributes.channel_type,
channelSid: event.message.ChannelSid
};
}
const twilioResponse = new Twilio.Response();
twilioResponse.setStatusCode(200);
twilioResponse.appendHeader('Content-Type', 'application/json');
twilioResponse.setBody(response);
callback(null, twilioResponse);
};
And assign the attributes in Studio like:
{
"name":"{{widgets.MyFunction.body.name}}",
"channelType":"{{widgets.MyFunction.body.channelType}}",
"channelSid":"{{widgets.MyFunction.body.channelSid}}"
}
Unfortunately, this shows the same error.
I am trying to develop an add-in for Microsoft Word Online. I am using Office.context.auth.getAccessTokenAsync call to get access token of a user, but it throws
error: Object {
name: "API Not Supported",
message: "This browser does not support the requested API.",
code: 5009
}
status: "failed"
Can anyone help?
Here is code that I am using to get Token.
"use strict";
(function() {
// The initialize function is run each time the page is loaded.
Office.initialize = function(reason) {
$(document).ready(function() {
// Use this to check whether the API is supported in the Word client.
if (Office.context.requirements.isSetSupported("WordApi", 1.1)) {
// Do something that is only available via the new APIs
$("#buttonPressed").click(getToken);
} else {
// Just letting you know that this code will not work with your version of Word.
$("#supportedVersion").html("This code requires Word 2016 or greater.");
}
});
};
function getToken() {
if (Office.context.requirements.isSetSupported("IdentityAPI", 1.1)) {
console.log("Yes Identity API is supported");
// code to request an SSO token.
Office.context.auth.getAccessTokenAsync(function(result) {
if (result.status === "succeeded") {
var token = result.value.accessToken;
console.log(token);
} else {
console.log("Error obtaining token", result.error);
}
});
} else {
console.log(" ID API not supported.");
}
}
})();
My manifest code placed above </VersionOverrides>
<WebApplicationInfo>
<Id> <My App ID is here> </Id>
<Resource>api://localhost:3000/<My App ID is here></Resource>
<Scopes>
<Scope>Files.Read.All</Scope>
<Scope>offline_access</Scope>
<Scope>openid</Scope>
<Scope>profile</Scope>
</Scopes>
</WebApplicationInfo>
Also tried
<Resource>https://localhost:3000</Resource>
in above code.
I work on a domain management software through the OVH's API.
I use nodejs and node-webkit and I downloaded the official Node.js wrapper for OVH.
Then, I followed the documentation here: https://www.npmjs.com/package/ovh and here: https://eu.api.ovh.com/g934.first_step_with_api, and I came with the following code:
// set the ovh object with the right configuration
var ovh = require('ovh')({
endpoint: 'ovh-eu',
appKey: 'APP_KEY', // replace it with my key
appSecret: 'APP_SECRET' // replace it with my key
});
ovh.request('POST', '/auth/credential', {
// set access rules
'accessRules': [
{'method': 'GET', 'path': '/*'},
{'method': 'POST', 'path': '/*'},
{'method': 'PUT', 'path': '/*'},
{'method': 'DELETE', 'path': '/*'},
]
}, function (error, credential) {
// print the error if the request failed, else, print the response
console.log(error || credential);
// set the consumerKey in the ovh object
ovh.consumerKey = credential.consumerKey;
// connect on the credential.validationUrl to validate the consumerKey
console.log(credential.validationUrl);
testMe();
});
function testMe() {
/*
This fonction test a request every second
to check if the user connected himself
*/
ovh.requestPromised('GET', '/me')
.then (function (me) {
// if the user is connected, tell him welcome
console.log('Welcome ' + me.firstname);
}
)
.catch (function (err) {
console.log(err);
// while the user is not connected, retry the request
setTimeout(testMe, 1000);
}
);
}
Well, when I execute this, everything is fine until I try to connect through the url, the testMe function keeps telling me an error and I don't get the welcome message.
In order to fix my problem, I tried to use different way to write my code and even checked in OVH's module sources if the signature was right before and after hashing, but it all seems to be good...
If someone already had this issue or if anybody see a error in my code, I would really appreciate your help. Thanks
you've got a syntax error :
then (function (response) {
// if the user is connected, tell him welcome
console.log('Welcome ' + me.firstname);
})
try this instead (renamed the parameter):
then (function (me) {
// if the user is connected, tell him welcome
console.log('Welcome ' + me.firstname);
})
Anyway, it it doesn't work properly please tell us the error you are getting.
I am using smsCordova plugin to send sms from my ionic application but getting the error that "SMS is not defined".
I had used the cordovaSms plugin in $ionicPlatform.ready() function.
Here is my code which i am using to send the SMS :-
//use verifyNumber service
verifyNumberService.verify()
.then(
function (result) {
if (result == "Successfully data save...") {
//alert and navigate to profile Info state
$ionicPopup.alert({
title: 'Registered',
template: 'Thanks For Signup'
});
$ionicPlatform.ready(function() {
alert("in device ready function")
sendOtp();
});
$state.go('profileInfo');
}
This is the function to sendOtp() :-
function sendOtp() {
alert("inside send otp");
$cordovaSms
.send('+919765293765', "Hi there",{})
.then(function () {
// Success! SMS was sent
alert("success")
console.log('Success');
}, function (error) {
// An error occurred
alert("error");
console.log(error);
});//then
alert("send otp");
}
Azhar Khan,
If we wants to use the send sms request in cordova, then
1. we need to install this plugin in you app :
cordova plugin add https://github.com/cordova-sms/cordova-sms-plugin.git
2.Need to add that plugin instance($cordovaSms) in Controler function :
.controller('ThisCtrl', function($cordovaSms) {
});
Now we can send the sms throw that plugin using this code inside you controler :
document.addEventListener("deviceready", function () {
$cordovaSms.send('mobile_number', 'SMS Message', options)
.then(function() {
alert(SMS sent )
}, function(error) {
alert(Problem in sending SMS)
});
});
Thats all we need for sending SMS to any number in ionic
Have a happy code day.
we already implemented push notification concept using android but we are trying to send push notification using web application to mobile
here is my code
function authentication() {
debugger;
Parse.$ = jQuery;
// Initialize Parse with your Parse application javascript keys
Parse.initialize("App key",
"javascript key");
debugger;
var pushQuery = new Parse.Query(Parse.Installation);
debugger;
pushQuery.containedIn("channels", true);
Parse.Push.send({
where: pushQuery,
data: {
alert: "Your push message here!"
}
}, {
success: function() {
debugger;
response.success("pushed");
}, error: function(error) {
reponse.error("didn't push");
debugger;
}
})
We are got error
POST https://api.parse.com/1/push 400 (Bad Request)
Uncaught ReferenceError: reponse is not defined
We followed this link & Docs of parse.com
Plz guide to us