diff --git a/config/message.json b/config/message.json new file mode 100644 index 0000000..85c1697 --- /dev/null +++ b/config/message.json @@ -0,0 +1,37 @@ +{ + "FULFILLMENT_STATUS_CODES":{ + "order-picked-up":{ + "message": "Your order has been picked up" + }, + "order-on-the-way": { + "message": "Your order is on the way" + }, + "order-delivered":{ + "message": "Your order has been delivered" + }, + "ticket-issued":{ + "message": "Your ticket has been issued" + }, + "ticket-validated":{ + "message": "Your ticket has been validated" + }, + "charging-started": { + "message": "Your charging has started" + }, + "charging-stopped":{ + "message": "Your charging has stopped" + }, + "charger-not-working":{ + "message": "Hi, it looks likes your charger is not working. DO you want to find other chargin stations?" + }, + "charging-completed":{ + "message": "Hi, your chargin is complete!" + }, + "checked-in": { + "message": "You have succesfully checked-in to your stay!" + }, + "checked-out":{ + "message": "You have succesfully checked-out from your stay!" + } + } +} \ No newline at end of file diff --git a/controllers/Bot.js b/controllers/Bot.js index 9c1f6b1..e24b87d 100644 --- a/controllers/Bot.js +++ b/controllers/Bot.js @@ -4,11 +4,12 @@ import logger from '../utils/logger.js' import DBService from '../services/DBService.js' import { v4 as uuidv4 } from 'uuid' import MapsService from '../services/MapService.js' - +import {readFileSync} from 'fs' const mapService = new MapsService() const db = new DBService() const actionsService = new ActionsService() +const message_config = JSON.parse(readFileSync('./config/message.json')) /** * @deprecated * @param {*} req @@ -394,22 +395,41 @@ async function process_action(action, text, session, sender=null, format='applic for(let session of sessions){ const orders = session.data.orders; const index = orders ? orders.findIndex((order)=>order.message.order.id == req.body.orderId) : null; - if(index!==null && orders[index].message.order.fulfillments[0].state.descriptor.name !== req.body.data.data.attributes.state_code) { + if(index!==null && index>=0 && (!orders[index].message.order.fulfillments[0].state||orders[index].message.order.fulfillments[0].state.descriptor.code !== req.body.data.data.attributes.state_code )) { // send whatsapp and add to context try{ // update session - orders[index].message.order.fulfillments[0].state.descriptor.name = req.body.data.data.attributes.state_code - let reply_message = ''; - if(req.body.data.data.attributes.state_code === "charger-not-working"){ - reply_message ="Hey, looks like the charger is not working, do you want to look for other chargers on the route?" - await actionsService.send_message(session.key, reply_message); - if(!session.data.text) session.data.text=[] - session.data.text.push({role: 'assistant', content: reply_message}); - await db.update_session(session.key, session.data); + orders[index].message.order.fulfillments[0] = { + ...orders[index].message.order.fulfillments[0], + state:{ + descriptor:{ + code:req.body.data.data.attributes.state_code, + short_desc:req.body.data.data.attributes.state_value + }, + updated_at:req.body.data.data.attributes.updatedAt + } + } + let reply_message = `Hey the status of your order is updated to ${req.body.data.data.attributes.state_code}` + if(Object.keys(message_config.FULFILLMENT_STATUS_CODES).includes(req.body.data.data.attributes.state_code)){ + reply_message = message_config.FULFILLMENT_STATUS_CODES[req.body.data.data.attributes.state_code].message } + await actionsService.send_message( + session.key, + reply_message + ) + if (!session.data.text) session.data.text = [] + session.data.text.push({ + role: 'assistant', + content: reply_message, + }) + await db.update_session( + session.key, + session.data + ) } catch(e){ logger.error(e); + throw new Error(e.message) } }