Localization of app-owned Open Graph objects - facebook

The localization procedure for Open Graph is tightly coupled to HTTP. There is a magical facebook HTTP-header or URL parameter given when scraping which should be used to change the value of localized data.
When using app-owned objects, the object is simply created using JSON. The question I have is: how do I localize an app-owned object?
Here is an example of the creation of an app-owned object. What can I do to localize the title?
$ curl -X POST "https://graph.facebook.com/app/objects/mynamespace:myobject" \
-F "$ACCESS" \
-F 'object = { "title" : "My Unlocalized Title",
"image" : "https://example.com/myimage.png",
"url" : "https://example.com/myobject" }'

In Android an id is returned in the Callback. Then I think you can use this id to localize the title. Sorry if I dont answer your question.
https://developers.facebook.com/docs/opengraph/guides/internationalization/#hosted

Related

How to correlate two text messages in facebook messenger API

I'm implementing a chat bot using messenger API. In a scenario like this, how to match answers with the question when both of them are in text message format. I can't use pattern matching here.
bot q1: How much is it?
user: 250
bot q2: How many?
user: 5
Is there a way to send meta data with a text message and get it as a post back. Is it required to store the last message.?
In the message field, in the same level of text field, you can define a field metadata, like defined in the doc (which has a 1000 character limit):
Custom string that will be re-delivered to webhook listeners
So it could be sth like that:
curl -X POST -H "Content-Type: application/json" -d '{
"recipient":{
"id":"USER_ID"
},
"message":{
"text":"hello, world!",
"metadata": "my meta data"
}
}' "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"

Get the next_available_ip from an Infoblox WAPI server using a dynamic network search, via REST

I have a collection of network objects in my Infoblox (IPAM) server and would like to allocate an IP address from one of them based on a search into their extattr fields.
Each network object has an extended attribute named CITYCODE, and many networks have the same value for that code. I want to allocate an IP address from any of those networks.
How can I use the next_available_ip function to create a record:host object, where the IPV4ADDR is determined by the next_available_ip function?
Answering my own question because it took some poking with other example code I found to get one that worked exactly right for me.
First you need a JSON body that specifies the fully qualified name of the new record:host. For my purposes I'm just generating random values. Note, that the domain must already exist (likely as a zone:auth object).
If the domain does not already exist you'll get the error: "The action is not allowed. A parent was not found."
Instead of specifying an actual ipv4addr, instead specify the target object and the function you will run against it. Since we want to run the next_available_ip function, the object is network.
The search we want to do is against an extensible attribute, so the _object_parameters are set to say "find networks that have an extensible attribute called -CITYCODE- with value -MZAPPKAK-. The asterisk is important and must be kept.
BODY='
{
"name" : "staticip-d4260ed7-3101-4034-0f79-056b72b2c5f0.some-domain-that-exists.com",
"ipv4addrs : [
{
"ipv4addr" : {
"_object" : "network",
"_object_function" : "next_available_ip",
"_object_parameters" : {"*CITYCODE" : "MZAPPKAK"},
"_object_field" : "value",
"_result_field" : "ips",
"_parameters" : {"num" : 1}
}
}
]
}
curl \
'https://1.2.3.4/wapi/v2.2.1/record:host' \
-H 'Content-Type: application/json' \
-d "$BODY"
The result code should be 201, and the result body will be a REF to the record:host that was created. Do a standard GET on the returned REF to learn what IP was found and placed on the record.

Difference between XPOST and XPUT

I'm just starting to use ElasticSearch. And I tried to know how to insert documents. I only found examples using the PUT method : $ curl -XPUT 'http://localhost:9200/...'
But it also seems to work using POST. Is there any difference between these two methods?
Thank you.
Generally when using a REST API:
- POST is used to create a resource, where the server will pick an ID.
- PUT is used to update OR PLACE a resource at a known ID.
Doc creation examples in the ES documentation show the caller picking an ID.
Like so:
curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}'
Since the caller is picking the ID a PUT seems appropriate here.
BUT
using POST Elasticsearch can also generate an ID for you.
$ curl -XPOST 'http://localhost:9200/twitter/tweet/' -d '{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}'
Somehow they have the same functionality with small different
PUT verb (“store this document at this URL”)
POST verb (“store this document under this URL”)
in the put you must indicate the exact URL but in the post you can set document by just type
for example:
PUT /website/blog/123 says put this document at exact this URL but POST /website/blog will insert the document in blog and auto increment the id of the last document.

How to use scaffolding and RESTfulness together in Grails 2.3

Official Grails documentation says that
Version 2.0.x of the scaffolding plugin includes different scaffolding
templates that are aligned with the new REST APIs introcued in Grails
2.3 and above.
(taken from here http://grails.org/doc/latest/guide/scaffolding.html)
But I can't make (or I don't understand the concept) work RESTfulness together with scaffolding.
Let's start from scratch:
grails create-app myapp
cd myapp/
grails create-domain-class Book
grails create-scaffold-controller myapp.Book
Add a field to the domain class
class Book {
String text
static constraints = {
}
}
and run the app with grails run-app.
Surfing on the http://localhost:8080/myapp/ shows that scaffolding works great:
http://localhost:8080/myapp/book/index page shows books list
http://localhost:8080/myapp/book/show/1 page show details for the book with id = 1
http://localhost:8080/myapp/book/create page creates a book
and so force, good old scaffolding.
Let's see what about REST.
Official docs say I should use URLs like http://localhost:8080/myapp/books/... for the REST but any attempt to access the app, like this curl -i -H "Accept: application/json" localhost:8080/myapp/books/1 returns 404 with bunch of HTML.
Ok, let's read docs carefully:
The easiest way to create a RESTful API in Grails is to expose a
domain class as a REST resource. This can be done by adding the
grails.rest.Resource transformation to any domain class
No problem, now the Book class heading is
import grails.rest.*
#Resource(uri='/books') class Book {
Now surfing on the http://localhost:8080/myapp/ shows that scaffolding is broken:
http://localhost:8080/myapp/book/index page shows books list
http://localhost:8080/myapp/book/create page shows xml output <?xml version="1.0" encoding="UTF-8"?><book><text /></book>
and so force, bad new xml output.
I'd played with #Resource and "/books"(resources:"book") in URLMappings.groovy but hadn't found any working solution which makes possible scaffolding and RESTfulness work back-to-back. Indeed, I managed to make them work separately.
Update
I'd found the way how to achieve the desired goal. The way I found is:
Mark the Book class with #Resource(uri = "/books").
Remove scaffold controller BookController.
Create dedicated controller with scaffolding for the Book: class HumanBookController {static scaffold = Book}
Now scaffold GUI pages with URLs like http://localhost:8080/myapp/humanBook/index work pretty well. Either json requests are handled well with URLs like http://localhost:8080/myapp/books/1. But it's not elegant to have 2 controllers doing same things for common web and json.
You can do this:
import grails.rest.RestfulController
class BookController extends RestfulController {
static responseFormats = ['html', 'json']
BookController() {
super(Book)
}
}
And then in the UrlMappings.groovy:
"/books"(resources:"book")
"/$controller/$action?/$id?(.${format})?"{
constraints {
// apply constraints here
}
}
No need to add #Resource in the domain.
You can now have /books/1.json or /books/1.html to point to the right places. You might still need to do grails generate-view Book to have the view generated. But although you need to generate the views for html, you keep only single controller and path.
I had the same problems as yours.
That might be a trivial solution and not for every case, but try updating your Grails version.
As for me: Grails 2.3.4 -> Grails 2.3.6 worked.
Hope that help anyone.
I'm currently using Grails 2.4.0, the solution came by doing this:
Controller: BookController { static scaffold = true }
Domain: Book { .... } // WITHOUT #Resource
The result is that you can:
/book.json to get a list JSONized
/book/index to get an HTML standard scaffolding
/book/create html scaffold for a new item
/book/show/1 html scaffold edit item 1
/book/show/1.json JSON for item id: 1
I'ts wicked, i know. I found this and it get me going.
With Grails 2.4.4 I was able to get the scaffolding working with single controller using the following steps:
Added a URL to Resource mapping in UrlMappings.groovy, e.g. "/books"(resources:"book")
Inserted static scaffold = true into the generated controller
I did not verify if the following made a difference, but I also set grails.mime.disable.accept.header.userAgents = [] and grails.mime.use.accept.header = true in Config.groovy (the latter is presumably the new default value).
Both of the scaffolded REST and UI interfaces are working fine with the following tests:
GET /app//1 (passing Accept header)
GET /app//1.json (no Accept header)
POST /app/ (with payload as json or form encoded)
DELETE /app//1
PUT /app//1 (with json payload. form payload updated the object, but sent back 302 redirects)
EDIT
Removed the Resource annotation step and clarified the URL mapping setup
The URI assigned in the URL mapping is not the same as the default URI for the controller. For example, "books" instead of "book". After adding this mapping, the URI for the controller will default to the URI in UrlMapping, but the original URI will continue to work.
The generated controller is a Restful controller because it implements actions aware of requests like:
curl -i -X GET yourDomain:8080/yourApp/books.json
It returns a list of books in json format. (10 books, assuming that you created test data, did you?)
You can append a parameter like:
curl -i -X GET yourDomain:8080/yourApp/books.xml?40
By default you will get the html format. You need to append .json or .xml to get the correct data.
You can to use the Accept header too
curl -i -X GET -H "Accept: application/xml" yourDomain/books/1
returns details of book with id=1 in xml format. Finally
curl -i -X POST -H "Content-Type: application/json" -d "{name: 'Book'}" yourDomain/books
creates a new book and
curl -i -X PUT -H "Content-Type: application/json" -d "{name: 'Book'}" yourDomain/books/1
updates name of book with id=1
All resources need to be exposes through and url. The url is not generated for you, you should write it on UrlMappings file:
"/v1/books"(resources: "book")
Where the first string "/v1/books" is the uri and the second string "book" is the controller name following the grails convention. (The preceding v1 string is because I always put version number to my API URIs)
| GET | /v1/books | Action: index |
| GET | /v1/books/create | Action: create |
| POST | /v1/books | Action: save |
| GET | /v1/books/${id} | Action: show |
| GET | /v1/books/${id}/edit | Action: edit |
| PUT | /v1/books/${id} | Action: update |
| DELETE | /v1/books/${id} | Action: delete |
All that should be required is #Resource annotation with the uri on the Domain class. If you want specific formats (default format is first), also include the formats:
#Resource(uri='/books', formats=['json', 'xml'])
That should be it. If ypu are still having trouble finding your dynamic #Resource endpoint, try running:
grails url-mappings-report
That will give you a nice summary of all urls, including those backed by scaffolded controllers for #Resource domains. I have found that I tend to make silly mistakes when trying to "guess" the URL - using the report output ensures you and grails are in agreement.

sinatra + mongoid

I'm creating an app that use sinatra + mongoid. I have two models, contact has many phones. To test my sinatra controller I post my data with this command `
curl -X POST -d "contact[name]=nome&contact[email]=email#dominio.com&contact[phone][0][number]=88888888&contact[phone][0][type]=1&contact[phone][2][number]=77777777&contact[phone][3][type]=1"
but when I did one query in mongodb I see that not save as expected. I need that phone class will be save with array, but now phone is a hash where the key is "0", "1", N like my post data. How do I to resolve this problem? I want data to be saved so:
{
"_id":"4f889875b336e722a0000003",
"email":"diego.dias2#dominio.com.br",
"github":"diegodfsd",
"name":"diego2",
"phone":{
"0":{
"number":"89311768",
"type":"cellphone"
},
"1":{
"number":"55555555",
"type":"home"
}
},
"twitter":"diegodfsd"
}
gist
You need use phones_attributes params instead of phone
curl -X POST -d "contact[name]=nome&contact[email]=email#dominio.com&contact[phones_attributes][0][number]=88888888&contact[phones_attributes][0][type]=1&contact[phones_attributes][2][number]=77777777&contact[phones_attributes][3][type]=1"