How can send a url to telegram bot? - bash

I write a code to send a proxy link to my telegram bot but when i send the request
my proxy link don't send complete
this is my code
#/bin/bash
TOKEN="***********************"
ID="************"
URL="https://api.telegram.org/bot$TOKEN/sendMessage"
PRLINK="tg://proxy?server=xxx.xxx.xxx.xxx&port=443&secret=eeb000000000000000000000000000"
curl -s -X POST $URL -d chat_id=$ID -d text=${PRLINK} > /root/sendResult
and this response
{ [..] ,"text":"tg://proxy?server=18.202.178.109", [..]}
in response
"text":"tg://proxy?server=18.202.178.109" no equal to PRLINK that me send with curl

The problem is related to URL encoding: https://www.w3schools.com/tags/ref_urlencode.ASP
In a nutshell, you should encode the ampersand character in order to let the entire URL be considered properly.
So that, the ampersand character is encoded as %26, therefore:
Your code should be:
#/bin/bash
TOKEN="***********************"
ID="************"
URL="https://api.telegram.org/bot$TOKEN/sendMessage"
PRLINK="tg://proxy?server=xxx.xxx.xxx.xxx%26port=443%26secret=eeb000000000000000000000000000"
curl -s -X POST $URL -d chat_id=$ID -d text=${PRLINK} > /root/sendResult

Related

Flock webhook token

I am trying to create webhook as per this document and this doesn't include any clue about where does the token comes from.
https://docs.flock.com/display/flockos/Create+An+Incoming+Webhook
My curl command as below
curl -X POST -H "Content-Type: application/json" https://api.flock.com/hooks/sendMessage/guid-guid -d '{"text": "This is a test message.","token":"test"}'
Error message:
{"error":"InvalidParameter","description":"A required parameter for the method call is missing or invalid","parameter":"token"}
Can someone point me what's missing here.
Flock gives you the token for the webhook when you finish adding a new one at https://dev.flock.com/webhooks
You can look it up again when you're done by going to the edit option for the webhook you've added; at the moment the token is given at the bottom of the page:
Webhook URL
Send your JSON payload to this URL
[your-token-here]

How can one use CURL to send a shell script as the POST body to Tornado?

It is unclear to me how Tornado expects to parse body arguments for all content types. It makes sense for JSON, and maybe form data, but for other body content types it is unclear to me how to format the request with CURL to get it to work.
Question: How can one use CURL to send a shell script as the POST body to Tornado? What is the formatting for the body arguments?
What I've tried:
curl -X POST -H "Content-Type: application/x-sh" http://localhost:8888/ -d script='#!/bin/bash \necho "scale=500\; 4*a(1)" | bc -l'
What I would hope or expect this to do is for Tornado to process the HTTP body so that (1) the body argument is script, and (2) the value corresponding to it is the shell script to compute pi:
#!/bin/bash
echo "scale=500\; 4*a(1)" | bc -l
Instead I just get a 400: Bad Request error, and my debugging logs show that the value of self.request.body_arguments is an empty dictionary {}.
You're sending POST data with this header - Content-Type: application/x-sh. But Tornado only supports application/x-www-form-urlencoded to parse submitted form/POST data.
Just submit the data with Content-Type: application/x-www-form-urlencoded and Tornado will make the submitted data available in body_arguments.

curl error 18 transfer closed with outstanding read data remaining

Setup
I'm Using curl in the following bash script to push a JSON file to a REST API running in tomcat sitting behind nginx.
while IFS= read -d '' -r file; do
base=$(basename "$file")
datetime=$(find $file -maxdepth 0 -printf "%TY/%Tm/%Td %TH:%TM:%.2TS")
curl -vX POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" \
-d #"$file" -u vangeeij:eian12 \
"http://192.168.105.10/homeaccess/services/aCStats/uploadData?username=vangeeij&filename=$base&datetime=$datetime"
#sudo mv "$file" /home/vangeeij/acserver/resultsOld
done < <(sudo find . -type f -print0)
Problem
When running this script I get a http 400 response with curl error:
curl: (18) transfer closed with outstanding read data remaining
What I have tried
I have found 2 things. First running the same URL and body through Postman yields a successful POST.
I found that this error goes away when the last parameter is removed from the URL &datetime=$datetime
I have also found a few connections between this error and setting a curl option something like
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
But I'm not sure where/how to set this exactly when using curl in a simple bash script
Question
What do I need to change in my curl command to get rid of the error and still be able to use all parameters?
UPDATE
Starting a new question, as further investigation has lead me to a better understanding of the problem.
New Question Link
The error has to do with the fact that the parameter datetime= ends up with text in it that needs to be URL encoded.
This was confirmed by replacing the variable with 2017%2F03%2F01%2008%3A50%3A56
and it worked.
So now the problem is, that I can't get --data-urlencode datetime=$datetime to work. It seems this just gets appended to the JSON data or something.
This error is being generated by the fact that the datetime= paramater is being passed in with non encoded non URL friendly characters... (eg. space).
The fix to this would be to find a way to convert the $datetime to a URLEncoded String.
eg. convert:
2017/03/01 08:50:56
TO
2017%2F03%2F01%2008%3A50%3A56
See the following discussion for one method to accomplish this.
Post JSON data to Rest with URLEncoded query paramaters

what does `curl -e` or `curl --referer` mean?

I've seen some example :-
curl --referer http://example.com/bot.html http://www.cyberciti.biz/
what is the different if I use curl http://www.cyberciti.biz/
shell script type:-
curl -e http://example.com/bot.html \
'http://www.cyberciti.biz/'
what is \ for?
From the man page of cURL
-e, --referer <URL>
(HTTP) Sends the "Referrer Page" information to the HTTP server. This
can also be set with the -H, --header flag of course. When used with
-L, --location you can append ";auto" to the --referer URL to make curl automatically set the previous URL when it follows a Location:
header. The ";auto" string can be used alone, even if you don't set an
initial --referer.
Essentially this tells the server which page sent you there.

Bash curl POST a binary variable

How do you POST a binary variable in curl bash?
#!/usr/bin/env bash
IMAGE=$(curl "http://www.google.com/images/srpr/logo3w.png")
curl --data-binary "$IMAGE" --request "POST" "http://www.somesite.com"
Curl seems to do corrupt the image when uploading.
Curl has the option to write response to disk and then read from it, but it'd be more efficient to do it solely in memory.
Try to eliminate the variable ... as follows:
curl "http://www.google.com/images/srpr/logo3w.png" | curl --data-binary - --request "POST" "http://www.somesite.com"
From the curl man page:
If you start the data with the letter #, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin.
EDIT: From the man page, too:
--raw When used, it disables all internal HTTP decoding of content or transfer encodings and instead makes them passed on unaltered, raw. (Added in 7.16.2)
What happens, if applied on either or both sides?
I had a related problem, where I wanted to dynamically curl a file from a given folder.
curl --data-binary directory/$file --request "POST" "http://www.somesite.com"
did not work - uploaded the string "directory/myFile.jar" instead of the actual file.
Adding the # symbol
curl --data-binary #directory/$file --request "POST" "http://www.somesite.com" fixed it.

Resources