Facebook webhook making multiple calls for the same message? - python

I made and echo bot on AWS with Python and serverless.
I keep getting the same request again and again.
I read the faq where it says u have to deliver a status code 200 else it will keep retrying the webhook.
I'm not sure How I do this.
I have noticed that the sequence number is always the same for the calls so I assume the reply I sent was not acknowledged.
my code is here
import os
import json
import requests
import random
from datetime import datetime
######################
# helper functions
######################
##recursively look/return for an item in dict given key
def find_item(obj, key):
item = None
if key in obj: return obj[key]
for k, v in obj.items():
if isinstance(v,dict):
item = find_item(v, key)
if item is not None:
return item
##recursivley check for items in a dict given key
def keys_exist(obj, keys):
for key in keys:
if find_item(obj, key) is None:
return(False)
return(True)
##send txt via messenger to id
def send_message(send_id, msg_txt):
print("Send message called")
print (datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])
params = {"statusCode": 200,"access_token": os.environment['accesstoken']}
headers = {"statusCode": "200","Content-Type": "application/json"}
data = json.dumps({"statusCode": "200","recipient": {"id": send_id},
"message": {"text": msg_txt}})
r = requests.post("https://graph.facebook.com/v2.9/me/messages", params=params, headers=headers, data=data)
print (r.text)
if r.status_code != 200:
print(r.status_code)
print(r.text)
#-----------------------------------------------------------
def hello(event, context):
#debug
event=json.loads(json.dumps(event))
print("event:" )
print(event)
# print("context")
# print(context)
#handle webhook challenge
try:
if keys_exist(event, ["queryStringParameters","hub.verify_token","hub.challenge"]):
print("subscribe to webhook invoked")
v_token = str(find_item(event, 'hub.verify_token'))
challenge = find_item(event, 'hub.challenge')
if ("strongtoken" == v_token):
response = {
"statusCode": 200,
"body": str(challenge)
}
print(challenge)
return response
#handle messaging events
if keys_exist(event, ['body']):
event_entry=json.loads(event['body'])
if ((len(event_entry['entry'])>0) & (keys_exist(event_entry['entry'][0],['messaging'])) ):
messaging_event = event_entry['entry'][0]['messaging'][0]
if (keys_exist(messaging_event,['message'])):
msg_txt = messaging_event['message']['text']
sender_id = messaging_event['sender']['id']
print(sender_id)
first_word = msg_txt.split(" ")[0]
send_message(sender_id, msg_txt)
else:
print("Did not send message")
pass
else:
print("Did not send message")
pass
else:
pass
except:
pass
I have given the status code 200 in soo many places and I'm not sure y I still keep getting the same problem.

If you are getting multiple messages your server did not return 200 status code to webhook request from Facebook server. This means an error occurred on your part otherwise 200 should be returned. It looks to me that problems are in following lines:
params = {"statusCode": 200,"access_token": os.environment['accesstoken']}
headers = {"statusCode": "200","Content-Type": "application/json"}
data = json.dumps({"statusCode": "200","recipient": {"id": send_id},
"message": {"text": msg_txt}})
Firstly you are passing statusCode in a data body of message and according to this documentation message response should not contain it.
Another problem could be sending status code inside params. I would remove status code completely from send_message method. I doubt its needed there. You are basically trying to return status 200 on wrong end. You are trying to return it on output instead of input (from Facebooks point of view).
So its quite possible you are correctly getting message from Facebook but you are still returning wrong status code because you are calling send_message method from inside messaging event and send_message method should return status "400 bad request" because you are sending faulty request. Thus your server also returns wrong response code.
Just make sure your code works correctly and 200 should be returned.
EDIT:
So i would try with following code:
params = {"access_token": os.environment['accesstoken']}
headers = {"Content-Type": "application/json"}
data = json.dumps({"recipient": {"id": send_id},
"message": {"text": msg_txt}})

I'm working on a Facebook Messenger chatbot with a Node/Express server and was having the same exact issue. The server was appropriately sending 200 responses and yet the issue persisted. I resolved the problem by caching message ID's and checking for duplicates before processing:
var NodeCache = require('node-cache');
var myCache = new NodeCache();
app.post('/webhook/', function(req, res) {
var messageID = req.body.entry[0].messaging[0].message.mid;
var checkForDupe = myCache.get(messageID);
if (checkForDupe == undefined) {
var setID = myCache.set(req.body.entry[0].messaging[0].message.mid, req.body.entry[0].messaging[0].message.mid);
//Handle message .....
I hope this helps someone. It was driving me nuts.

Your webhook callback should always return a 200 OK HTTP response when invoked by Facebook. Failing to do so may cause your webhook to be unsubscribed by the Messenger Platform. (copied from messenger docs.)
You have to search problem from your returning response. It is not with the sending data.
response = requests.post(POST_MESSAGE_URL, headers={
'Content-Type': 'application/json'
}, params={
'access_token': ACCESS_TOKEN
}, data=json.dumps(DATA_JSON)
this code working fine for me

I am from Java background but still I will try to help you.
In handle webhook challenge I can see you returning 200 status code
But in handle messaging block 200 is not returned You have set 200 status code in send message. But it will not work you have to return 200 from handle messaging block.
Also 200 should be returned even if any exception occurs in this block otherwise on exception it will get blocked. So may be in finally block you can return 200.

As an alternative, you can use chatbotproxy.com which always return 200 in 1ms and pass the exact same request to your endpoint. Since immediate response is very important for Facebook Messenger platform, 1 millisecond time is a very promising feature. If you need help to try, let me know.

Related

Python request to retrieve all requests from Postman collection

I need to print all requests from specific Postman collection. I have this code:
import requests
# Set up Postman API endpoint and authorization
postman_api_endpoint = "https://api.getpostman.com/collections"
postman_api_key = "PMAK-63b6bf724ebf902ad13d4bf2-e683c12d426716552861acda**********"
headers = {"X-Api-Key": postman_api_key}
# Get all requests from Postman collection
collection_id = "25184041-c1537769-f598-4c0e-b8ae-8cd185a79c03"
response = requests.get(f"{postman_api_endpoint}/{collection_id}/items", headers)
if response.status_code != 200:
print("Error retrieving collection:", response.text)
else:
# Print all requests
requests_data = response.json()["items"]
for request_data in requests_data:
request_method = request_data["request"]["method"]
request_url = request_data["request"]["url"]
request_headers = request_data["request"]["header"]
request_body = request_data["request"]["body"]["raw"] \
if request_data["request"]["body"]["mode"] == "raw" else ""
print(f"{request_method} {request_url}")
print("Headers:")
for header in request_headers:
print(f"{header['key']}: {header['value']}")
print("Body:")
print(request_body)
I received an error while I try to call response.text and have such massage:
Error retrieving collection: {"error":{"name":"notFound","message":"Requested resource not found"}}
Which means that I have 404 error. I have several assumptions what I did wrong:
I entered incorrect api key(But I checked several times and regenerated it twice)
I entered incorrect collection id, but in the screen below you can see where I took it and it is correct
And as I think the most likely variant I wrote incorrect request where I put my key and my collection id(I din't find any example how such requests should be like)
And of course I have requests in my collection, so error can not be because the collection is empty
Please give me some advice how I can fix this error. Thank you!
The answer is actually is really simple. I didn't know that I need to push button save request in Postman. I think if I create request in collection it will automatically save it. But I didn't, so I just saved all requests manually and finally receive correct response.

PAYPAL IPN Call responds with Status Retrying and HTTP-Code 404

I am trying to set up a notification from on a payment. Here is my code:
def PayPal_IPN():
'''This module processes PayPal Instant Payment Notification messages (IPNs).'''
# Switch as appropriate
VERIFY_URL_PROD = 'https://ipnpb.paypal.com/cgi-bin/webscr'
VERIFY_URL = VERIFY_URL_PROD
# Read and parse query string
params = request.form.to_dict()
# Add '_notify-validate' parameter
params['cmd'] = '_notify-valudate'
# Post back to PayPal for validation
headers = {'content-type': 'application/x-www-form-urlencoded',
'user-agent': 'Python-IPN-Verification-Script'}
r = requests.post(VERIFY_URL, params=params, headers=headers, verify=True)
r.raise_for_status()
# Check return message and take action as needed
if r.text == 'VERIFIED':
print("SUCCESSFULL")
elif r.text == 'INVALID':
print("FAILURE")
else:
print("NOTHING HAPPENED?")
return ""
Here is my error message:
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://ipnpb.paypal.com/cgi-bin/webscr....
Can someone help me figure out what I am not understanding? maybe the link has changed?
Here is the documentation I'm looking at https://developer.paypal.com/api/nvp-soap/ipn/IPNIntro/
I've tried changing the ipnpb to www. and still no luck.
The problem is the format of params. I'm not sure what request.form.to_dict() returns, but I do know that when you add cmd=_notify_validate it should be in this format:
params.append(('cmd', '_notify-validate'))
Also returning the appropriate format is urllib.parse.parse_qsl

Slack blocks + sending response doesn't work in python, not even when async - only in CURL

Sending block to Slack - it shows on slack correctly.
Button is clicked, I am getting the interactive event webhook.
webhook returns 200 OK to slack, not before scheduling an async POST to the response_url
the return message is posted to response_url AFTER the 200 ack is returned from the webhook function
Slack responds 404 to the response_url
that same response URL works when curled outside of python....
I don't get why slack rejects the posted return message with 404 when the very same response_url works when used in curl outside of python.
my webhook processor:
def slack_webhook(request):
json_dict = json.loads(request.body.decode('utf-8'))
token = json_dict['token'] if 'token' in json_dict else None
message = json_dict['message'] if 'message' in json_dict else None
trigger_id = json_dict['trigger_id'] if 'trigger_id' in json_dict else None
response_url = json_dict['response_url'] if 'response_url' in json_dict else None
actions = json_dict['actions'] if 'actions' in json_dict else None
for action in actions:
print(f"** received action {action['action_id']}")
print(f"** response_url: {response_url}")
print(f"** trigger_id: {trigger_id}")
payload = {
"replace_original": True,
"text": "Success!",
}
# async via Celery...
send_slack_interactive_response.delay(response_url, payload, trigger_id)
return HttpResponse(status=200)
the async celery task which sends the
#app.task(bind=True, retry_kwargs={'max_retries': 10, 'countdown': 5})
def send_slack_interactive_response(self, response_url, payload, trigger_id):
print(f"** -> response_url: {response_url}")
print(f"** -> trigger_id: {trigger_id}")
if response_url and payload and trigger_id:
headers = {"Content-Type": "application/json"}
payload['trigger_id'] = trigger_id
print(json.dumps(payload))
r = requests.post(response_url, data=payload, headers=headers)
print(r.__dict__)
This function fails with 404. however, when i take the response_url, trigger_id and create the exact same POST with command line curl - it works.
What am I doing wrong?
Just a comment on your code : You can do token = json_dict.get("token", None) that save you lot of code
On your problem :
Be sure Celery parameters is not weird encoded (response_url is send to messagerie and is encoded)
Be sure request parameters are well used (like using json instead of data...)

I dont know why I get a 400 bad request error for sending a discord webhook. Everything seems fine but I am new at this

I am new at this so it is probably a stupid mistake
Im trying to make a program that sends the price of a stock quote to discord as a webhook.
The webhook does not send. But, when I do print(symbol.json()["Global Quote"]) I get all of the information.
I think there is a problem with the "Content-Type": "application/json" but im not really sure how to fix it.
This is a snippet:
webhook = 'https://discordapp.com/api/webhooks/609119785015312414/HOkHGPWqfPfLJHpSp15XZ9dUNhUDly_DKQ8OwLgk3Rw0-4G08gx0mOrKN3FKhompb5ig'
session = requests.session()
symbol = session.get("https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=AAPL&apikey=F4RV85MVA2M2YTIY")
data = {}
data["content"] = symbol.json()["Global Quote"]
data["username"] = "AAPL"
result = requests.post(webhook, data=json.dumps(data), headers={"Content-Type": "application/json"})
try:
result.raise_for_status()
except requests.exceptions.HTTPError as err:
print(err)
else:
print("Payload delivered successfully, code {}.".format(result.status_code))
The output for this is: 400 Client Error: BAD REQUEST for url: https://discordapp.com/api/webhooks/609119785015312414/HOkHGPWqfPfLJHpSp15XZ9dUNhUDly_DKQ8OwLgk3Rw0-4G08gx0mOrKN3FKhompb5ig
btw - I deleted the webhook before posting this
There is one problem with your code. The webhooks are functioning properly.
data["content"] = symbol.json()["Global Quote"]
In the line data["content"] is a dict while it needs to be a string
You have to convert It to a String
There are two ways to fix this:-
First Way Using json -
import json
Then edit this line data["content"] = symbol.json()["Global Quote"] to -
data["content"] = json.dumps(symbol.json()["Global Quote"])
Second way-
Edit this line data["content"] = symbol.json()["Global Quote"] to -
data["content"] = str(symbol.json()["Global Quote"])
Just do:
from dhooks import Webhook
hook = Webhook("your-webhook")
hook.send("message-here")

Discord API 401 Unauthorized with OAuth

Quick question: I'm trying to use the Discord API to make a backup of all the messages on a server (or a guild, if you use the official term).
So I implemented OAuth without any problems, I have my access token and I can query some endpoints (I tried /users/#me, /users/#me/guilds). Though, most of them don't work. For example, if I query /users/#me/channels (which is supposed to be the DMs) I get a 401 Unauthorized response from the API. It's the same if I gather a guild id from /users/#me/guilds and then try to list the channels in it with /guilds/guild.id/channels.
The really weird thing is that I do have all the scopes required (I think so, I didn't take the RPC ones since I don't think it's required for what I want to do) and I can't figure it out myself... What is also weird is that on the OAuth authorization screen, I have those two things:
It kind of counterdicts itself... :(
Do you have any ideas you'd like to share ?
Thanks!
Note: I'm using Python but I don't think it's related here, since some endpoints do work with the headers and tokens I have...
Here is my "authentication code":
baseUrl = "https://discordapp.com/api"
def authorize():
scopes = [
"guilds",
"email",
"identify",
"messages.read",
"guilds.join",
"gdm.join",
"connections"
]
urlAuthorize = "{}/oauth2/authorize?client_id={}&scope={}&response_type=code".format(baseUrl, clientid, ('+'.join(scopes)))
pyperclip.copy(urlAuthorize)
code = input("Code: ")
return code
def getAccessToken(code):
url = "{}/oauth2/token".format(baseUrl)
params = {
"client_id" : clientid,
"client_secret" : clientsecret,
"redirect_uri" : "http://localhost",
"grant_type":"authorization_code",
"code" : code,
}
req = requests.post(url, params = params)
return json.loads(req.text)
And the code related to an API request:
def getHeaders():
return {
"Authorization" : "{} {}".format("Bearer", config["accessToken"]),
# "user-agent" : "DiscordBackup/0.0.1"
}
def getRequest(endpoint, asJson = True, additional = None):
url = "{}/{}".format(baseUrl, endpoint)
req = requests.get(url, headers = getHeaders())
print()
print(getHeaders())
print(url)
print(req.text)
if asJson:
return json.loads(req.text)
else:
return req.text
def getMe(): # this works
endpoint = "users/#me"
return getRequest(endpoint)
def getMyDMs(): # this gives me a code 401 Unauthorized
endpoint = "/users/#me/channels"
return getRequest(endpoint)
I came across this post when encountering this issue, and to put it bluntly, there's no way to resolve it.
The messages.read permission is for a local RPC server; https://discordapp.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes
However, local RPC servers are in private beta and you must sign up/get accepted to use this.
I wanted to create a DM exporter, but that doesn't look likely now.

Categories