read Local audio file STT Google speech-to-text - json

I don't find how to send a local file using the Google STT in curl command line.
As followed in their tuto, here my request.json :
{
"config": {
"encoding":"FLAC",
"sample_rate": 16000,
"language_code": "fr-FR"
},
"audio": {
"uri":"audio-file.flac"
}
}
my command line is:
curl -s -X POST -H "Content-Type: application/json" --data-binary #request.json "https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=MY_API_KEY"
But I receive the error result:
{
"error": {
"code": 400,
"message": "RecognitionAudio not set.",
"status": "INVALID_ARGUMENT"
}
}
How can I write the request.json to send a local audio file ??
Thanks for help
:)

You need to use the base64 in the content section of the request.json file. It should look something like this except with a lot more base64 data:
{
'config': {
"encoding":"FLAC",
"sample_rate": 16000,
"language_code": "fr-FR"
},
'audio': {
'content':'UklGRuRDFQBXQVZFZm10IBAAAAABAAEAQB8AAIA+AAACABAAZGF0YcBDFQAIAAgACAAIAAgA
CAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgA
CAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgA
CAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAD4/wgACAD4//j/+P8IAPj/+P8IAAgA
+P8IAAgACAAIAAgA+
'
}
}

Google doesn't provide an option to specify local file path in json object while using curl command.We are require to convert audio file into base64 and pass the output into json object
base64 source_audio_file -w 0 > dest_base64_audio_file_content
Alternatively one of the best approach to pass localfile is to use gcloud ml command.
gcloud ml speech recognize 'Clip48_Mike.wav' --language-code='en-US'
Example:
NOTE:
Following command to activate gcloud account if not already done.
gcloud auth activate-service-account --key-file /tmp/account-name.json

Related

Wrong file type when sending PDF files through Whatsapp API using Postman

I am trying to send a message to myself containing a PDF file stored in Onedrive through Whatsapp API but I am getting the following jpg format:
Message in Whatsapp APP
Oddly enough, when I check Whatsapp Web the same message shows correctly:
Message in Whatsapp Web
What am I doing wrong?
I followed the official documentation and uploaded the media with the following values:
values
This returns me the id:
"id": "7054xxxxxxxxxxx"
Finally, I proceed to send the messsage using that same id:
{
"messaging_product": "whatsapp",
"to": "{{Recipient-Phone-Number}}",
"type": "document",
"document": {
"id": "7054xxxxxxxxxxx",
"caption": "Hello, here is today's Daily Report",
"filename": "dailyreport.pdf"
}
}
Also, I tried retrieving the media URL and got the following output:
{
"url": "https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=705479771132690&ext=1670800000&hash=ATvvKq-JEv6kn0bW7kq8SXy00yD1BLX_MMbOK-xxxxxx",
"mime_type": "image/jpeg",
"sha256": "067e1ac9488efc068dbb1ee4a35ae30c1dec575a70a4dcd77f6cbdc396615912",
"file_size": 1912582,
"id": "705479771132690",
"messaging_product": "whatsapp"
}
I guess "mime_type" should be "application_pdf" instead of "image/jpeg". I have no idea why it would say it is an image.
I am pretty new with this so it may be a simple mistake I am not seeing.
Thank you in advanced for your help!
This is a working example to send a pdf:
curl -X POST 'https://graph.facebook.com/v15.0/<MEDIA_ID>/media' \
-H 'Authorization: Bearer <ACCESS_TOKEN>' \
-F 'file=#"documents/sales.pdf"' \
-F 'type="application/pdf"' \
-F 'messaging_product="whatsapp"'
You are supposed to set "type" to the media type you're posting.
See details of supported media types, and limitations, here:
https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#supported-media-types
Try setting the header as content-type not just type:
Content-Type: application/pdf

Failed to read body as a JSON Object on curl command

I am testing out push notifications for iOS Development and am currently using Pusher. When I run this command in terminal
curl -H "Content-Type:application/json" -H "Authorization: Bearer 7FF3763C208E26F2198CE44624E9486B1EC8B02A489DCF2F476FD511763887AE" -X POST 'https://d3ca6e4b-3222-49c9-8028-f9c2d28b3ad7.pushnotifications.pusher.com/publish_api/v1/instances/d3ca6e4b-3222-49c9-8028-f9c2d28b3ad7/publishes' -d#publish-body.json
It gives me this warning
Warning: Couldn't read data from file "publish-body.json", this makes an empty
Warning: POST.
{"error":"Bad Request","description":"Failed to read body as a JSON
object"}
Here is my JSON file
{
"interests": [
"Reminder"
],
"apns": {
"aps": {
"alert": {
"title": "Hello",
"body": "Hello, world!"
}
}
}
}
I tried going to the Pusher API and all it told me is that the error is due to a bad request. Any ideas on how to fix this? This app is a gift for my girlfriend and I would love to have this working!

How to use Google Natural Language processing from Google Cloud Storage?

I have a sample code here. It is json
{
"document":{
"type":"PLAIN_TEXT",
"content":"Joanne Rowling, who writes under the pen names J. K. Rowling and Robert Galbraith, is a British novelist and screenwriter who wrote the Harry Pott$
},
"encodingType":"UTF8"
}
I found a tutorial on google's documentation on Natural Language processing on reading from Google Cloud Storage.
curl -X POST \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \ --data "{
'document':{
'type':'PLAIN_TEXT',
'gcsContentUri':'gs://reubucket/textData'
}
}" "https://language.googleapis.com/v1/documents:analyzeEntitySentiment"
And the error that I got is
ERROR: (gcloud.auth) Invalid choice: '*************-_m6csS1Wzlj1pyC_J7vzC0'.
Usage: gcloud auth [optional flags] <group | command>
group may be application-default
command may be activate-service-account | configure-docker | list |
login | revoke
How do I call the command with my API key.
I need a way to change the "content" to entries into my CSV file.
Thank you.
Here is an example of the error that I am receiving please help:
mufaroshumba#reucybertextmining:~/myFolder$ gcloud auth activate-service-account --key-file="/home/mufaroshumba/myFolder/reucybertextmining-74fa66372251.json"
Activated service account credentials for: [starting-*******[CENSORED]#reucybertextmining.iam.gserviceaccount.com]
mufaroshumba#reucybertextmining:~/myFolder$ curl "https://language.googleapis.com/v1/documents:analyzeSentiment?key=${API_KEY}" \ -s -X POST -H "Content-Type: app
lication/json" --data-binary #request.json
{
"error": {
"code": 401,
"message": "Permission to access the GCS address is denied.",
"status": "UNAUTHENTICATED",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "document.gcs_content_uri",
"description": "Permission to access the GCS address is denied."
}
]
}
]
}
}
curl: (6) Could not resolve host: -s
mufaroshumba#reucybertextmining:~/myFolder$
I then used this website trying to get
It looks like your auth is not setup correctly. If you just run this command:
gcloud auth application-default print-access-token
it should be giving you a token, but it seems like it's not. Please follow the steps here to make sure that this command is working first:
https://cloud.google.com/natural-language/docs/quickstart#quickstart-analyze-entities-cli
Then, as long as you have permission to access the gcs bucket, you should be able to get content out of it. Note that the API is expecting to see the actual content in the gcs file, and not a CSV.

Curl - POST request using JSON with query parameter

I have a URL https://mylink/apply?data= for a server which accepts the data variable as valid JSON of following properties:-
name: String with your name (required)
email: String with your email address (required)
urls: Array of strings with links
comment: String with any comment/request you might have (optional)
My JSON file "data.json" is the following:
{
"data":
{
"name": "Rogers Bell",
"email": "xxxx#gmail.com",
"urls":["https://stackoverflow.com/users/3067241/imran", "https://github.com/i4ali"],
"comment":"none"
}
}
When I use the following CURL command to send a POST request, I get error code 400 Bad Request with a message from the server Error: no data passed. indicating something wrong with my JSON format or command. Not sure what I am doing wrong here
curl -v -i -X POST -d #data.json https://mylink/apply?data= -H "Accept: application/json" -H "Content-Type: application/json" -k
I am using Windows 7 if that matters
The API is pretty weird. But if you have to read JSON file and POST its content to server as URL parameter, you can cat the file, encode it and send it in curl. Here is an example:
curl -G -X POST https://requestb.in/1n88lah1 --data-urlencode data="$(cat input.json)"
The input.json would look like:
{
"name": "Rogers Bell",
"email": "xxxx#gmail.com",
"urls":["https://stackoverflow.com/users/3067241/imran", "https://github.com/i4ali"],
"comment":"none"
}
Using Postman I was able to send this POST request with the following complete URL. Got a 200 OK response
https://mylink/apply?data={
"name": "Rogers Bell",
"email": "xxxx#gmail.com",
"urls":["https://stackoverflow.com/users/3067241/imran", "https://github.com/i4ali"],
"comment":"none"
}

Bash script: Use string variable in curl JSON Post data

I want to send a json request and embedd a variable in the post data.
I did a little research and I came up with the single quotes around the variable.
#!/bin/bash
FILENAME="/media/file.avi"
curl -i -X POST -H "Content-Type: application/json" —d '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{"file":"'$FILENAME'"}}}' http://192.167.0.13/jsonrpc
Unfortunately I get some errors:
curl: (6) Couldn't resolve host '—d'
curl: (3) [globbing] nested braces not supported at pos 54
HTTP/1.1 200 OK
Content-Length: 76
Content-Type: application/json
Date: Wed, 29 Jan 2014 19:16:56 GMT
{"error":{"code":-32700,"message":"Parse error."},"id":null,"jsonrpc":"2.0"}
Appearently there are some problems with the braces and the http answer states, that the command could not be executed. What's wrong with my code here?
Thanks!
This is my curl version:
curl 7.30.0 (mips-unknown-linux-gnu) libcurl/7.30.0 OpenSSL/0.9.8y
Protocols: file ftp ftps http https imap imaps pop3 pop3s rtsp smtp smtps tftp
Features: IPv6 Largefile NTLM NTLM_WB SSL
Update: use the simpler
request_body=$(cat <<EOF
{
"jsonrpc": "2.0",
"method": "Player.Open",
"params": {
"item": {
"file": "$FILENAME"
}
}
}
EOF
)
rather than what I explain below. However, if it is an option, use jq to generate the JSON instead. This ensures that the value of $FILENAME is properly quoted.
request_body=$(jq -n --arg fname "$FILENAME" '
{
jsonrpc: "2.0",
method: "Player.Open",
params: {item: {file: $fname}}
}'
It would be simpler to define a variable with the contents of the request body first:
#!/bin/bash
header="Content-Type: application/json"
FILENAME="/media/file.avi"
request_body=$(< <(cat <<EOF
{
"jsonrpc": "2.0",
"method": "Player.Open",
"params": {
"item": {
"file": "$FILENAME"
}
}
}
EOF
))
curl -i -X POST -H "$header" -d "$request_body" http://192.167.0.13/jsonrpc
This definition might require an explanation to understand, but note two big benefits:
You eliminate a level of quoting
You can easily format the text for readability.
First, you have a simple command substitution that reads from a file:
$( < ... ) # bash improvement over $( cat ... )
Instead of a file name, though, you specify a process substitution, in which the output of a command is used as if it were the body of a file.
The command in the process substitution is simply cat, which reads from a here document. It is the here document that contains your request body.
My suggestion:
#!/bin/bash
FILENAME="/media/file 2.avi"
curl -i -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{"file":"'"$FILENAME"'"}}}' http://192.167.0.13/jsonrpc
The differences are hyphen in -d (instead of a dash) and double quotes around $FILENAME.
Here is another way to insert data from a file into a JSON property.
This solution is based on a really cool command called jq.
Below is an example which prepares request JSON data, used to create a CoreOS droplet on Digital Ocean:
# Load the cloud config to variable
user_data=$(cat config/cloud-config)
# Prepare the request data
request_data='{
"name": "server name",
"region": "fra1",
"size": "512mb",
"image": "coreos-stable",
"backups": false,
"ipv6": true,
"user_data": "---this content will be replaced---",
"ssh_keys": [1234, 2345]
}'
# Insert data from file into the user_data property
request_data=$(echo $request_data | jq ". + {user_data: \"$user_data\"}")