How to send a file using POST method to TelegramBot? - python

I want to send a file with the post method, but I don't know what's wrong with my code
I have chat_id, file_id, and every requirement parameters
this is a sample code for sending voice via POST Request
import requests
my_data = {'chat_id': '72600457' ,'file_id': 'AwADBAADPAYAAvFWCVFZFfPyZdGLfhYE'}
my_url = 'https://api.telegram.org/bot<MY TOKEN>/sendVoice'
request.post(url=my_url, data=my_data)
when I run the code, no error happens. But nothing is shown from the bot;
This file_id works with GET METHOD and I could send text with POST METHOD, But for files it seems it doesn't work.

Check documentation for sendVoice - it doesn't use name file_id but voice
data = {'chat_id': '72600457', 'voice': 'AwADBAADPAYAAvFWCVFZFfPyZdGLfhYE'}
If you use file ID then you can use POST but also GET
And you should get response from server to see information about wrong request
import requests
token = '<MY TOKEN>'
data = {'chat_id': '72600457', 'voice': 'AwADBAADPAYAAvFWCVFZFfPyZdGLfhYE'}
url = f'https://api.telegram.org/bot{token}/sendVoice'
#response = requests.post(url, data=data)
response = requests.get(url, params=data)
print(response.json())
By the way: there is module python-telegram-bot. GitHub: python-telegram-bot

Related

Python3 flask receive file and send it to another api

I am trying to receive a file in python flask endpoint via post request, and then send it to another endpoint. I receive a file on my API but I do not know how to wrap it for the next request.
The code looks like this
#app.route('/file', methods=['POST'])
def get_task_file():
response_obj = {}
if 'file' not in request.files:
abort(400)
print(request.files["file"].filename)
# TEXT EXTRACT
url1 = 'http://localhost:5001/text/api'
headers = {'Content-type': 'multipart/form-data'}
print(type(request.files['file']))
r1 = requests.request("POST", url1, files={'file':request.files['file']}, headers=headers)
print(r1.text)
Right now the file type is <class 'werkzeug.datastructures.FileStorage'> and I am getting 400 errors. I just want to add that the logic on the second API is similar, should I change it?
I got code:400 as return or it shows the file I upload is empty, I found requests in flask is a little bit tricky.
change this
r1 = requests.request("POST", url1, files={'file':request.files['file']}, headers=headers)
into:
file = request.files['file']
r1 = requests.post(url1, files={'file':(file.filename, file.stream, file.content_type, file.headers)})

Requests.Get not working for API call - Yielding 401 Error and "Not Logged In" - Second Call

I have successfully connected to the target server using requests.post; however, when I try to make any request.get calls I am receiving the following error:
{"message":"Not logged in","code":401}
I have confirmed I am logging in successfully, as when I print(response.text) I am showing info from the server. Based on the error message, it appears that I am not persisting the login.
My script:
import requests
data = {}
payload = {'j_username': 'my_username', 'j_password': 'my_password'}
response = requests.post('https://sample.com', data=payload)
getBlocks_response = requests.get('https://sample.com/api/v1/resourceBlock')
print(getBlocks_response.text)
getDataDateRange_response = requests.get('https://sample.com/api/v1/factData/getDataDateRange', data=payload)
print(getDataDateRange_response.text)
I am expecting an output of data in the form of a JSON, but have only received two errors for both requests.get calls above. I am new to Python as well as API interaction. Any insight would be greatly appreciated. Thanks.
I read the documentation regarding Session(). I was able to get my get requests to run successfully by adjusting my script to:
import requests
with requests.Session() as session:
data = {}
payload = {'j_username': 'my_username', 'j_password': 'my_password'}
response = session.post('https://sample.com', data=payload)
getBlocks_response = session.get('https://sample.com/api/v1/resourceBlock')
print(getBlocks_response.text)
getDataDateRange_response = session.get('https://sample.com/api/v1/factData/getDataDateRange', data=payload)
print(getDataDateRange_response.text)

How to call an API using Python Requests library

I can't figure out how to call this api correctly using python urllib or requests.
Let me give you the code I have now:
import requests
url = "http://api.cortical.io:80/rest/expressions/similar_terms?retina_name=en_associative&start_index=0&max_results=1&sparsity=1.0&get_fingerprint=false"
params = {"positions":[0,6,7,29]}
headers = { "api-key" : key,
"Content-Type" : "application/json"}
# Make a get request with the parameters.
response = requests.get(url, params=params, headers=headers)
# Print the content of the response
print(response.content)
I've even added in the rest of the parameters to the params variable:
url = 'http://api.cortical.io:80/rest/expressions/similar_terms?'
params = {
"retina_name":"en_associative",
"start_index":0,
"max_results":1,
"sparsity":1.0,
"get_fingerprint":False,
"positions":[0,6,7,29]}
I get this message back:
An internal server error has been logged # Sun Apr 01 00:03:02 UTC
2018
So I'm not sure what I'm doing wrong. You can test out their api here, but even with testing I can't figure it out. If I go out to http://api.cortical.io/, click on the Expression tab, click on the POST /expressions/similar_terms option then paste {"positions":[0,6,7,29]} in the body textbox and hit the button, it'll give you a valid response, so nothing is wrong with their API.
I don't know what I'm doing wrong. can you help me?
The problem is that you're mixing query string parameters and post data in your params dictionary.
Instead, you should use the params parameter for your query string data, and the json parameter (since the content type is json) for your post body data.
When using the json parameter, the Content-Type header is set to 'application/json' by default. Also, when the response is json you can use the .json() method to get a dictionary.
An example,
import requests
url = 'http://api.cortical.io:80/rest/expressions/similar_terms?'
params = {
"retina_name":"en_associative",
"start_index":0,
"max_results":1,
"sparsity":1.0,
"get_fingerprint":False
}
data = {"positions":[0,6,7,29]}
r = requests.post(url, params=params, json=data)
print(r.status_code)
print(r.json())
200
[{'term': 'headphones', 'df': 8.991197733061748e-05, 'score': 4.0, 'pos_types': ['NOUN'], 'fingerprint': {'positions': []}}]
So, I can't speak to why there's a server error in a third-party API, but I followed your suggestion to try using the API UI directly, and noticed you're using a totally different endpoint than the one you're trying to call in your code. In your code you GET from http://api.cortical.io:80/rest/expressions/similar_terms but in the UI you POST to http://api.cortical.io/rest/expressions/similar_terms/bulk. It's apples and oranges.
Calling the endpoint you mention in the UI call works for me, using the following variation on your code, which requires using requests.post, and as was also pointed out by t.m. adam, the json parameter for the payload, which also needs to be wrapped in a list:
import requests
url = "http://api.cortical.io/rest/expressions/similar_terms/bulk?retina_name=en_associative&start_index=0&max_results=1&sparsity=1.0&get_fingerprint=false"
params = [{"positions":[0,6,7,29]}]
headers = { "api-key" : key,
"Content-Type" : "application/json"}
# Make a get request with the parameters.
response = requests.post(url, json=params, headers=headers)
# Print the content of the response
print(response.content)
Gives:
b'[[{"term":"headphones","df":8.991197733061748E-5,"score":4.0,"pos_types":["NOUN"],"fingerprint":{"positions":[]}}]]'

How to use an auth token and submit data using Python requests.POST?

Using WheniWork's api, I need to use a token for authentication, and I also need to send data to create a new user. Does the order or name of arguments I send with requests.post() matter?
If I'm just using GET to pull information, I can have the url contain the thing I'm looking for, and then send a payload that is the token. For example:
url = 'https://api.wheniwork.com/2/users/2450964'
payload = {"W-Token": "ilovemyboss"}
r = requests.get(url, params=payload)
print r.text
When I try to add a new user however, I'm either not able to authenticate or not passing the data correctly. The api reference shows this format for using cURL:
curl https://api.wheniwork.com/2/users --data '{"first_name":"FirstName", "last_name": "LastName", "email": "user#email.com"}' -H "W-Token: ilovemyboss"
Here's what I've written out in python (2.7.10) using Requests:
url = 'https://api.wheniwork.com/2/users'
data={'first_name':'TestFirst', 'last_name': 'TestLast','email':'test#aol.com'}
params={"W-Token": "ilovemyboss"}
r = requests.post(url, data=data, params=params)
print r.text
Can someone explain if/how data(the user) gets sent separately from authentication(the token)?
I found the issue!
The data (user dict) needs to be in quotes. I'm not sure if their API is expecting a string, or if that's how requests works, or what. But here's the solution:
url = 'https://api.wheniwork.com/2/users'
data = "{'first_name':'TestFirst', 'last_name': 'TestLast','email':'test#aol.com'}"
params = {"W-Token": "ilovemyboss"}
r = requests.post(url, data=data, params=params)
print r.text
We can solve the above problem by converting the data dictionary to JSON string by using json.dumps.
data={'first_name':'TestFirst', 'last_name': 'TestLast','email':'test#aol.com'}
r = requests.post(url, data=json.dumps(data), params=params)
print r.text

Python - Using requests sends post message

I used Postman to send a raw request to Jetstar website to get the flight details. And I wanted to use python script to do the same thing using requests library, but I cannot get back the correct response.
Here what I have done in Postman:
And a simple script I used to send post request:
import requests
files = {'file': open('PostContent.txt', 'rb')}
if __name__ == "__name__"):
url = "http://www.jetstar.com/"
r = requests.post(url, files = files)
print(r.text)
When I run the python script, I always get the welcome page not flight details. I am not sure what is error?
Note: The PostContent.txt contains the form-data in raw text when I search for flights.
I used Chrome Dev Tool to capture the POST request when I search for a specific flight date. And it is the Form Data in the Headers.
Try using a dictionary instead of a FILE. The FILE is supposed to be for posting a FILE, not a FORM-ENCODED post, which is probably what the site expects.
payload = {
'DropDownListCurrency': 'SGD'
}
r = requests.post("http://httpbin.org/post", data=payload)
You use a key file which is wrong for this type of request. Also your sample code isn't working! Just paste working code here...
import requests
import logging
logging.basicConfig(level=logging.DEBUG)
payload = {"__EVENTTARGET":"",
"__EVENTARGUMENT":"",
"__VIEWSTATE":"/wEPDwUBMGQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgEFJ01lbWJlckxvZ2luU2VhcmNoVmlldyRtZW1iZXJfUmVtZW1iZXJtZSDCMtVG/1lYc7dy4fVekQjBMvD5",
"pageToken":"",
"total_price":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$RadioButtonMarketStructure":"RoundTrip",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin1":"Nadi (NAN)",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination1":"Melbourne (Tullamarine) (MEL)",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate1":"14/01/2015",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate1":"16/02/2015",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListCurrency":"AUD",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin2":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination2":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate2":"16/02/2015",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate2":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin3":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination3":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate3":"27/12/2014",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate3":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin4":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination4":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate4":"03/01/2015",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate4":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin5":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination5":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate5":"10/01/2015",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate5":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin6":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination6":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate6":"17/01/2015",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate6":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_ADT":1,
"ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_CHD":0,
"ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_INFANT":0,
"ControlGroupSearchView$AvailabilitySearchInputSearchView$RadioButtonSearchBy":"SearchStandard",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMultiCityOrigin1":"Origin",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMultiCityDestination1":"Destination",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureMultiDate1":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMultiCityOrigin2":"Origin",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMultiCityDestination2":"Destination",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureMultiDate2":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMultiPassengerType_ADT":1,
"ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMultiPassengerType_CHD":0,
"ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMultiPassengerType_INFANT":0,
"ControlGroupSearchView$AvailabilitySearchInputSearchView$numberTrips":2,
"ControlGroupSearchView$AvailabilitySearchInputSearchView$ButtonSubmit":""}
if __name__ == "__main__":
url = "http://booknow.jetstar.com/Search.aspx"
r = requests.post(url, data=payload)
print(r.text)

Categories

Resources