Skip to content

Commit

Permalink
Fixed: Webhook API for For All Fulfillments
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyvishal committed Apr 9, 2024
1 parent b750486 commit 6119c3f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
37 changes: 37 additions & 0 deletions config/message.json
Original file line number Diff line number Diff line change
@@ -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!"
}
}
}
40 changes: 30 additions & 10 deletions controllers/Bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

}
Expand Down

0 comments on commit 6119c3f

Please sign in to comment.