Twilio call a user, play audio and receive callback - java

I need some help in achieving functionality using twilio API.
I want to call user and play some pre-recorded audio file and receive callback status/event.
Here is what I have achieved.
Twilio.init(TWILIO_SID, TWILIO_AUTH_TOKEN);
Call call = Call.creator(new PhoneNumber(callDetailModel.getToNumber()),
new PhoneNumber(FROM_TWILIO_NUMBER),
new URI(API_URL + "/call/twilio/voice/demo-voice")).create();
Now in "/call/twilio/voice/demo-voice" I am generating TwiML.
Play play = new Play.Builder(
"demo-play.wav")
.loop(1).build();
Number number = new Number.Builder(request.getParameter("Caller"))
.statusCallback(API_URL + "/call/twilio/events-demo")
.statusCallbackMethod(Method.POST).statusCallbackEvents(Arrays
.asList(Event.INITIATED, Event.RINGING, Event.ANSWERED, Event.COMPLETED)).build();
Dial dial = new Dial.Builder()
.timeLimit(60)
.callerId(request.getParameter("Called"))
.record(Dial.Record.DO_NOT_RECORD)
.number(number).build();
VoiceResponse twiml = new VoiceResponse.Builder().dial(dial).play(play).build();
return (twiml.toXml());
And now in event callback we are checking if receiver has received call or not, is it completed or not etc.
Now, the problem is when user receive a call auto voice mail of Twilio is getting played and after that my demo-play.wav get played. We want to remove that twilio voice mail and want to play only demo-play.wav which we have passed through TwiML.
UPDATE:
The audio getting played is here.
https://vocaroo.com/i/s0KNEhHxUKX5
Also the twiml which is generated is something like this
<Response>
<Dial timeLimit="7200" callerId="+1xxxxx" record="record-from-answer-dual">
<Number statusCallbackEvent="initiated ringing answered completed" statusCallback="http://38c5cb01.ngrok.io/call/twilio/events-demo" statusCallbackMethod="POST">+1zzzzzzzz</Number>
</Dial>
<Play loop="1">https://aws.xxx.H1OAOec30bbc5-bc3a-4251-a27c-2e4e41b386061521208641292FOIZ1.wav</Play>
</Response>
Am I missing something? Do I need to pass the Play in Dial verb? If then how?

Related

Android Sip: How to make audio call without using SIP address (I want to use real phone number instead)

I am using Android sip from android.net.sip package. I have a SIP credential which make call to phone number. below is sample piece of SIP call in android
SipProfile.Builder builder = null;
builder = new SipProfile.Builder("+16674442153" +
"", "");
builder.setPassword("");
SipProfile outgoing = builder.build();
sipManager. makeAudioCall(sipProfile.getUriString(), outgoing.getUriString(), listener, 30);
In this makeAudiocall function of SipManager is accepting only a sipaddress and whenever i enter a phone number its throw a exception
java.text.ParseException: Invalid URL: sip:+16674442153#
so is there is another way or library which helps me to make a phone number call using SIP in android.
This depends on your SIP Provider. If your SIP Provider allows SIP calls to normal phone numbers they should have documented how the SIP URI should look like.
For some providers the following scheme works:
phonenumber#sipproviderserver.com
But this is not a programming problem but a provider problem. The provider must allow you to connect from SIP to the Phone network. How the SIP endpoint have to be configured and if this can happen automagically depends, again, on the provider.

Is there a way to delete push notification data message sent by FCM?

In my application, I am offering video and voice calls between users. I'm using Firebase Realtime database and FCM as well to do so.
Here is the data that my notification is delivering:
data: {
channel_id: channel_id,
user_id: user_id
}
In my FirebaseMessagingService class, I retrieve the data and open the Video/Call activity like so:
if(remoteMessage.getData().size > 0) {
Intent intent = new Intent(this, VideoCallActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("channel_id", remoteMessage.getData().get("channel_id"));
intent.putExtra("user_id", remoteMessage.getData().get("user_id"));
getApplicationContext().startActivity(intent);
}
Everything is working perfectly even when the application is in background. However, I'm facing this issue:
If an user didn't have internet connection when he was called, when he connects to internet his phone will automatically start the VideoCallActivity even if the call has been aborted (by the caller) and the notification removed from Firebase real time database.
So, please is there a way to cancel sent notifications, or know the delivery status of those notifications, or another way around to do so? That will allow me to add the list of missed calls for users.
Thanks!
In a scenario like this, I know of two options:
Send another FCM message when the call is cancelled
Only use FCM to send a tickle
Send another FCM message when the call is cancelled
One of the properties you can give an FCM message is an collapse_key. When you send a message with a collapse_key, that message replaces any previous message with the same collapse_key value.
You can use this to send a "nope, forget about it" message, when the user cancels the call.
Only use FCM to send a tickle
Alternatively you can have the FCM message just be a so-called tickle: an almost empty message, that just tells the app to wake up and go check its queue of incoming messages in the database.
That way: if the call is still in the database, the app can pick it up. But if there is no call in the database, it can just do nothing.
You'll want to do this type of database interaction in a service, so that it can run when the app is backgrounded.
Giving an expiration time is feasible as suggested in the documentation
Check Here
Setting the lifespan of a message
On Android and Web/JavaScript, you can specify the maximum lifespan of a message. The value must be a duration from 0 to 2,419,200 seconds (28 days), and it corresponds to the maximum period of time for which FCM stores and attempts to deliver the message. Requests that don't contain this field default to the maximum period of four weeks.
Here are some possible uses for this feature:
Video chat incoming calls
Expiring invitation events
Calendar events

Twilio TaskRouter Worker Conference

I have a Twilio phone number which I am using for softphone and that phone I have configured for TaskRouter. So whenever Somebody call to twilio number that call route to available Worker, this is working fine but my question is how Worker can call other non twilio number in live call.
I am reading a docs on https://www.twilio.com/docs/api/taskrouter/handling-assignment-callbacks#redirecting-call where it mentions that
1. Dial the worker in a Conference name by the ReservationSid. This can be done on assignment call back.
2. Utilize Redirect Assignment Instruction to transfer the customer from a Queue to a Conference named by the ReservationSid.
So how can I dial a Worker in conference and how customer redirect to that conference so Agent can dial a non twilio number in live call and redirect that call in conference
means it is like three way communication,
Customer-->Twilio Number--->Worker--->Non Twilio number??
in PHP I have manage like this
first set one URL assignment in taskrouter
HERE : TaskRouter->Settings->Event Callbacks
like www.site.com/test/event_status_callback.php
You can use twilio conferece task router :-
https://www.twilio.com/docs/api/taskrouter/reservations
Conference Instruction like this
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/user/account
$accountSid = "AC1afdf65d5c4e434dc58792456bda940f";
$authToken = "your_auth_token";
$workspaceSid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$taskSid = "WTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$reservationSid = "WRXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$client = new Client($accountSid, $authToken);
// Update a Reservation with a Conference instruction
$reservation = $client->taskrouter
->workspaces($workspaceSid)
->tasks($taskSid)
->reservations($reservationSid)
->fetch();
$reservation->update(
array(
'instruction' => 'conference',
'dequeueFrom' => '+18001231234'
)
);
1) call come on twilio number and goes to taskrouter
<Response>
<Enqueue workflowSid="WWXXXXXXXXXXXXXXXXXXXXXXXX">
<Task>{"selected_language": "<?= $language ?>"}</Task>
</Enqueue>
</Response>
2)now call goes to perticuler language wise and dail to avelable workers and here you can redirect your call to other Non Twilio number get call sid
also you can Use Db for manage worker side and call sid
In this event_status_callback.php page you get call_sid
$call = $client
->calls($call_sidss)
->update(
array(
"url" => $url . "test/callredirectonsupport.php",
"method" => "POST"
)
);
echo $call->to;
3) callredirectonsupport.php
now you call redirect to callredirectonsupport.php
here you can write youe dial code

check automatically for new message android

I am trying to create a simple android messaging app. So basically let us assume that user A sends a message to user B. I would like that user B receives a notification when the message is received. I know how to create the notification and all. But basically I would like to know how the user B constantly checks if a new message has been received even when he is out of the app, and then that would trigger the notification and subsequent actions.
Thank you
You have to setup an unbound background service.
With this you can constantly make pull-requests to your server or get push-notifications from your server and display notifications.
https://developer.android.com/training/run-background-service/create-service.html
You can use Event Bus lib for this purpose. When new message will be received it will create and event and then you can receive that event event and do other operations.
If you are talking about text messages then you will have to create a BroadcastReceiver
http://developer.android.com/reference/android/content/BroadcastReceiver.html
You can see my answer here for SMS Receiver
Verify sms text message by app before it displayed to user
Don't forget to give permissions in your app's manifest.
Hope it helps.

Android SMS API

I know that the SMS content provider is not part of the public API (at least not documented), but if I understand correctly it's still possible to use many of the SMS features as long as you know how to use the API(?).
E.g it's pretty straightforward to insert an SMS into your inbox:
ContentValues values = new ContentValues();
values.put("address", "+457014921911");
contentResolver.insert(Uri.parse("content://sms"), values);
Unfortunately this does not trigger the standard "new-SMS-in-your-inbox" notification. Is it possible to trigger this manually?
Edit: AFAIK the "standard mail application (Messaging)" in Android is listening for incoming SMSes using the android.permission.RECEIVE_SMS permission. And then, when a new SMS has arrived, a status bar notification is inserted with a "special" notification id.
So one solution to my problem (stated above) could be to find, and send the correct broadcast intent; something like "NEW SMS HAS ARRIVED"-intent.
Edit: Downloaded a third party messaging application (chompsms) from Android market. This application satisfies my needs better. When i execute the code above the chompsms notice the new sms and shows the "standard status bar notification". So I would say that the standard Android Messaging application is not detecting sms properly? Or am I wrong?
Unfortunately the code responsible for these notifications is hidden in the messaging application. The class MessagingNotification has a static method updateAllNotifications that you could call using a PathClassLoader and reflection:
PathClassLoader c = new PathClassLoader("/system/app/Mms.apk", getClassLoader());
Class.forName("com.android.mms.util.ContactInfoCache", true, c)
.getMethod("init", Context.class).invoke(null, context);
Class.forName("com.android.mms.transaction.MessagingNotification", true, c)
.getMethod("updateAllNotifications", Context.class).invoke(null, context);
This is obviously a very bad idea for several reasons but I can't think of another way to do what you described.
Could you trigger a PUSH notification after the SMS?
Thread: Does Android support near real time push notification?
Maybe you should replace
content://sms
with
content://sms/inbox

Categories

Resources