Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import os
  2. import base64
  3. import hashlib
  4. import hmac
  5. import json
  6. import tweepy
  7. import urllib
  8. from twilio.request_validator import RequestValidator
  9.  
  10.  
  11. def lambda_handler(event, context):
  12. print("received: " + str(event))
  13. # if twilioSignature exists and message from authorized number, create a validator & a dictionary of received data
  14. if u'twilioSignature' in event and u'Body' in event and event['From'] == os.environ['MY_NUMBER']:
  15. form_parameters = {
  16. key: urllib.parse.unquote_plus(value) for key, value in event.items()
  17. if key != u'twilioSignature'
  18. }
  19.  
  20. validator = RequestValidator(os.environ['AUTH_TOKEN'])
  21.  
  22. # validate api call is from twilio
  23. request_valid = validator.validate(
  24. os.environ['REQUEST_URL'],
  25. form_parameters,
  26. event[u'twilioSignature']
  27. )
  28.  
  29. # if request valid, authenticate tweepy and send tweet
  30. if request_valid:
  31. auth = tweepy.OAuthHandler(os.environ['API_KEY'], os.environ['API_SECRET'])
  32. auth.set_access_token(os.environ['ACCESS_TOKEN'], os.environ['ACCESS_SECRET'])
  33. api = tweepy.API(auth)
  34. msg = form_parameters['Body']
  35. api.update_status(msg)
  36.  
  37. return '<?xml version=\"1.0\" encoding=\"UTF-8\"?>' \
  38. '<Response><Message>Request Received, OK!</Message></Response>'
  39.  
  40. # if request is not from twilio, give appropriate response
  41. else:
  42. return '<?xml version=\"1.0\" encoding=\"UTF-8\"?>' \
  43. '<Response><Message>Nice Try...</Message></Response>'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement