I'm trying to make a call to a number using the Twilio service using a Trial Account.
I'm following the Java example here: https://www.twilio.com/docs/quickstart/java/rest/call-request
I've configured the example with my API credentials, the provided Twilio number, the destination number and the TwiML instructions url.
When I run the MakeCall class the destination number get called.
When I respond to the call I get the "trial account" message, then it asks me to press any key. When I press a key the call is dropped.
As I can see the TwiML instructions url is not called by Twilio.
I've tested also with the Test Credentials with no success.
Any idea on why the TwiML instructions url is not called?
Using the twilio-java helper library and following code from the docs you mentioned above:
We then instantiate a new client object, set the request method to
'POST', fill in the 'From', 'To' and 'Url' parameters in an
associative array, and fire off the request to Twilio!
Aside from any potential issues with your URL...did you also set the request method to POST while configuring your twilio number in the console?
import java.util.Map;
import java.util.HashMap;
import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.instance.Account;
import com.twilio.sdk.resource.instance.Call;
import com.twilio.sdk.resource.factory.CallFactory;
public class MakeCall {
public static final String ACCOUNT_SID = "AC123";
public static final String AUTH_TOKEN = "456bef";
public static void main(String[] args) throws TwilioRestException {
TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
Account mainAccount = client.getAccount();
CallFactory callFactory = mainAccount.getCallFactory();
Map<String, String> callParams = new HashMap<String, String>();
callParams.put("To", "5105551212"); // Replace with your phone number
callParams.put("From", "(510) 555-1212"); // Replace with a Twilio number
callParams.put("Url", "http://demo.twilio.com/welcome/voice/"); // Configure your own URL with TwiML instructions using TwiML Bins
// Make the call
Call call = callFactory.create(callParams);
// Print the call SID (a 32 digit hex like CA123..)
System.out.println(call.getSid());
}
}
Related
If I have a Google Meet link, how can I programatically join the call? I can get the dial-in phone number and use something like Twilio, but then how can I set the caller ID to have a name?
I've seen various systems join calls with a specified name for a meet / hangout call.
I apologize for the vagueness of the question. I'm not sure how to better ask it - please add comments if you need clarification and I'll happily edit the question.
If you have a number in the request , You can just call uding the twillo API
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Call;
import com.twilio.type.PhoneNumber;
import java.net.URI;
public class Example {
// Find your Account Sid and Token at twilio.com/console
// DANGER! This is insecure. See http://twil.io/secure
public static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
public static final String AUTH_TOKEN = "your_auth_token";
public static void main(String[] args) {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Call call = Call.creator(
new com.twilio.type.PhoneNumber("+14155551212"),
new com.twilio.type.PhoneNumber("+15017122661"),
URI.create("http://demo.twilio.com/docs/voice.xml"))
.create();
System.out.println(call.getSid());
}
}
Source : https://www.twilio.com/docs/voice/make-calls#initiate-an-outbound-call-with-twilio
You can also use fetcher to get the existing :
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Call;
public class Example {
public static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
public static final String AUTH_TOKEN = "your_auth_token";
public static void main(String[] args) {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Call call = Call.fetcher("CA42ed11f93dc08b952027ffbc406d0868").fetch();
System.out.println(call.getTo());
}
}
When you have a Google Hangouts for Enterprise that comes with GSuite When a Google Hangouts meet starts, It gives a dial-in number with a pin.
You can connect using curl itself
curl 'https://api.twilio.com/2010-04-01/Accounts/AC8bc5f1756b2e10ce344333e0ec6f7acacc46/Calls.json' -X POST \
--data-urlencode 'To=+1 xxxx-xxxx-3235' \
--data-urlencode 'From=+1xxxxxxxxxx6' \
--data-urlencode 'Url=https://demo.twilio.com/welcome/voice/' \
--data-urlencode 'SendDigits=wwwww34975093##' \
-u AC8bc5f1756b2e10c824e0ec6f7acacc46:[AuthToken]
Source :
Twilio Join Google Hangouts Conference Call
Looks like you can set caller id once the number is verified, otherwise not
https://support.twilio.com/hc/en-us/articles/223180148-Unable-to-Display-a-Business-Name-or-Custom-Text-as-Caller-ID
Is there any way to output real time clock values in google sheets? Basically what I'm trying to do is use the values in a cell to create a live Gantt chart that will output the status of the machine based on the time value and the downtime reason the operator used.
There are 2 ways of achieving this. The first is the simplest, =now() gets updated every minute automatically. The second technique is more complex but offers more control, especially if you want to control the precise moment when the clock should get updated.
Easiest way - 20 seconds:
Apply the =NOW() function and change your recalculation setting to "On change and every minute" in your spreadsheet at File > Spreadsheet settings.
Long way - 5 to 30 minutes. Requires a server to execute code every x minutes
Its possible to do this via the API. There are a couple of ways to achieve this, but both involve the API.
1) Create a blank sheet and update the time in any cell using the API. Lets call this sheet universal. Simply inserting "=now()" into a cell will always ensure the time is updated every time an API call is made. Overwrite this cell with "=now()" in every API call. Then use the function : IMPORTRANGE() in any sheet referencing the call value in the universal sheet. This is the preferred option since you only need to create code for one sheet and then you can reference in any other sheet.
2) Same as above, but just write directly to your sheet and cell as opposed to a universal sheet. The disadvantage of this method is if you move cells around you will need to change your code.
Both options will require you to call the API and setting up a scheduled job to run the program every x minutes.
Instructions (JAVA - though you can follow the same logic for other languages). Pretty much most of the below is copied from Google Sheets API guide, I have listed all my steps below:
Step 1: Turn on the Google Sheets API
a Use this wizard to create or select a project in the Google Developers Console and automatically turn on the API. Click Continue, then Go to credentials.
b On the Add credentials to your project page, click the Cancel button.
c At the top of the page, select the OAuth consent screen tab. Select an Email address, enter a Product name if not already set, and click the Save button.
d Select the Credentials tab, click the Create credentials button and select OAuth client ID.
e Select the application type Other, enter the name "Google Sheets API Quickstart", and click the Create button.
f Click OK to dismiss the resulting dialog.
g Click the file_download (Download JSON) button to the right of the client ID.
h Move this file to your working directory and rename it client_secret.json.
Step 2 If using Maven add the following dependencies to your pom file:
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client-java6</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-sheets</artifactId>
<version>v4-rev504-1.23.0</version>
</dependency>
Step 3 Copy this class Credentials.java
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
public class Credentials {
/** Application name. */
private static final String APPLICATION_NAME =
"Google Sheets";
/** Directory to store user credentials for this application. */
private static final java.io.File DATA_STORE_DIR = new java.io.File(
System.getProperty("user.home"), ".credentials/sheets.googleapis.com-java-quickstart");
/** Global instance of the {#link FileDataStoreFactory}. */
private static FileDataStoreFactory DATA_STORE_FACTORY;
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY =
JacksonFactory.getDefaultInstance();
/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;
/** Global instance of the scopes required by this quickstart.
*
* If modifying these scopes, delete your previously saved credentials
* at ~/.credentials/sheets.googleapis.com-java-quickstart
*/
private static final List<String> SCOPES =
Arrays.asList(SheetsScopes.SPREADSHEETS);
static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
/**
* Creates an authorized Credential object.
* #return an authorized Credential object.
* #throws IOException
*/
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in =
Quickstart.class.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
System.out.println(
"Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}
/**
* Build and return an authorized Sheets API client service.
* #return an authorized Sheets API client service
* #throws IOException
*/
public static Sheets getSheetsService() throws IOException {
Credential credential = authorize();
return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
}
Step 4 Copy the class UpdateDate.java below. Change the spreadsheetID with the ID of your spreadsheet. Change the range with your cell ref. I have entered a default value of Sheet1!a1 as an example
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.model.UpdateValuesResponse;
import com.google.api.services.sheets.v4.model.ValueRange;
public class UpdateDate {
public static void main(String[] args) throws IOException{
Sheets service = Credentials.getSheetsService();
String spreadsheetId = "YOUR_SHEET_ID";
String range = "Sheet1!a1";
#SuppressWarnings("unchecked")
List<List<Object>> values = Arrays.asList(
Arrays.asList(
(Object)"=NOW()"
)
);
ValueRange body = new ValueRange().setValues(values);
UpdateValuesResponse result =
service.spreadsheets().values().update(spreadsheetId, range, body)
.setValueInputOption("USER_ENTERED")
.execute();
}
}
Step 5 Setup a scheduled job to run every x minutes according to your use case. The very first time you run this your browser should open and you will need to click on the accept consent screen. Credentials are then saved locally so thereafter you can run the process via command line without having to click on the consent screen again.
Good Luck!
It looks like you want the NOW function. I don't think sheets supports real time updates, so a volatile function (changes when the sheet is updated) is the best you can do.
I am able to call a number with following code from the link
https://www.twilio.com/docs/api/voice/making-calls
Is it possible to enable dual channel recording with following code ?. if yes, how ?
SDK Version: 6.x 7.x
// Install the Java helper library from twilio.com/docs/java/install
import java.net.URI;
import java.net.URISyntaxException;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Call;
import com.twilio.type.PhoneNumber;
public class Example {
// Find your Account Sid and Token at twilio.com/user/account
public static final String ACCOUNT_SID = "ACd6b6b7dc8ae6f3e6f7ff72c8dbbd457f";
public static final String AUTH_TOKEN = "your_auth_token";
public static void main(String[] args) throws URISyntaxException {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Call call = Call.creator(new PhoneNumber("+14155551212"), new PhoneNumber("+15017250604"),
new URI("http://demo.twilio.com/docs/voice.xml")).create();
System.out.println(call.getSid());
}
}
Twilio developer evangelist here.
You are missing the parameter to record the call in your API call here. When building the call object you need something like the following:
Call call = Call.creator(
new PhoneNumber("+14155551212"),
new PhoneNumber("+15017250604"),
new URI("https://example.com/voice")
)
.setRecord(true)
.setRecordingChannels("dual")
.setRecordingStatusCallback("https://example.com/recording")
.create();
The URL that you pass to the call creator should point at an application that you control as well. This application needs to return TwiML that will connect the first call to another to give you the two legs to record. You need to use <Dial> with either <Number>, <Client>, <Sip> or <Sim>. Like this:
<Response>
<Dial>
<Number>NUMBER TO CONNECT TO</Number>
</Dial>
</Response>
Let me know if this helps at all.
I'm developing an "Apache Oltu Spring MVC Github" integration example. In this example I will be sending "App ID" and "Secret" to get the "access_token" in order to access the protected resources like "Gist", "user" etc.
So first step is to create / register the "App" using https://github.com/settings/applications/new.
Once you create a App make sure to Note: AppID and Secret, we need these values to be used in Spring code.
To develop this functionality / code - I search a lot and I did not find any ready made code. So I decided to furnish / explain my code below. So one can find these links useful.
I've taken a reference of following URL's to developed whole code:-
https://developer.github.com/v3/oauth/
https://cwiki.apache.org/confluence/display/OLTU/OAuth+2.0+Client+Quickstart
http://www.jasha.eu/blogposts/2013/09/retrieve-facebook-profile-data-java-apache-oltu.html
Attached is the screen shot to register the "App" on Github. "MyApp" is the App that I created.
Use the same code from http://www.jasha.eu/blogposts/2013/09/retrieve-facebook-profile-data-java-apache-oltu.html, just make sure to change the
AUTHORIZATION_URL = "https://github.com/login/oauth/authorize";
ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token"
To get Protected Resource like User Profile use: https://api.github.com/user
The output I get when run the code:
The user4798111 has mentioned is correct and just adding some more details. Pre-requisite, you need to register App on Github. Once you registered the App, you will get the CLIENT_SECRET,CLIENT_ID to be used to get the Protected resources from the github.
If you're using the OAuthClientRequest API to to make an initial call, you need to have the following details
private static final String AUTHORIZATION_URL = "https://github.com/login/oauth/authorize";
private static final String CLIENT_ID = "8515a1e4XXXXXXX";
private static final String CLIENT_SECRET = "ae3487601d891d257f7193XXXXXXXXX";
private static final String REDIRECT_URL = "http://localhost:8080/apache-oltu/github/redirect";
private static final String ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token";
#RequestMapping(value = "/auth", method = RequestMethod.GET)
public String authenticate() throws OAuthSystemException {
OAuthClientRequest request = OAuthClientRequest
.authorizationLocation(AUTHORIZATION_URL)
.setClientId(CLIENT_ID)
.setRedirectURI(REDIRECT_URL)
.setResponseType("code")
.setScope("user,gist,user:email,user:follow,public_repo,repo,repo_deployment,repo:status,repo:invite")
.buildQueryMessage();
System.out.println("REDIRECT TO: "+request.getLocationUri());
return "redirect:" + request.getLocationUri();
}
The same something simllar you would need to use like below
request= new OAuthBearerClientRequest("https://api.github.com/user").
setAccessToken(oAuthResponse.getAccessToken()).
buildQueryMessage();
The information about the scopes and other details can be found here:
https://developer.github.com/apps/building-integrations/setting-up-and-registering-oauth-apps/about-authorization-options-for-oauth-apps/
https://developer.github.com/apps/building-integrations/setting-up-and-registering-oauth-apps/about-scopes-for-oauth-apps/
The result which could see is below for reference:
{
"login":"JavaHelper",
"id":8208031,
"avatar_url":"https://avatars0.githubusercontent.com/u/8208031?v=4",
"gravatar_id":"",
"url":"https://api.github.com/users/JavaHelper",
"html_url":"https://github.com/JavaHelper",
"followers_url":"https://api.github.com/users/JavaHelper/followers",
"following_url":"https://api.github.com/users/JavaHelper/following{/other_user}",
"gists_url":"https://api.github.com/users/JavaHelper/gists{/gist_id}",
"starred_url":"https://api.github.com/users/JavaHelper/starred{/owner}{/repo}",
"subscriptions_url":"https://api.github.com/users/JavaHelper/subscriptions",
"organizations_url":"https://api.github.com/users/JavaHelper/orgs",
"repos_url":"https://api.github.com/users/JavaHelper/repos",
"events_url":"https://api.github.com/users/JavaHelper/events{/privacy}",
"received_events_url":"https://api.github.com/users/JavaHelper/received_events",
"type":"User",
"site_admin":false,
"name":"JavaProgramer",
"company":null,
"blog":"",
"location":null,
"email":null,
"hireable":null,
"bio":null,
"public_repos":45,
"public_gists":0,
"followers":4,
"following":60,
"created_at":"2014-07-19T10:03:42Z",
"updated_at":"2017-09-09T12:55:57Z",
"private_gists":0,
"total_private_repos":0,
"owned_private_repos":0,
"disk_usage":142270,
"collaborators":0,
"two_factor_authentication":false,
"plan":{
"name":"free",
"space":976562499,
"collaborators":0,
"private_repos":0
}
}
On the following link there is code which should access the Google Calendar api using OAuth 2.0. Unfortunately it uses the Draft 10 Client Library which is apparently deprecated.
https://developers.google.com/google-apps/calendar/instantiate
The latest Client Library is google-api-java-client-1.12.0-beta. From what I can make out things have changed a lot since the Draft 10 Client Library and I can't work out how to rewrite this code for the current Client Library.
The deprecated code is shown below.
import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.calendar.Calendar;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
...
public void setUp() throws IOException {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
// The clientId and clientSecret are copied from the API Access tab on
// the Google APIs Console
String clientId = "YOUR_CLIENT_ID";
String clientSecret = "YOUR_CLIENT_SECRET";
// Or your redirect URL for web based applications.
String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
String scope = "https://www.googleapis.com/auth/calendar";
// Step 1: Authorize -->
String authorizationUrl = new GoogleAuthorizationRequestUrl(clientId, redirectUrl, scope)
.build();
// Point or redirect your user to the authorizationUrl.
System.out.println("Go to the following link in your browser:");
System.out.println(authorizationUrl);
// Read the authorization code from the standard input stream.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is the authorization code?");
String code = in.readLine();
// End of Step 1 <--
// Step 2: Exchange -->
AccessTokenResponse response = new GoogleAuthorizationCodeGrant(httpTransport, jsonFactory,
clientId, clientSecret, code, redirectUrl).execute();
// End of Step 2 <--
GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
response.accessToken, httpTransport, jsonFactory, clientId, clientSecret,
response.refreshToken);
Calendar service = new Calendar(httpTransport, accessProtectedResource, jsonFactory);
service.setApplicationName("YOUR_APPLICATION_NAME");
...
}
...
Can anyone tell how to rewrite this code so it works with the current Client Library?
You can have a look at the documentation of the Google Drive API, which is up to date:
https://developers.google.com/drive/credentials
Then it shouldn't be hard to replace the Drive scope with the Calendar scope and to instantiate the correct Calendar class for the service instead of a Drive service object.
If that situation happens again, the best to be sure you can find the latest version of code samples is to have a look directly on the website of the Google APIs Client Library for Java. You can have a look at it's wiki about auth but also have a look at the sample apps which they make sure are compiling and working with he latest version of the library (it's just harder to keep all the docs up to date sometimes)