Facebook Webhooks
Monitor a Facebook feed. Works reasonably well, but not exactly real-time (there's a 6 seconds delay between the action and the callback).
Create an app: https://developers.facebook.com/apps/
In order to create a webhook you need a valid HTTPS URL that will serve as callback.
openssl req -x509 -sha512 -nodes -days 365 -newkey rsa:4096 -keyout /etc/ssl/private/certificat.key -out /etc/ssl/private/certificat.crt virtualenv -p python3 certbot cd certbot . bin/activate pip install certbot su cerbot certonly cp /etc/letsencrypt/live/yourdomain.ext/privkey.pem privkey.pem cp /etc/letsencrypt/live/yourdomain.ext/fullchain.pem fullchain.pem chown youruser:youruser privkey.pem chown youruser:youruser fullchain.pem exit
Verify Callback URL
- In App Manager left sidebar, choose "Add product" then "Webhook".
- In the webhook page, choose "Page" in the first dropdown menu (should be selected by default) then "Subscribe to this topic".
- Add your callback URL and a token of your choice
- Start your HTTPS server. Your server must respond on GET request with the
hug.challenge
. (You should also check that theverify_token
that you've chosen matches).
Example callback here : https://github.com/LgHS/facebook-webhooks/blob/master/app/Http/Controllers/FacebookController.php
Subscribe a page to your app
Once your webhook is set, the page you want to monitor needs to be subscribed to your app to receive the in-real-time notifications as stated here:
You can use Graph API Explorer to subscribe your app to the page : https://developers.facebook.com/tools/explorer/?method=POST&path=%7Bpage-id%7D%2Fsubscribed_apps&version=v2.9
- Choose the App you'll use for the webhooks
- Choose the Page Access Token for the page you want to monitor. You may need to authorize the Graph Api to manage pages to do that.
- Add the page ID of the page you want to monitor.
- You should see
{success: true}
as a response. Check that your app is there by trying the sending the same request with GET method.
Others
It's much simpler to monitor Twitter feeds in real-time.
Example: https://github.com/ideoforms/python-twitter-examples/blob/master/twitter-stream-responder.py
Requirements:
pip install twitter
- Create an app on Twitter: https://apps.twitter.com/app/new
You'll need the following info in your config.py
:
consumer_key = "XxXxXxxXXXxxxxXXXxXX"
consumer_secret = "xXXXXXXXXxxxxXxXXxxXxxXXxXxXxxxxXxXXxxxXXx"
access_key = "XXXXXXXX-xxXXxXXxxXxxxXxXXxXxXxXxxxXxxxxXxXXxXxxXX"
access_secret = "XxXXXXXXXXxxxXXXxXXxXxXxxXXXXXxXxxXXXXx"