//Uber Eats API v5.0.3 Integration for BP Meadowlands (Store hardcoded) //Get Notification of order request text = var('request.content') //Parse order details link url=json_decode(text)['resource_href'] //Get link for Order Details //Parse order_id event_type=json_decode(text)['event_type'] //Decode event type resource=json_decode(text)['meta'] //Decode meta array uber_id=(resource)['resource_id'] order_id=string_slice((resource)['resource_id'], -5); // Get Order ID from meta array if (event_type == 'orders.release') { dump('Event type is orders.notification. Script stopping.'); return; // Exit script early if event_type is orders.notification } if(event_type == 'orders.cancel') { request('ntfy.sh/rdp0huLjjJnGSQb7', 'skeduber: Uber Order UE:'+order_id+' was cancelled', 'POST' ); dump(order_id+' cancelled') } else { //Create XML to Authenticate content = query([ 'client_id': var('uber.client_id'), //Add "test." for production 'client_secret': var('uber.client_secret'), //Add "test." for production 'grant_type': 'client_credentials', 'scope': 'eats.store.orders.read' ]) headers = ['Content-Type: application/x-www-form-urlencoded']; //POST request for Authentication Token ws1_response = ['status': null] //Set status to start at null requests = 0 while (requests < 3) { ws1_response = request('https://login.uber.com/oauth/v2/token', content, 'POST', headers); if (ws1_response['status']) { if (ws1_response['status'] == 200) { dump('Authentication Succeeded'); //Parse Authentication token token=json_decode(ws1_response['content'])['access_token'] auth = ['Authorization:Bearer '+token] //Format for header of Order Detail Request //GET Request for Order Details ws2_response = ['status': null] //Set status to start at null requests = 0 while (requests < 3) { ws2_response = request(url,'','GET',auth); if (ws2_response['status']) { if (ws2_response['status'] == 200) { dump('Order Details Retrieved'); //Parse Order Details response type = json_decode(ws2_response['content'])['type'] if (type == 'DELIVERY_BY_RESTAURANT') { uber_pickup_time = json_decode(ws2_response['content'])['estimated_ready_for_pickup_at']; pickup_time = date_format(uber_pickup_time,'YYYY-MM-DD HH:mm:ss'); uber_delivery_time = to_date(uber_pickup_time+'+140 minutes'); delivery_time = date_format(uber_delivery_time,'YYYY-MM-DD HH:mm:ss'); dump(pickup_time) dump(delivery_time) eater = json_decode(ws2_response['content'])['eater']; //dump(eater) customer_firstname = (eater)['first_name']; //Customer First Name //dump(customer_firstname) customer_lastname = (eater)['last_name']; //Customer Last Name //dump(customer_lastname) customer_phone = ' '; if ( eater.has('phone') ) { customer_phone = (eater)['phone']; //dump(customer_phone) } phone_code = ' '; if ( eater.has('phone_code') ) { phone_code = (eater)['phone_code']; //dump(phone_code) } delivery_type = ' '; if ( eater['delivery'].has('type') ) { delivery_type = (eater)['delivery']['type']; //dump(delivery_type) } notes = ' '; if ( eater['delivery'].has('.notes') ) { notes = (eater)['delivery']['notes']; //dump(notes) } business_name = ' '; if ( eater['delivery']['location'].has('business_name') ) { business_name = (eater)['delivery']['location']['business_name']; //dump(business_name) } unit_number = ' '; if ( eater['delivery']['location'].has('unit_number') ) { unit_number = (eater)['delivery']['location']['unit_number']; //dump(unit_number) } location_type = 'STREET_ADDRESS'; if ( eater['delivery']['location'].has('type') ) { location_type = (eater)['delivery']['location']['type']; //dump(location_type) } payment = json_decode(ws2_response['content'])['payment'] //dump(payment) subtotal = 0 if ( payment['charges'].has('sub_total')) {subtotal = (payment)['charges']['sub_total']['amount']; //dump(subtotal) } tip = 0 if ( payment['charges'].has('tip')) {tip = (payment)['charges']['tip']['amount']; //dump(tip) } //If GOOGLE_PLACE Get Lat/long from MAPS API otherwise use STREET_ADDRESS Lat/Long if (location_type == 'GOOGLE_PLACE') { place_id = (eater)['delivery']['location']['google_place_id'] google_api='https://maps.googleapis.com/maps/api/place/details/json?place_id='+place_id+'&key='+var('maps_key') ws3_response = ['status': null] //Set status to start at null requests = 0 while (requests < 3) { ws3_response = request(google_api,'','GET',['']); if (ws3_response['status']) { if (ws3_response['status'] == 200) { dump('Google Place Retrieved'); result = json_decode(ws3_response['content'])['result'] address = result['formatted_address'] lat = result['geometry']['location']['lat'] lng = result['geometry']['location']['lng']; //Create JSON for Tookan (see Tookan API documentation) payload = json_encode([ 'notify': 1, 'api_key': var('skedadel.api.key'), 'auto_assignment': 1, 'team_id': '1479655', 'timezone': '-120', 'ride_type': 1, 'has_pickup': 1, 'layout_type': 0, 'has_delivery': 1, 'tracking_link': 0, 'order_id': 'UE:'+order_id+' '+customer_firstname+' '+customer_lastname, 'customer_username': customer_firstname+' '+customer_lastname, 'customer_phone': customer_phone, 'customer_address': address+' '+business_name, 'latitude': lat, 'longitude': lng, 'job_description': delivery_type + ': ' +notes, 'job_delivery_datetime': delivery_time, 'job_pickup_name': 'KFC BP MEADOWLANDS', 'job_pickup_phone': '0860 100 222', 'job_pickup_address': 'Corner Modise Street And, Mabasotho St, Meadowlands, Soweto, 1803', 'job_pickup_datetime': pickup_time, 'custom_field_template': 'Uber', 'ignore_validate_otp': 1, 'pickup_custom_field_template': 'Uber', 'pickup_meta_data': [ ['data': 'Uber_Eats', 'label': 'App' ], ['data': 'KFC BP MEADOWLANDS', 'label': 'Stores_Name' ], ['data': 'Corner Modise Street And, Mabasotho St, Meadowlands, Soweto, 1803', 'label': "Stores_Display_Address" ], ['data': 'Uber Eats Order', 'label': 'Special_Instructions' ], ['data': subtotal/100, 'label': 'Subtotal' ], ['data': '0', 'label': 'DeliveryCharges' ], ['data': tip/100, 'label': 'Tip_Amount' ], ['data': 'Paid', 'label': 'Payment_Type' ], ['data': uber_id, 'label': 'Reference' ] ], 'meta_data': [ ['data': 'Uber_Eats', 'label': 'App' ], ['data': 'KFC BP MEADOWLANDS', 'label': 'Stores_Name' ], ['data': 'Corner Modise Street And, Mabasotho St, Meadowlands, Soweto, 1803', 'label': "Stores_Display_Address" ], ['data': 'Uber Eats Order', 'label': 'Special_Instructions' ], ['data': subtotal/100, 'label': 'Subtotal' ], ['data': '0', 'label': 'DeliveryCharges' ], ['data': tip/100, 'label': 'Tip_Amount' ], ['data': 'Paid', 'label': 'Payment_Type' ], ['data': uber_id, 'label': 'Reference' ] ] ]) // POST payload to Skedadel with warnings to Pushed.com if failed // <-- START cut here to test payload res = ['status': null] //Set status to start at null requests = 0 while (requests < 3) { //POST to Skedadel Dashboard res = request( 'https://api.tookanapp.com/v2/create_task', payload, 'POST', ['Content-Type: application/json']) //Check if dashboard sent a success response if (res['status']) { if (res['status'] == 200) { dump('Send to Skedadel with Google Place succeeded'); break } if (res['status'] != 200) { dump('Send to Skedadel with Google Place failed'); } } if (requests == 2) { request('ntfy.sh/rdp0huLjjJnGSQb7', 'skeduber: '+order_id+' Uber Try {}'.format(requests), 'POST', ['Authorization: Bearer tk_8z3vi5ribtimwpucou54r3vd6yu7l']); } requests = requests + 1 //add iteration to effect loop } break } if (ws3_response['status'] != 200) { dump('Google Place Retrieval failed'); } } if (requests == 2) { request('ntfy.sh/rdp0huLjjJnGSQb7', 'skeduber: '+order_id+' Google Place Retrieval failed', 'POST', ['Authorization: Bearer tk_8z3vi5ribtimwpucou54r3vd6yu7l']); } requests = requests + 1 //add iteration to effect loop } } else { echo('Adress supplied by Uber'); address = 'Test Address' if ( eater['delivery']['location'].has('street_address')) {address = (eater)['delivery']['location']['street_address'];} lat = '-33.8782998' if ( eater['delivery']['location'].has('latitude')) {lat = (eater)['delivery']['location']['latitude'];} lng = '18.6342725' if ( eater['delivery']['location'].has('longitude')) {lng = (eater)['delivery']['location']['longitude'];}; //Create JSON for Tookan (see Tookan API documentation) payload = json_encode([ 'notify': 1, 'api_key': var('skedadel.api.key'), 'auto_assignment': 1, 'team_id': '1479655', 'timezone': '-120', 'ride_type': 1, 'has_pickup': 1, 'layout_type': 0, 'has_delivery': 1, 'tracking_link': 0, 'order_id': 'UE:'+order_id+' '+customer_firstname+' '+customer_lastname, 'customer_username': customer_firstname+' '+customer_lastname, 'customer_phone': customer_phone, 'customer_address': address+' '+business_name, 'latitude': lat, 'longitude': lng, 'job_description': delivery_type + ': ' +notes, 'job_delivery_datetime': delivery_time, 'job_pickup_name': 'KFC BP MEADOWLANDS', 'job_pickup_phone': '0860 100 222', 'job_pickup_address': 'Corner Modise Street And, Mabasotho St, Meadowlands, Soweto, 1803', 'job_pickup_datetime': pickup_time, 'ignore_validate_otp': 1, 'custom_field_template': 'Uber', 'pickup_custom_field_template': 'Uber', 'pickup_meta_data': [ ['data': 'Uber_Eats', 'label': 'App' ], ['data': 'KFC BP MEADOWLANDS', 'label': 'Stores_Name' ], ['data': 'Corner Modise Street And, Mabasotho St, Meadowlands, Soweto, 1803', 'label': "Stores_Display_Address" ], ['data': 'Uber Eats Order', 'label': 'Special_Instructions' ], ['data': subtotal/100, 'label': 'Subtotal' ], ['data': '0', 'label': 'DeliveryCharges' ], ['data': tip/100, 'label': 'Tip_Amount' ], ['data': 'Paid', 'label': 'Payment_Type' ], ['data': uber_id, 'label': 'Reference' ] ], 'meta_data': [ ['data': 'Uber_Eats', 'label': 'App' ], ['data': 'KFC BP MEADOWLANDS', 'label': 'Stores_Name' ], ['data': 'Corner Modise Street And, Mabasotho St, Meadowlands, Soweto, 1803', 'label': "Stores_Display_Address" ], ['data': 'Uber Eats Order', 'label': 'Special_Instructions' ], ['data': subtotal/100, 'label': 'Subtotal' ], ['data': '0', 'label': 'DeliveryCharges' ], ['data': tip/100, 'label': 'Tip_Amount' ], ['data': 'Paid', 'label': 'Payment_Type' ], ['data': uber_id, 'label': 'Reference' ] ] ]) // POST payload to Skedadel with warnings to Pushed.com if failed // <-- START cut here to test payload res = ['status': null] //Set status to start at null requests = 0 while (requests < 3) { //POST to Skedadel Dashboard res = request( 'https://api.tookanapp.com/v2/create_task', payload, 'POST', ['Content-Type: application/json']) //Check if dashboard sent a success response if (res['status']) { if (res['status'] == 200) { dump('Send to Skedadel with Uber Address succeeded'); dump(payload) break } if (res['status'] != 200) { dump('Send to Skedadel with Uber Address failed'); } } if (requests == 2) { request('ntfy.sh/rdp0huLjjJnGSQb7', 'skeduber: '+order_id+' Uber Try {}'.format(requests), 'POST', ['Authorization: Bearer tk_8z3vi5ribtimwpucou54r3vd6yu7l']); } requests = requests + 1 //add iteration to effect loop } }; } else { dump('Not a Delivery Order') }; break } if (ws2_response['status'] != 200) { dump('Order Detail Retrieval failed'); } } if (requests == 2) { request('ntfy.sh/rdp0huLjjJnGSQb7', 'skeduber: '+order_id+' Order Detail Retrieval failed', 'POST', ['Authorization: Bearer tk_8z3vi5ribtimwpucou54r3vd6yu7l']); } requests = requests + 1 //add iteration to effect loop } break } if (ws1_response['status'] != 200) { dump('Authentication Failed'); } } if (requests == 2) { request('ntfy.sh/rdp0huLjjJnGSQb7', 'skeduber: '+order_id+' Authentication failed', 'POST', ['Authorization: Bearer tk_8z3vi5ribtimwpucou54r3vd6yu7l']); } requests = requests + 1 //add iteration to effect loop } }