I am using Python for sending message to a telegram group, the URL returns 404. Below is the code:
import requests
from config import API, CHAT_ID
# telegram url
url = "https://api.telegram.org/bot{}".format(API)
print(url)
def send_mess(text):
params = {'chat_id': CHAT_ID, 'text': text}
response = requests.post(url + 'sendMessage', data=params, timeout=20)
return response
if __name__ == '__main__':
d = send_mess('Hi')
print(d)
it looks like the API or the CHAT_ID are not well configured. Even though, I would like to suggest using the telegram library:
pip install python-telegram-bot
import telegram
def send_mess(text):
token = "XXXXXX"
chat_id = "XXXXXX"
bot = telegram.Bot(token=token)
bot.sendMessage(chat_id=chat_id, text=text)
if __name__ == '__main__':
d = send_mess('Hi')
print(d)
There's a simple typo in your code, lets take a look at the following lines:
url = "https://api.telegram.org/bot{}".format(API)
...
response = requests.post(url + 'sendMessage', data=params, timeout=20)
This wel produce an url like;
https://api.telegram.org/bot123456:QWERTYsendMessage
Here, the url is missing an / between the token and the method, would recommend changing;
url + 'sendMessage' to url + '/sendMessage' to get the correct url:
https://api.telegram.org/bot123456:QWERTY/sendMessage
^
Then your code send the message as expected.
Related
I am trying to fill html form and get the intended result as i get when i fill manually. But I fail.
I am trying to fill the site https://www.desco.org.bd/ebill/login.php with value 32000001. So far my try is as below-
import requests
#LOGIN_URL = 'https://www.desco.org.bd/ebill/login.php'
#LOGIN_URL = 'https://www.desco.org.bd/ebill/authentication.php'
LOGIN_URL = 'https://www.desco.org.bd/ebill/billinformation.php'
payload = {
'username': '32000001',
'login':'Login',
'login':'true'
}
with requests.Session() as s:
p = s.post(LOGIN_URL, data=payload)#, verify=False)
# print the html returned or something more intelligent to see if it's a successful login page.
print (p.text)
I have found that login.php redirects to authentication.php and it further redirects to billinformation.php which delivers the true data i needed.
Thanks in advance.
N.B. I am not planning to use selenium since it is too slow for my case i.e. collect huge data from this site.
i am working for similar case, may be you would try using websockets:
import websockets
def process_function(message):
# process the message
def server(ws:str, path:int):
while True:
message_received = await ws.recv() # receive from ui
print(f'Msg [{message_received}]')
message_to_send = process_function(message)
await ws.send(message_to_send) # send feedback to ui
server = websockets.serve(server, '127.0.0.1', 5678) # set the html to run in the same ip:port
another try:
import json, requests
def do_auth(url):
headers = {"Content-Type": "application/json", "Accept":'*/*'}
body = json.dumps({'username': 'user', 'password': 'pass'})
r = requests.post(url=url, headers=headers, data=body, verify=False)
print(r.status_code);
d = json.loads(r.text);
print(d['access_token']);
print(d['refresh_token'])
return d['access_token'], d['refresh_token']
do_auth(url_auth) # authorization
requests.get(url_content, headers=headers, verify=False) # get data
Can someone help me to make a linktervise bypass bot for discord.py I have this code that I found on github but I can't understand it
import requests
def bypass(url):
payload = {
"url": url,
}
r = requests.post("https://api.bypass.vip/", data=payload)
return r.json()
if __name__ == '__main__':
result = bypass("https://...") # include url to bypass
print(result)
You have to try and make it yourself, the people here won't spoonfeed you.
You could make a command that uses
def bypass(url):
payload = {
"url": url,
}
r = requests.post("https://api.bypass.vip/", data=payload)
return r.json()
and then check what the value is that it returns and send that back to the user
How do you use Bitbucket's 2.0 API to decline a pull request via Python?
According to their documentaion, it should be something like:
import requests
kwargs = {
'username': MY_BITBUCKET_ACCOUNT,
'repo_slug': MY_BITBUCKET_REPO,
'pull_request_id': pull_request_id
}
url = 'https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests/{pull_request_id}/decline'.format(**kwargs)
headers = {'Content-Type': 'application/json'}
response = requests.post(url, auth=(USERNAME, PASSWORD), headers=headers)
However, this fails with response.text simply saying "Bad Request".
This similar code works for me with their other API endpoints, so I'm not sure why the decline method is failing.
What am I doing wrong?
You have to authenticate with Oath. I wrote a wrapper for making these requests. Here is a simple example that works. The only thing I couldn't figure out was how to add a reason it was declined. I ended up making a request before I declined the PR that added a comment on why it was declined.
import os
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session
class Bitbucket(object):
def __init__(self, client_id, client_secret, workplace, repo_slug):
self.workplace = workplace # username or company username
self.repo_slug = repo_slug
self.token_url = 'https://bitbucket.org/site/oauth2/access_token'
self.api_url = 'https://api.bitbucket.org/2.0/'
self.max_pages = 10
self.client = BackendApplicationClient(client_id=client_id)
self.oauth = OAuth2Session(client=self.client)
self.oauth.fetch_token(
token_url=self.token_url,
client_id=client_id,
client_secret=client_secret
)
def get_api_url(self, endpoint):
return f'{self.api_url}repositories/{self.workplace}/{self.repo_slug}/{endpoint}'
bitbucket = Bitbucket(os.environ['BITBUCKET_KEY'], os.environ['BITBUCKET_SECRET'], workplace='foo', repo_slug='bar')
pr_id = 1234
resp = bitbucket.oauth.post(f"{bitbucket.get_api_url('pullrequests')}/{pr_id}/decline")
if resp.status_code == 200:
print('Declined')
else:
print('Someting went wrong.')
I'm using Python 2.7.10 64-bit.
In the update_jira_field method, I'm getting the following error:
TypeError: post() takes at least 1 argument (1 given)
I tried also requests.put(), combination of json = payload while declaring the payload as a json, but still got the same error.
I'm not sure what am I doing wrong, never experienced this error while using the requests module.
import requests
import json
import urllib2
auth = *****
propertKey = 'customfield_13557'
headers = {'Accept':'application/json','Bearer':****'}
def get_jira_real_id(jiraKey):
endpoint = 'https://****.atlassian.net/rest/api/3/issue/{0}'.format(jiraKey)
response = requests.get(endpoint, headers = headers, auth = auth)
if response.status_code == 200:
print "Success getting Jira Id"
response = json.loads(response.text)
return response['id']
def update_jira_field(jiraId,jiraKey):
endpoint = 'https://****.atlassian.net/rest/api/3/issue/{0}'.format(jiraId)
payload = dict({"fields": {"customfield_13557":{"self": "https://****.atlassian.net/rest/api/3/customFieldOption/14915", "value": "Yes", "id": "14915"}}})
response = requests.post(endpoint = endpoint, headers = headers, auth = auth, data = payload)
if response.status_code == 200:
print "Success! Updated", jiraId, jiraKey
jiraList = ['****']
for jiraKey in jiraList:
jiraId = get_jira_real_id(jiraKey)
update_jira_field(jiraId, jiraKey)
print "Done Done Done"
Any idea why I get this error? and how do I fix it?
You try to pass in a named parameter named endpoint, but the correct name is url. It whould work if you change the line to
response = requests.post(endpoint, headers = headers, auth = auth, data = payload)
import requests
req = requests.post('http://example.in/phppage.php', data = {'device_id':220,'tank_type':1,'level_pct':78,'water_flow':0,'water_over_flow':0})
print("HTTP Connection Status::"+str(req.status_code))
When I run this code it shows http connection status 404. What is wrong?
# importing the requests library
import requests
# defining the api-endpoint
API_ENDPOINT = "http://pastebin.com/api/api_post.php"
# your API key here
API_KEY = "XXXXXXXXXXXXXXXXX"
# your source code here
source_code = '''
print("Hello, world!")
a = 1
b = 2
print(a + b)
'''
# data to be sent to api
data = {'api_dev_key':API_KEY,
'api_option':'paste',
'api_paste_code':source_code,
'api_paste_format':'python'}
# sending post request and saving response as response object
r = requests.post(url = API_ENDPOINT, data = data)
# extracting response text `enter code here`
pastebin_url = r.text
print("The pastebin URL is:%s"%pastebin_url)