I am trying to post message to Microsoft Teams channel using Windows batch script but I could not make use of the Teams message card formats. I am able to post messages using the below commands but as plain texts. Is there anyway in which I can make use of the Message card JSON formats ?
I also have some command line arguments which need to be used for the batch script so that the message displayed uses the same arguments.
curl -H "Content-type: application/json" --data "{\"#type\": \"ActionCard\",\"title\": \"New Lab %2 deployed successfully\", \"text\": \"Status is %3\"}" %1
The above command worked just fine. But doesnt satisfy my exact requirement which is described above.
I also created a seperate json file which was called as below and this worked fine but couldnt make use of command line arguments to format the JSON values.
curl --data #message.json webhook_url
message.json is as below
{
"summary":"New Lab deployed",
"sections":[
{
"activityTitle":"A <b>new lab</b> has been added!"
},
{
"title":"Details:",
"facts":[
{
"name":"Lab Name",
"value":"REPLACE"
},
{
"name":"Status",
"value":"REPLACE"
}
]
}
]
}
cURL is able to read data from file:
--data-binary "#message.json"
do not forget prepend AT sign to identify the doublequoted string is a filename, not a data itself.
Related
I'm building a Slack bot that handles slash commands.
I'm following the Slack documentation https://api.slack.com/interactivity/handling#message_responses
and manage to get proper payload sent with the command activation, i.e
token=TTT
apiAppId=AAA
teamId=MMM
teamDomain=acme
enterpriseId=null
enterpriseName=null
channelId=CCC
channelName=chan
userId=UUU
userName=Mugen
command=/cmd
text=my-text
responseUrl=https://hooks.slack.com/commands/XXX/YYY/ZZZ
triggerId=a.b.c
isEnterpriseInstall=false
To which I immediately respond with some text.
Then when I POST a delayed response using responseUrl, I get my response posted fine, however, the original message is not modified or deleted. Even if I post it again and again to same url.
curl --location --request POST 'https://hooks.slack.com/commands/XXX/YYY/ZZZ' \
--header 'Content-type: application/json; charset=utf-8' \
--data-raw '{
"response_type": "ephemeral",
"replace_original": true,
"delete_original": true,
"text": "txt"
}'
I tried with all sorts of combinations on the two booleans, nothing worked. Did I miss something?
I'm using the Java sdk to handle the command, posted here with simplified terms to show its unrelated to the Java code.
Seems that delete/replace of message is simply not supported for command responses. Only for action responses.
This isn't evident in the documentation, but is strongly suggested by the sdk objects.
I'm writing a bash script to interface with my ecobee (query info and change settings). I have the authorization all worked out (access token and refresh token) and am now trying to request info from the ecobee. This json parameter list is dynamically created. None of the curl examples in the Developers API Doc seem to work.
I've tried assigning the json to a variable (?json="$selection") and to a file (?json=#"$location"). My latest attempt (hard coding the json and escaping the braces) of the curl is as follows:
response=$(curl -s "https://api.ecobee.com/1/thermostat?json=\{"selection":\{"selectionType":"registered","selectionMatch":null,"includeRuntime":true,"includeSettings":true/}/}" -H "Content-Type: application/json;charset=UTF-8" -H "Authorization: Bearer $access_token")
and I received a null response declare -- response=""
If I have curl read from a file:
{"selection":{"selectionType":"registered","selectionMatch":null,"includeRuntime":true,"includeSettings":true}}
then I receive:
response='{
"status": {
"code": 4,
"message": "Serialization error. Malformed json. Check your request and parameters are valid."
}
}'
Which I assuming it' an escape issue?
Can anyone offer any insight? Again, I thought this would be the easy part. I'm using jq to build my request json. Other alternatives to curl that can better deal with json? I'm limited to bash (which is what I know)
Thanks
To integrate (arbitrary) data into a URL (or URI in general) you need to prepare it using percent-encoding first.
As you have mentioned to use jq to compose that data, you could add #uri to your jq filter to perform the encoding, and use the --raw-output (or -r) option to then output it properly.
For example, jq -r '#uri' applied to your JSON
{
"selection": {
"selectionType": "registered",
"selectionMatch": null,
"includeRuntime": true,
"includeSettings": true
}
}
would output
%7B%22selection%22%3A%7B%22selectionType%22%3A%22registered%22%2C%22selectionMatch%22%3Anull%2C%22includeRuntime%22%3Atrue%2C%22includeSettings%22%3Atrue%7D%7D
which you can use within the query string ?json=….
I am trying to upload a zip file to Google drive account using curl.
The file is uploaded successfully but the filename is not getting updated. It gets uploaded with default filename i.e. "Untitled".
I am using below command.
curl -k -H "Authorization: Bearer cat /tmp/token.txt" -F "metadata={name : 'backup.zip'} --data-binary "#backup.zip" https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart
You can use Drive API v3 to upload the zip file. The modified curl code is as follows.
curl -X POST -L \
-H "Authorization: Bearer `cat /tmp/token.txt`" \
-F "metadata={name : 'backup.zip'};type=application/json;charset=UTF-8" \
-F "file=#backup.zip;type=application/zip" \
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
In order to use this, please include https://www.googleapis.com/auth/drive in the scope.
The answer above works fine and was the command I used in uploading my file to Google Drive using Curl. However, I didn't understand what scope was and all of the initial setup required to make this command work. Hence, for documentation purposes. I'll give a second answer.
Valid as at the time of writing...
Visit the Credentials page and create a new credential (this is assuming you have created a project). I created credentials for TVs and Limited devices, so the work flow was similar to:
Create credentials > OAuth client ID > Application Type > TVs and Limited Input devices > Named the client > Clicked Create.
After doing this, I was able to copy the Client ID and Client Secret when viewing the newly created credential.
NB: Only the variables with double asterisk from the Curl commands should be replaced.
Next step was to run the Curl command:
curl -d "client_id=**client_id**&scope=**scope**" https://oauth2.googleapis.com/device/code
Scope in this situation can be considered to be the kind of access you intend to have with the credential having the inputted client_id. More about scope from the docs For the use case in focus, which is to upload files, the scope chosen was https://www.googleapis.com/auth/drive.file.
On running the curl command above, you'll get a response similar to:
{ "device_code": "XXXXXXXXXXXXX", "user_code": "ABCD-EFGH",
"expires_in": 1800, "interval": 5, "verification_url":
"https://www.google.com/device" }
Next step is to visit the verification_url in the response in your browser, provide the user_code and accept requests for permissions. You will be presented with a code when all prompts have been followed, this code wasn't required for the remaining steps (but there may be some reasons to use it for other use cases).
Next step is to use the Curl command:
curl -d client_id=**client_id** -d client_secret=**client_secret** -d device_code=**device_code** -d grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code https://accounts.google.com/o/oauth2/token
You will get a response similar to:
{ "access_token": "XXXXXXXXX", "expires_in": 3599,
"refresh_token": "XXXXXXXXX", "scope":
"https://www.googleapis.com/auth/drive.file", "token_type": "Bearer"
}
Now you can use the access token and follow the accepted answer with a Curl command similar to:
curl -X POST -L \
-H "Authorization: Bearer **access_token**" \
-F "metadata={name : 'backup.zip'};type=application/json;charset=UTF-8" \
-F "file=#backup.zip;type=application/zip" \
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
I want to terminate invokhttp processor as soon as it fails, for that I use ExecuteStreamCommand processor I have made bat file with code like this:
curl http://localhost:8080/nifi-api/controller/process-groups/root/processors/f511a6a1-015d-1000-970e-969eac1e6fc5'-X PUT -H 'Accept: application/json'-d #stop.json -vv
and I have related json file with code like this:
{
"status": {
"runStatus": "STOPPED"
},
"component": {
"state": "STOPPED",
"id": "f511a6a1-015d-1000-970e-969eac1e6fc5"
},
"id": "f511a6a1-015d-1000-970e-969eac1e6fc5",
"revision": {
"version": 30,
"clientId": "0343f0b9-015e-1000-7cd8-570f8953ec11"
}
}
I use my jso file as an argument for command inside ExecuteStreamCommand processor bat it throws an exception like this:
What should I change?
all actions in nifi that you can do through web browser you can do through nifi-api.
use google chrome you can press F12 to activate DevTools
(other browsers also has this option)
then select Network tab
do required action on nifi (for example stop the processor)
right-click the request and choose menu copy -> copy as cUrl (bash)
now you have curl command in clipboard that repeats the same nifi action through calling nifi-api
you can remove all headers parameters (-H) except one: -H 'Content-Type: application/json'
so the stop action for my processor will look like this:
curl 'http://localhost:8080/nifi-api/processors/d03bbf8b-015d-1000-f7d6-2f949d44cb7f' -X PUT -H 'Content-Type: application/json' --data-binary '{"revision":{"clientId":"09dbb50e-015e-1000-787b-058ed0938d0e","version":1},"component":{"id":"d03bbf8b-015d-1000-f7d6-2f949d44cb7f","state":"STOPPED"}}'
beware! every time you change processor (even state) its version changes.
so before sending stop request you have to get current version & state.
you have to sent GET request to the same url as above without any additional headers:
http://localhost:8080/nifi-api/processors/d03bbf8b-015d-1000-f7d6-2f949d44cb7f
where d03bbf8b-015d-1000-f7d6-2f949d44cb7f is id of your processor.
you can just try this url in browser but replace the processor id in it.
the response will be in json.
{"revision":
{"clientId":"09dbb50e-015e-1000-787b-058ed0938d0e","version":4},
"id":"d03bbf8b-015d-1000-f7d6-2f949d44cb7f",
"uri":
...a lot of information here about this processor...
}
you can take clientId and version from result and use those attributes to build correct STOP request.
PS:
ExecuteStreamCommand transfers flow file into executing command as an input stream that could cause problems
use ExecuteProcess because you passing all the parameters to curl in command line and not through input stream.
you can stop the nifi processor without using curl - you just need to build correct sequence of processors like this:
InvokeHTTP (get current state) -> EvaluateJsonPath (extract version and clientId) -> ReplaceText (build json for stop using attrs from prev step) -> InvokeHTTP (call stop)
try to avoid the logic of stopping processor from nifi - sure it's possible. just re-think your algorithm.
here is template which show how to stop invokehttp processor :
https://www.dropbox.com/s/uv14kuvk2evy9an/StopInvokeHttpPoceesor.xml?dl=0
Hi I am using cURL command to upload a file which is a POST request to my local machine service.
I am using following commands to upload
curl -i -X POST -H "Content-Type: multipart/form-data" -F
"/Users/myName/Folder/file.csv" http://localhost:port/api/fileupload
In my application side I am using spring frameworks web binding to receive the file
Following is the code snippet
public ResponseEntity importDimensions(#RequestBody MultipartFile file) {
// file is variable is always null
}
What am I missing here?
You need an # sign before the filename, like this: #/Users/myName/Folder/file.csv.
And if your server-side code is expecting a parameter named file then you need to do this:
-F "file=#/Users/myName/Folder/file.csv"