How to send text queries from java to my api.ai agent? - java

I have created an agent on api.ai and it is working well. Now I want to pass text queries to the agent from a java project and get response from it. Any help?

This worked for me on my android project.
final AIConfiguration config = new
AIConfiguration("your key",
AIConfiguration.SupportedLanguages.English,
AIConfiguration.RecognitionEngine.System);
AIRequest request = new AIRequest("your text request");
AIService aiService = AIService.getService("implement new AIService", config);
try {
aiService.textRequest(request);
} catch (AIServiceException e) {
e.printStackTrace();
}

Related

"t.data.data is undefined" in grafana

I'm new to micrometer, prometheus and grafana. I'm trying to run my first example with these three things working together. But I'm having a hard time figuring out what I'm doing wrong.
I'm using micrometer's API to create a PrometheusMeterRegistry like this:
new PrometheusMeterRegistry(new PrometheusConfig() {
#Override
public Duration step() {
return Duration.ofSeconds(10);
}
#Override
#Nullable
public String get(String k) {
return null;
}
});
And I'm creating a HttpServer as described here using the following code:
try {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/prometheus", httpExchange -> {
String response = prometheusMeterRegistry.scrape();
httpExchange.sendResponseHeaders(200, response.getBytes().length);
try (OutputStream os = httpExchange.getResponseBody()) {
os.write(response.getBytes());
}
});
new Thread(server::start).start();
} catch (IOException e) {
throw new RuntimeException(e);
}
I configured Prometheus as my data-source in grafana and set the URL to http://localhost:8080/prometheus. But when I try creating a new dashboard and add a graph to it, I get a red exclamation mark saying t.data.data is undefined. And because of this I'm not able to view the graphs. The complete stack-trace is as follows:
kr</t.prototype.transform#http://localhost:3000/public/build/app.dfabdd44b3be44288eac.js:22:723420
jr</t.prototype.query/</<#http://localhost:3000/public/build/app.dfabdd44b3be44288eac.js:22:736135
qt#http://localhost:3000/public/build/vendor.dfabdd44b3be44288eac.js:9:5239
Wa#http://localhost:3000/public/build/vendor.dfabdd44b3be44288eac.js:9:40274
jr</t.prototype.query/<#http://localhost:3000/public/build/app.dfabdd44b3be44288eac.js:22:735858
c/</<#http://localhost:3000/public/build/vendor.dfabdd44b3be44288eac.js:130:92198
c/<#http://localhost:3000/public/build/vendor.dfabdd44b3be44288eac.js:130:92043
$digest#http://localhost:3000/public/build/vendor.dfabdd44b3be44288eac.js:130:97575
$apply#http://localhost:3000/public/build/vendor.dfabdd44b3be44288eac.js:130:99590
$applyAsync/r<#http://localhost:3000/public/build/vendor.dfabdd44b3be44288eac.js:130:99735
h#http://localhost:3000/public/build/vendor.dfabdd44b3be44288eac.js:130:33036
ln/this.$get</</i.defer/n<#http://localhost:3000/public/build/vendor.dfabdd44b3be44288eac.js:130:34396
The following is the screenshot of the error in grafana that I'm gettng:
Any suggestions what I might be missing?
Note: I can see timer getting published on native prometheus using URL localhost:8080/prometheus.
8080 is your application server. You need to run a Prometheus server that scrapes this application, then you can use localhost:9090 as a datasource.

send message to single device using Firebase cloud with Java

I'm trying to send messages to single devices using their token from a Java application. I'm using the Firebase Admin SDK. Below is what I have
FileInputStream serviceAccount = null;
try {
serviceAccount = new FileInputStream("google-services.json");
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
FirebaseOptions options = null;
try {
options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://MYPROJECTID.firebaseio.com/")
.build();
} catch (IOException e1) {
e1.printStackTrace();
}
FirebaseApp.initializeApp(options);
String registrationToken = "MYDEVICETOKEN";
// See documentation on defining a message payload.
Message message = Message.builder().putData("time", "2:45").setToken(registrationToken)
.build();
// Send a message to the device corresponding to the provided
// registration token.
String response = null;
try {
response = FirebaseMessaging.getInstance().sendAsync(message).get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
But I get the following exception
java.io.IOException: Error reading credentials from stream, 'type' field not specified.
What am I doing wrong here?
Click on Generate new private key button.
The error means that your google-services.json file contains invalid data. GoogleCredentials class expects your file to have a type property, but it's not there.
Brief googling gave me this post regarding very similar problem. It says:
From the API Manager, just create select "Create credentials" >
"Service Account key" and generate a new key for the Service
that is associated to your Google Play account.

Mailchimp Apache Error

im working with mailchimp, my objetive is to send users to the list in the mailchimp, so i used a wrapper from ecwid this one
So i did a method that send a test user to my list, i added my list id and my Api Key , but i have an error
this is my code
private void mailchimp(){
MailchimpClient client = new MailchimpClient("MY_API_KEY");
try {
EditMemberMethod.CreateOrUpdate method = new EditMemberMethod.CreateOrUpdate("MY_LIST_ID", "vasya.pupkin#gmail.com");
method.status = "subscribed";
method.merge_fields = new MailchimpObject();
method.merge_fields.mapping.put("FNAME", "Vasya");
method.merge_fields.mapping.put("LNAME", "Pupkin");
MemberInfo member = null;
Log.e("mailchimpmember",""+member);
member = client.execute(method);
} catch (IOException e) {
e.printStackTrace();
} catch (MailchimpException e) {
e.printStackTrace();
}
}
The problem is when i reach this method i get this from Apache in my android monitor at line MailchimpClient client = new MailchimpClient("MY_API_KEY");
No virtual method setConnectionManagerShared(Z)Lorg/apache/http/impl/client/HttpClientBuilder; in class Lorg/apache/http/impl/client/HttpClientBuilder; or its super classes (declaration of 'org.apache.http.impl.client.HttpClientBuilder' appears in /data/app/com.myapp.app.debug-1/split_lib_dependencies_apk.apk:classes78.dex)
at com.ecwid.maleorang.connector.HttpClientConnector.(HttpClientConnector.kt:71)
and this one
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/impl/client/HttpClientBuilder;
im really struggling to get mailchimp running in my project, i cant find a good wrapper and dont know how to properly set it up
thanks
Check this video tutorial about posting new members to MailChimp list
https://www.youtube.com/watch?v=TkRUi_vN12k
Or just try using Volley for the http requests

What is the right way to use Espresso in android?

I am trying to run an Espresso test for my android app, but there is a problem that has been troubling me. In MainActivity, some views' visibility depends on data loaded from net, but in MainActivityTest, I can't manipulate the process of loading data, so I don't know the real data and which view should show and which view should not show. As a result, I don't know how to continue my test. Anyone can tell me how to handle this situation? Thanks!
Try using the MockWebServer library. It lets you mock http responses in your tests, like this:
/**
* Constructor for the test. Set up the mock web server here, so that the base
* URL for the application can be changed before the application loads
*/
public MyActivityTest() {
MockWebServer server = new MockWebServer();
try {
server.start();
} catch (IOException e) {
e.printStackTrace();
}
//Set the base URL for the application
MyApplication.sBaseUrl = server.url("/").toString();
//Create a dispatcher to handle requests to the mock web server
Dispatcher dispatcher = new Dispatcher() {
#Override
public MockResponse dispatch(RecordedRequest recordedRequest) throws InterruptedException {
try {
//When the activity requests the profile data, send it this
if(recordedRequest.getPath().startsWith("/users/self")) {
String fileName = "profile_200.json";
InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
String jsonString = new String(ByteStreams.toByteArray(in));
return new MockResponse().setResponseCode(200).setBody(jsonString);
}
//When the activity requests the image data, send it this
if(recordedRequest.getPath().startsWith("/users/self/media/recent")) {
String fileName = "media_collection_model_test.json";
InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
String jsonString = new String(ByteStreams.toByteArray(in));
return new MockResponse().setResponseCode(200).setBody(jsonString);
}
} catch (IOException e) {
e.printStackTrace();
}
return new MockResponse().setResponseCode(404);
}
};
server.setDispatcher(dispatcher);
}

Tesseract implementing a web service to trigger OCR actions

I am trying to implement a web service which triggers OCR actions of the server side.
Client code:
...
sy = belgeArsivle(testServisIstegi, ab);
...
private static ServisYaniti belgeArsivle(com.ocr.ws.ServiceRequest serviceRequest,com.ocr.ws.Document document) {
com.ocr.ws.ServiceRequest service = new com.ocr.ws.OCRArsivWSService();
com.ocr.ws.OCRArsivWS port = service.getOCRArsivWSPort();
return port.docArchive(serviceRequest, document);
}
When I run the code on the server side there is no problem. But whenever I call the web service method from the client I got this error code:
Exception: javax.xml.ws.soap.SOAPFaultException: Unable to load library 'libtesseract302': The specified module could not be found.
The working server-side code is:
public static void main(String[] args) {
// TODO code application logic here
File imageFile = new File("...OCR\\testTurWithBarcodeScanned.png");
Tesseract instance = Tesseract.getInstance();
try {
String lang = "tur";
instance.setLanguage(lang);
String result = instance.doOCR(imageFile);
System.out.println(result);
// write in a file
try {
File file = new File("...MyOutputWithBarcode.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(result);
out.close();
} catch (IOException ex) {
}
} catch (TesseractException ep) {
System.err.println(ep.getMessage());
}
}
I know that this error code is about Tesseract libraries. I put the corresponding .dll files (liblept168 and libtesseract302) under the client project's folder, added corresponding libraries (jna, jai_imageio, ghost4j_0.3.1), did neccessary changes in classpath but still getting this error.
I run a test code on the server side, it works fine. But the client side code is not working. Do I need to make some extra adjustment on the client side to run this web service?
I found out that the actual problem was with the Tomcat Server. I had to put the jar files to the Tomcat's Sources under Properties, than voila!

Categories

Resources