WIT.AI how to post `context-key` - java

using Wit.ai HTML api.
Posting /converse message I get the response of type:action with action name, then I execute some my own code. How then make POST with context-key to wit API to get an answer from the robot?
Also how to trigger robot message when user do not need to say anything?

Your own code (the action you need to run) is responsible for updating the context (e.g. adding a key "forecast" with value "sunny"). In your Wit.ai /converse calls, you provide the context in the body.
See the example in the documentation: https://wit.ai/docs/http/20160526#post--converse-link
You can call your send action when you want to trigger a robot message.

Related

Telegram client for android: how to get last message?

I want to built a custom Telegram client for android with a function to save messages into file. As a base project, I'm using official source code: https://github.com/DrKLO/Telegram
And I can't find a way to actually get a message. In Documentation for Telegram mentioned method getChatHistory https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1get_chat_history.html
But I don't see any usage of the API in their code. Instead, they are used custom classes somehow similar to API. And the general class for any actions with messages is MessageObject. I can get the text content of this object by using this
message.messageText.toString();
where message is object of class MessageObject, but how I can create this object?
The supposed workflow is: when interested chat is opened, besides already existed common buttons, custom button for save message is appearing, after tapping this button, begins a cycle in which we create object of class MessageObject start with id of last message in chat, we extrating a text from this message, and saves it a file.

Correct way to handle a REST API path that can be called synchronously or asynchronously

I'm working on a Spring Boot REST API that handles document and can launch a check on a document.
I have a document resource: /doc:
Create a doc with POST /doc
Rest of the CRUD actions with /doc/{id}
Now I can launch a check on a doc, check can be seen either as an action or as a sub-resource.
It's pretty straightforward to launch (create) a check on a document: POST /doc/{id}/check
The check can however take some time so I want to give the user the choice to launch a synchronous or asynchronous check.
How would I handle this path wise?
Should the user choose sync or async check through a query parameter on POST /doc/{id}/check?
Should I create 2 separate paths?
Also in the case of an async check, I would create a temporary Task resource that can be pooled to know the status of the check.
But then if both check and task are returned from the same path it gets confusing, no?
I read an article that says the resource returned in async should be a check resource filled as much as possible but with a link to the task that can be pooled.
That seems like a good way; I would return a partial check if async with a link to the /task/{id} associated with the check.
However I'm still confused as to what path my API should offer to let the user pick between sync and async checks.
How would you handle it path and resource wise?
Basically it's up to you. Usually if it's a big chunk of data you want to query like /resource/{id} most APIs I have used use GET for synchronous requests and POST for async request returning task or job ID.
For POST in your case if the creation/checking takes time I would consider always doing it asynchronous and returning HTTP 202 Accepted and doc/{id}/check/{id} url where the user can see the result if it is ready or some status that it is still working.
If you want to give them a choice to wait or not it's up to you how to do it. There is a standard header that can be used to modify behavior. For example Expect: 202-accepted for async calls and no header or Expect: 201-created for synchronous calls. This makes the API a bit less clear even though it is a standard. Most people (including me) would probably stick to adding a parameter to the URL for clarification. I don't think it should be in the POST data because it should be data related to the object you are creating
There are multiple questions here. I would try to answer one by one
Checking the health of a resource can be done with query param
/doc/{id} - GET Get the resource details
/doc/{id}?healthCheck=true&async=true GET - Get the resource details and trigger an async health check
For the async health check the response as you mentioned will be 202 and the response contains the link to the health status URL
HTTP/1.1 202 Accepted
Location: /doc/12345/status
If the client sends a GET request to this endpoint, the response should contain the current status of the request. Optionally, it could also include an estimated time to completion or a link to cancel the operation.
Reference
https://learn.microsoft.com/en-us/azure/architecture/best-practices/api-design

How to create Ajax request that gets information as the servlet runs?

I have a form that creates an account and a servlet that handles the request.
However, the process to create this account is a long process and I want to create something like a status bar or a progress bar. Heres the POST:
$.post("createAccount.jsp", function(data) { $("#status").text(data);
});
And the servlet would continuously print data like "creating x..." then "creating y" as the servlet runs. Is there a way to accomplish this or maybe another way to tackle this issue?
Thanks
Http works on a request-response model. You send a request, and server responds back. After that Server doesn't know who are you?!
It's like Server is a post-office that doesn't know your address. You
go to it and get your letters.It doesn't come to your home for
delivering letters.
If you want constant notifications from server, You can either use Web Sockets(Stack Overflow also uses Web Sockets) or use `AJAX Polling' mechanisms,
which sends an AJAX request to the server and waits for server to
respond. On retrieval of response,it generates another AJAX request
and keep on doing the same until server stops generating new data.
Read this for an explanation of AJAX Polling techniques
You could have your account creation servlet update a database or context attribute as it creates the account.
You could have a separate AJAX request to a different servlet that sends back to the webpage the most recent development found in the database or context attribute. You would then poll your server with that AJAX request every so many fractions of a second(or relevant time interval depending on how long of a task it is to create an account) to get all the updates.

How to avoid saving details in database when a new user tries to create account with existing username?

I have a jsp page in my project where user fills up the details for creating an account.
when a user enters username and clicks on the check button, the button looks in the database if the same name exists or not(it is able to check because of the servlet code).
If username exists it shows not available.
Now the problem is even if username is not available when user clicks on sumbit button with existing username the details get saved. how to correct this?
(I'm nt able to post image otherwise it would have been more clear.)
Just add an if-else block to your servlet, something like this:
if (usernameExists) {
showError();
} else {
saveUser();
}
Do not do this:
if (usernameExists) {
showError();
}
saveUser();
I'd also add an UNIQUE constraint on the username column in the DB so that your DAO throws an SQLException or like.
See also:
Our Servlets wiki page - contains a basic Hello world example with server-side validation
First, you serlvet accepting the HTTP POST must validate the data sent to it, when the user clicks a button, if the receiver doesn't validate the information then bad data will get into your system regardless of what you do in the JSP.
Some people send raw HTTP POST messages from time to time just for fun (I don't know why :) ) to see if bad data can get into poorly written web applications.
Once the servlet accepting the POST rejects bad data, you can have it redirect back to the offending web page, filled out with the information that was sent in the bad request, perhaps highlighting the offending field or fields.
Later on, if you have the time, you can write up a bunch of javascript to pre-check the fields and deactivate the submit button. This saves the back end servlet the bother of receiving so many bad requests; however, you cannot use such a technique to avoid fixing the back end servlet. There's too many ways your servlet could get the POST message that don't involve your specific javascript code working.

How can I correlate the response from Google Checkout to the correct order in the database?

When I get the notifications from google at the callback URL I am unable to correlate the messages to the correct order in the database. Can I pass some identifier that would be sent back to me as a part of the callback notification? How is this done in general?
I am using the latest google-checkout-java SDK.
You should be able to use your merchant-private-data-section for this
<checkout-shopping-cart xmlns="http://checkout.google.com/schema/2">
<shopping-cart>
<merchant-private-data>
<your-ref>019b1723a2754981ed5bc24e6ac9f501</your-ref>
</merchant-private-data>
[...]
</shopping-cart>
</checkout-shopping-cart>
When you get the notification, you can read this data.
See also this question and especially this answer. I'd only note additionally that you don't have to use merchant-note (or your-ref), but can define any tags of your own as merchant-private-data is defined as a sequence of xs:any.
Hope that helps...

Categories