Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.40 KB | None | 0 0
  1. def order_Tuna(intent_request):
  2.    
  3.     dynamo = boto3.resource('dynamodb')
  4.    
  5.     table = dynamo.Table('delivery_order')
  6.     recordId = str(uuid.uuid4())
  7.     location = try_ex(lambda: intent_request['currentIntent']['slots']['address'])
  8.     Tuna = try_ex(lambda: intent_request['currentIntent']['slots']['Fast'])
  9.     quantity = safe_int(try_ex(lambda: intent_request['currentIntent']['slots']['number']))
  10.     uid = try_ex(lambda: intent_request['userId'])
  11.     eid = try_ex(lambda: intent_request['currentIntent']['slots']['email'])
  12.     session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {}
  13.  
  14.     # Load confirmation history and track the current reservation.
  15.     reservation = json.dumps({
  16.         'ReservationType': 'Tuna',
  17.         'address': location,
  18.         'number': quantity,
  19.         'Fast': Tuna,
  20.         'Email':eid
  21.     })
  22.  
  23.     session_attributes['currentReservation'] = reservation
  24.  
  25.     if intent_request['invocationSource'] == 'DialogCodeHook':
  26.         # Validate any slots which have been specified.  If any are invalid, re-elicit for their value
  27.         validation_result = validate_Tuna(intent_request['currentIntent']['slots'])
  28.         if not validation_result['isValid']:
  29.             slots = intent_request['currentIntent']['slots']
  30.             slots[validation_result['violatedSlot']] = None
  31.  
  32.             return elicit_slot(
  33.                 session_attributes,
  34.                 intent_request['currentIntent']['name'],
  35.                 slots,
  36.                 validation_result['violatedSlot'],
  37.                 validation_result['message'],
  38.                 build_response_card(
  39.                     'Specify {}'.format(validation_result['violatedSlot']),
  40.                     build_options(validation_result['violatedSlot'], Tuna)
  41.                    
  42.                 )
  43.             )
  44.  
  45.         # Otherwise, let native DM rules determine how to elicit for slots and prompt for confirmation.  Pass price
  46.         # back in sessionAttributes once it can be calculated; otherwise clear any setting from sessionAttributes.
  47.      
  48.         if location and quantity and Tuna :
  49.            
  50.             price = quantity*11
  51.             session_attributes['currentReservationPrice'] = price
  52.         else:
  53.             try_ex(lambda: session_attributes.pop('currentReservationPrice'))
  54.  
  55.         session_attributes['currentReservation'] = reservation
  56.         return delegate(session_attributes, intent_request['currentIntent']['slots'])
  57.  
  58.    
  59.     logger.debug('orderTuna under={}'.format(reservation))
  60.  
  61.     try_ex(lambda: session_attributes.pop('currentReservationPrice'))
  62.     try_ex(lambda: session_attributes.pop('currentReservation'))
  63.     session_attributes['lastConfirmedReservation'] = reservation
  64.     DynamoDict = {
  65.         'id': recordId,
  66.         'item': Tuna,
  67.         'quantity': quantity,
  68.         'address': location,
  69.         'price': quantity*11,
  70.         'emailId': eid
  71.        
  72.         }
  73.  
  74.     table.put_item(Item=DynamoDict)
  75.    
  76.    
  77.     ses = boto3.client('ses')
  78.  
  79.     email_from = 'bhushansainidss.1@gmail.com'
  80.     email_to = '{}'.format(eid)
  81.     email_cc = 'Email'
  82.     emaiL_subject = 'Subject'
  83.     email_body = 'Body'
  84.     pr='Order Id : {}\nYour order for {} {} of {} dollars at {} has been placed and it will be delivered within 30 mins.\nThanks for ordering your meal.\n Have a nice day.'.format(recordId,quantity,Tuna,quantity*11,location)
  85.                
  86.     response = ses.send_email(
  87.         Source = email_from,
  88.         Destination={
  89.             'ToAddresses': [
  90.                 email_to,
  91.             ]
  92.            
  93.         },
  94.         Message={
  95.             'Subject': {
  96.                 'Data': 'ORDER CONFIRMATION FROM BOT-the-CAFE'
  97.             },
  98.             'Body': {
  99.                 'Text': {
  100.                     'Data': pr
  101.                    
  102.                 }
  103.             }
  104.         }
  105.     )
  106.     return close(
  107.         session_attributes,
  108.         'Fulfilled',
  109.         {
  110.             'contentType': 'PlainText',
  111.             'content': 'Thanks, I have placed your order and your order ID is {}. It will be delivered before 30 minutes. Please let me know if you would like to order some Beverages and if you want to continue with this order then type utterances like : i want some beverages , i am thirsty, i want a drink  '.format(
  112.                 recordId
  113.                 )
  114.                      
  115.         }
  116.     )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement