Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import boto3
- import os
- import random
- import string
- import botocore.vendored.requests as requests
- from botocore.exceptions import ClientError
- from urllib.parse import urlparse, parse_qs
- def send_sms(number, message):
- client = boto3.client('sns')
- try:
- client.publish(PhoneNumber=number, Message=message)
- except Exception as e:
- print(e)
- return False
- return True
- def lambda_handler(event, context):
- if 'httpMethod' in event:
- http_method = event['httpMethod']
- if 'queryStringParameters' in event:
- query_string = event['queryStringParameters']
- else:
- query_string = ''
- path = event['path']
- print(' my path is: ' + path)
- if http_method == 'GET':
- if path == '':
- html = 'Nothing to see here'
- if path == '/t':
- html = '''
- <form action="" method="post">
- Phone number:<br>
- <input type="text" name="number" value="332-">
- <br>
- <input type="submit" value="Submit">
- </form>
- </body>
- </html>
- '''
- # emergency client landing page
- elif '/test' in path:
- html = '''
- <html>
- <body>
- <center><p><h2><b>EMERGENCY TRACKER</h2></b></p></center>
- <center><button style="height:1400px;width:900px" onclick="getLocation()"><h1><b>CLICK TO SHAERE LOCATION</h1></b></button></center>
- <p id="demo"></p>
- <script>
- var x = document.getElementById("demo");
- var xhttp = new XMLHttpRequest();
- var urlParams = new URLSearchParams(window.location.search);
- function getLocation() {
- if (navigator.geolocation) {
- navigator.geolocation.watchPosition(showPosition);
- } else {
- x.innerHTML = "Geolocation is not supported by this browser.";
- }
- }
- function showPosition(position) {
- xhttp.open("GET", "https://gps.yoyo.co.il/location=true&id=" + urlParams.get('id') + "&latitude=" + position.coords.latitude + "&longitude=" + position.coords.longitude, true);
- xhttp.send();
- }
- </script>
- </body>
- </html>
- '''
- # push location to s3
- elif '/location' in path:
- parse_post_body = parse_qs(path)
- s3 = boto3.resource('s3')
- print(parse_post_body)
- if 'latitude' in parse_post_body:
- print(1)
- latitude = parse_post_body['latitude'][0]
- longitude = parse_post_body['longitude'][0]
- id = parse_post_body['id'][0]
- content=str({'latitude': latitude, 'longitude': longitude})
- s3.Object('gps-super-secret', id + '.txt').put(Body=content)
- html = '''
- ok
- '''
- # show location to rep
- elif '/get_location' in path:
- parse_post_body = parse_qs(path)
- s3 = boto3.resource('s3')
- id = query_string['id']
- if id:
- obj = s3.Object('gps-super-secret', id + '.txt')
- try:
- location = json.loads(obj.get()['Body'].read().decode('utf-8').replace('\'', '"'))
- except ClientError as ex:
- if ex.response['Error']['Code'] == 'NoSuchKey':
- return {'statusCode': 200,
- 'headers': {
- 'Content-Type': 'text/html',
- },
- 'body': '<b><h1>No results yet</b></h1> <meta http-equiv="refresh" content="5" />'
- }
- lat = location['latitude']
- lng = location['longitude']
- html = '''<html>
- <head>
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
- <meta charset="utf-8">
- <title>Simple Markers</title>
- <style>
- #map {
- height: 100%;
- }
- html, body {
- height: 100%;
- margin: 0;
- padding: 0;
- }
- </style>
- </head>
- <body>
- <div id="map"></div>
- <script>
- function initMap() {
- var myLatLng = {lat: ''' + lat + ''', lng: ''' + lng + '''};
- var map = new google.maps.Map(document.getElementById('map'), {
- zoom: 18,
- center: myLatLng
- });
- var marker = new google.maps.Marker({
- position: myLatLng,
- map: map,
- title: 'Hello World!'
- });
- }
- </script>
- <script async defer
- src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY_HERE&callback=initMap">
- </script>
- </body>
- </html>
- '''
- else:
- html = path
- print(event)
- return {
- 'statusCode': 200,
- 'headers': {
- 'Content-Type': 'text/html',
- },
- 'body': html
- }
- elif http_method == 'POST':
- random_string = ''.join([random.choice(string.ascii_letters + string.digits) for n in range(32)])
- phone_number = event['body'].split('=')[1]
- send_sms(phone_number, f'Locaiton tracker - go to https://gps.yoyo.co.il/test?id={random_string}')
- html = f'sent sms to {phone_number}, go to: <a href="https://gps.yoyo.co.il/get_location?t=1&id={random_string}">here</a> to watch location'
- return {
- 'statusCode': 200,
- 'headers': {
- 'Content-Type': 'text/html',
- },
- 'body': html
- }
- else:
- return {
- 'statusCode': 200,
- 'headers': {
- 'Content-Type': 'application/javascript',
- },
- 'body': json.dumps(event, sort_keys=True, indent=4)
- }
Advertisement
Add Comment
Please, Sign In to add comment