I've implemented a call center in Salesforce with Twilio client JavaScript SDK. I'm trying to save the call record information in Salesforce and I'm using the connection.parameters.CallSid to identify the correct record when the recording call back fires. However my CallSid in client side is getting changed automatically sometimes to a different format and hence the recording call back function can't find the Salesforce end record to update it with the RecordingUrl. Have anyone experienced this before or appreciate any guidance.
Below is my JavaScript code. To be more specific, in the startCall function console.log print the CallSid correctly but when goes to saveLog function it's having a different value with a different format and hence the saved record having an incorrect value.
<script type="text/javascript">
Twilio.Device.setup("{! token }");
var callerSid; // hold the Twilio generated CallSid unique to this call
var parentId; // hold the parent record being called in order to associate as the parent of task being logged for the call
var newTaskId; // hold the id of newly created task
// function to fire when click 2 dial executes
var startCall = function (payload) {
sforce.opencti.setSoftphonePanelVisibility({visible: true}); //pop up CTI softphone
parentId = payload.recordId; // the record that the phone number being called belongs to
var cleanednumber = cleanFormatting(payload.number);
params = {"PhoneNumber": cleanednumber};
var connection = Twilio.Device.connect(params);
callerSid = connection.parameters; // track the unique Twilio CallSid
console.log('clk2dial : ', callerSid.CallSid); **// here it logs correcly as CAc57d05994cd69498e0353a5f4b07f2dc**
setTimeout(function(){
saveLog(); // save the call information in a Task record
}, 2000
);
};
//OpenCTI!!
sforce.opencti.enableClickToDial();
sforce.opencti.onClickToDial({listener : startCall}); // click 2 dial
function cleanFormatting(number) {
//changes a SFDC formatted US number, which would be 415-555-1212 into a twilio understanble number 4155551212
return number.replace(' ','').replace('-','').replace('(','').replace(')','').replace('+','');
}
// save the call information in a Task record
function saveLog() {
var keyPrefix;
var taskToSave;
console.log('callerSid.CallSid : ', callerSid.CallSid); **// surprisingly here it logs as TJSce253eb4-c2a0-47f3-957f-8178e95162aa**
if(parentId != null) {
keyPrefix = parentId.slice(0,3);
}
if(keyPrefix != null && keyPrefix == '003') {
taskToSave = {WhoId:parentId, Type: "Call", CallObject: callerSid.CallSid, entityApiName: "Task", Subject: "Call log"};
} else {
taskToSave = {WhatId:parentId, Type: "Call", CallObject: callerSid.CallSid, entityApiName: "Task", Subject: "Call log"};
}
sforce.opencti.saveLog({value:taskToSave, callback: saveLogCallBack});
}
// call back function for saving the call information in a Task record
var saveLogCallBack = function(response) {
if(response.success) {
newTaskId = response.returnValue.recordId;
console.log('save success! : ', newTaskId);
} else {
console.error('Error saving : ', response.errors);
}
}
</script>
Answering my own question as I got through this. I registered a function for Twilio.Device.connect and in the call back function retrieved the CallSid. Along with that I've updated my click 2 dial function as well accordigly as below. However I was unable to find this approach in Twilio documentation and any comments are welcome.
// function to fire when click 2 dial executes
var startCall = function (payload) {
sforce.opencti.setSoftphonePanelVisibility({visible: true}); //pop up CTI softphone
parentId = payload.recordId; // the record that the phone number being called belongs to
var cleanednumber = cleanFormatting(payload.number);
params = {"PhoneNumber": cleanednumber};
Twilio.Device.connect(params);
};
//OpenCTI!!
sforce.opencti.enableClickToDial();
sforce.opencti.onClickToDial({listener : startCall}); // click 2 dial
// registered a function for Twilio Device connect
Twilio.Device.connect(function(response) {
callSid = response.parameters; // track the unique Twilio CallSid
// nothing change in save function so not posting again
saveLog(); // save the call information in a Task record
});
Related
This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 6 years ago.
I'm working on adding notifications to my chrome extension that lets users chat with one another. Each user has a chats section that lists all the chatrooms they're in, along with the last message they saw. To check if a message was sent while they were away, I'm trying to loop through the chatrooms they're in and seeing if the last message sent was added to the database after the last message they saw was added.
Here's my code, I'm using a noSQL database called firebase:
var checkNotifications = function(user){
var notification = false;
firebase.database().ref('users/'+user+'/chats').once('value', function(snapshot){
for (var chat in snapshot.val()){
var lastMessage = snapshot.val()[chat];
firebase.database().ref('chats/'+chat+'/msgs').once('value', function(snap){
if (Object.keys(snap.val())[0] > lastMessage){
notification = true; // Message is newer than their last seen message
}
});
}
});
}
I'm running into the issue where before the second database call to return the messages from the chatroom currently being looked at in the for loop the for loop already moves on and updates the variable lastMessage, so that variable ends up holding the last message from a chatroom further down the chain.
Is there a way to make this less asynchronous so that the variable lastMessage has the same value for each step in the for loop?
To check if a message was sent while they were away, I'm trying to
loop through the chatrooms they're in and seeing if the last message
sent was added to the database after the last message they saw was
added.
transactions take a function and are the proper tool for this.
When working with data that could be corrupted by concurrent
modifications, such as incremental counters, you can use a transaction
operation. You can give this operation an update function and an
optional completion callback. The update function takes the current
state of the data as an argument and returns the new desired state you
would like to write. If another client writes to the location before
your new value is successfully written, your update function is called
again with the new current value, and the write is retried.
For instance, in the example social blogging app, you could allow users to star and unstar posts and keep track of how many stars a post has received as follows:
function toggleStar(postRef, uid) {
postRef.transaction(function(post) {
if (post) {
if (post.stars && post.stars[uid]) {
post.starCount--;
post.stars[uid] = null;
} else {
post.starCount++;
if (!post.stars) {
post.stars = {};
}
post.stars[uid] = true;
}
}
return post;
});
}
This should work. It captures the variable in a closure: (function(){ ... })();
var checkNotifications = function(user){
var notification = false;
firebase.database().ref('users/'+user+'/chats').once('value', function(snapshot){
for (var chat in snapshot.val()){
(function(){
var lastMessage = snapshot.val()[chat];
firebase.database().ref('chats/'+chat+'/msgs').once('value', function(snap){
if (Object.keys(snap.val())[0] > lastMessage){
notification = true; // Message is newer than their last seen message
}
});
})();
}
});
}
Or this:
var checkNotifications = function(user){
var notification = false;
firebase.database().ref('users/'+user+'/chats').once('value', function(snapshot){
var func = function(lastMessage) {
firebase.database().ref('chats/'+chat+'/msgs').once('value', function(snap){
if (Object.keys(snap.val())[0] > lastMessage){
notification = true; // Message is newer than their last seen message
}
});
};
for (var chat in snapshot.val()){
func(snapshot.val()[chat]);
}
});
}
This question already has answers here:
Create an empty child record in Firebase
(2 answers)
Closed 6 years ago.
I'm working on a chrome extension and pushing data from a Google-authenticated user to Firebase.
I'm listening for a message coming from another JS file and when it comes in, I want to take the "user profile" object (request) and tack on a property to it, called "visitedLinks". visitedLinks should be set to an empty object.
I do 3 console.logs throughout the code, and in all three cases, the console.logs show "visitedLinks" set to an empty object, yet when I push to Firebase, "visitedLinks" isn't a property.
//Relevant 3 console statements are the following
//console.log('request.accountData = ', request.accountData)
//console.log('userObject test #1 = ', userObject)
//console.log('userObject = ', userObject)
var rootRef = new Firebase("https://search-feed-35574.firebaseio.com/");
if (localStorage.userIsAuthenticated) {
console.log('user is authenticaled')
chrome.runtime.onMessage.addListener(
//listen for messages
function(request, sender, sendResponse) {
//url is coming in from a content script, use localStorage.uid to make database call
if (request.url) {
console.log('message coming from content script')
var uid = localStorage.uid;
var url = request.url;
var userRef = rootRef.child(uid);
newLinkRef = userRef.push(url);
//otherwise, we're getting a message from popup.js, meaning they clicked it again, or they've signed in for the first time
} else {
console.log('message coming from popup')
//here we're passing in all the data from the person's Google user account
var googleUID = request.accountData.uid
//then we make a new object with the key as their google UID and the value all the account data
request.accountData.visitedLinks = {}
console.log('request.accountData = ', request.accountData)
var userObject = {};
userObject[googleUID] = request.accountData;
console.log('userObject test #1 = ', userObject)
//here were checking to see if the UID is already a key in the database
//basically, if they've ever logged in
rootRef.once('value', function(snapshot) {
if (snapshot.hasChild(googleUID)) {
//user has authenticated before, they just happened to click the popup again
console.log('already authenticated, you just clicked the popup again')
} else {
console.log('users first DB entry');
//if they're not in the database yet, we need to push userObject to the DB
//and push their current url to the publicLinks array
rootRef.set(userObject, function(error) {
console.log('error = ', error);
console.log('userObject after DB insert = ', userObject)
});
}
})
}
//figure out if this user has entries in the DB already
//just push the link information onto the "links" node of the db object
//if not, push a ref (to the right place)
// console.log(sender)
});
} else {
console.log('user isnt authenticated')
}
So it turns out that you can't insert empty objects into the database. Similar question answered here: Create an empty child record in Firebase
Im using an external API. I'd like to show logged in user's nickname (steam API)
server main.js
Meteor.startup(function () {
ServiceConfiguration.configurations.upsert(
{ service: 'steam' },
{
$set: {
loginStyle: 'redirect',
timeout: 10000 // 10 seconds
}
}
);
Accounts.onLogin(function() {
var steam64Id = Meteor.user().profile.id;
console.log(steam64Id + " is logged in.");
// To retrieve more details about user
var steamApiKey = ("XXXXXXXXXXXXX");
var result = Meteor.http.get('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxxxx&steamids=76561197977045111');
console.log(result.data.response.players[0].personaname);
});
});
This returns "My Nickname" in the console log, but I'd like to retrieve this variable in client side so I can add {{Nickname}} to client js template and show user's Nickname in the template
The most obvious way would be to add that result to the user's profile in their user record. Then it would automatically be synchronized to the client and available for use there.
var steamApiKey = ("XXXXXXXXXXXXX");
var result = Meteor.http.get('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxxxx&steamids=76561197977045111');
Meteor.users.update(Meteor.userId(),
{ $set: { 'profile.personaname': result.data.response.players[0].personaname }}
);
On the client side, make yourself a helper (either a global one or one specific to the template(s) where you need it) to get this value:
Nickname(){
return Meteor.user().profile.personaname;
}
Note that the profile object under the user is automatically published to the client so you don't have to make a special publication for it.
I'm still struggling to understand how to access Meteor.users as a foreign key from another collection query. I understand that only the current user is published by default so I have a publication on the server as
Meteor.publish('itemOwner', function(userId) {
check(userId, String);
var user = Meteor.users.find({id: userId});
return user;
// return Meteor.users.find({id: userId}, {
// fields: {'profile': 1} });
});
I then have a Deps.autorun on the client..
Deps.autorun(function () {
var itemOwnerId = Session.get("itemOwnerID");
if (itemOwnerId) {
debugger
var ID = Session.get("itemOwnerID");
Meteor.subscribe('itemOwner', Session.get("itemOwnerID"));
}
});
I set the session ID on a modal form load, and display it in the template by calling the ownerProfile helper (or try to)
Template.showQuoteModalInner.helpers({
getQuote: function () {
// Get the quote ID from the session var
var quote = Session.get("quoteID");
if(quote) {
debugger;
var ID = quote.user._id;
Session.set("itemOwnerID", quote.user._id);
return quote;
}
},
ownerProfile: function() {
debugger;
var quote = Session.get("quoteID");
if(quote) {
var ID = quote.user._id;
var theUser = Meteor.users.find({_id: quote.user._id});
return theUser;
};
}
});
Now, I can trace the user ID at each stage and see it getting correctly passed to the autorun and the helpers. If I stop the program at the debugger in the ownerProfile helper and in the console put in Meteor.user.fetch({_id: "the id here"}).fetch() I get the correct user back.. but, in the handler itself the Meteor.users.find returns null??? What am I missing?
Two possibilities I noticed.
First, you are missing an underscore in the find in your publish function.
.find({id: userId}) should be .find({_id: userId}).
But this probably isn't the issue if you are seeing the user (other than the logged in user) in the console.
Second, if you are not seeing the user from your Template.showQuoteModalInner.ownerProfile helper, it is probably because you are returning a find() instead of a findOne().
find() returns a cursor whereas findOne() returns the record. Try findOne() if you want to display that single user's attributes.
Here is my code:
Titanium.Network.registerForPushNotifications({
types: [
Titanium.Network.NOTIFICATION_TYPE_BADGE,
Titanium.Network.NOTIFICATION_TYPE_ALERT
],
success:function(e)
{
var deviceToken = e.deviceToken;
Ti.API.info("Push notification device token is: "+deviceToken);
alert('device token is' +e.deviceToken);
Ti.API.info("Push notification types: "+Titanium.Network.remoteNotificationTypes);
Ti.API.info("Push notification enabled: "+Titanium.Network.remoteNotificationsEnabled);
},
error:function(e)
{
Ti.API.info("Error during registration: "+e.error);
},
callback:function(e)
{
// called when a push notification is received.
//Titanium.Media.vibrate();
var data = JSON.parse(e.data);
var badge = data.badge;
if(badge > 0){
Titanium.UI.iPhone.appBadge = badge;
}
var message = data.message;
if(message != ''){
var my_alert = Ti.UI.createAlertDialog({title:'', message:message});
my_alert.show();
}
}
});
}
The callback function:
callback:function(e)
{
// called when a push notification is received.
//Titanium.Media.vibrate();
var data = JSON.parse(e.data);
var badge = data.badge;
if(badge > 0){
Titanium.UI.iPhone.appBadge = badge;
}
var message = data.message;
if(message != ''){
var my_alert = Ti.UI.createAlertDialog({title:'', message:message});
my_alert.show();
}
}
});
is fired when the push notification is recieved when the app is running in the foreground.
Question , if I have 2 files:
app.js -> newwindow.js
and say that I am in newwindow.js , will I still receive push notifications? (if the code above is all pasted in app.js?)
2) When I recieve a push notification when the app is running in the background, how can I write a callback method for it, so I can tell the app what to do with that notification
3) What is the best way of handling different notifications, i.e. say I need to open different windows, when its in background mode?
notification 1 - > win1.js
notification 2 - > win2.js
notification 3 - > win3.js
First of all you should Implement it in your app(It seems you just copied from docs) and see how actually push notification appear it will make every thing a lot clear.
The callback you have written above will fetch the message that you have send using the Push notification.
As for part 2 of your question : Once the user clicks on the push received your app will come into foreground and the callback will be automatically called.
And to handle different notifications, there are never different push notifications, there is always a single push notification.you have to parse the data and have act according to the condition. you can modify your push message and open window according to it.