Skip to content

Commit

Permalink
Added: Update Status to webhook API
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyvishal committed Apr 9, 2024
1 parent 8286a23 commit 1fd4a1f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 89 deletions.
59 changes: 51 additions & 8 deletions controllers/Bot.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import ActionsService from '../services/Actions.js'
import AI from '../services/AI.js'

import logger from '../utils/logger.js'
import {db} from '../server.js'
import { v4 as uuidv4 } from 'uuid'
import MapsService from '../services/MapService.js'
import { fileURLToPath } from 'url';
import path from 'path'

const mapService = new MapsService()

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import CronService from '../services/CronService.js'
const actionsService = new ActionsService()

/**
Expand Down Expand Up @@ -105,6 +100,7 @@ async function process_text(req, res) {
bookings: [],
active_transaction: null,
routes:[],
orders:[],
selected_route:null
}

Expand Down Expand Up @@ -228,9 +224,10 @@ async function process_text(req, res) {
session.bookings = ai.bookings;
response = await process_action(ai.action, message, session, sender, format);
ai.bookings = response.bookings;

// update actions
if(ai.action?.action === 'confirm') {
session.orders.push(response.raw.responses[0]);
session.actions = EMPTY_SESSION.actions;
session.text = EMPTY_SESSION.text;
}
Expand Down Expand Up @@ -387,7 +384,53 @@ async function process_action(action, text, session, sender=null, format='applic

return response;
}

async function webhookControl (req, res) {
try{
const sessions = await db.get_all_sessions();
logger.info(`Got ${sessions.length} sessions.`)


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) {
// 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"){
console.log('here3', session.data.orders);
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);
}
}
catch(e){
logger.error(e);
}

}
}
return res.status(200).json({
status:true,
message:'Notification Sent'
})
}catch(error){
return res.status(400).json({
status:false,
message:'Some Error Occured'
})
}
}



export default {
process_wa_webhook,
process_text,
process_text,
webhookControl,
}
2 changes: 2 additions & 0 deletions controllers/ControlCenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ export const updateStatus = async (req, res) => {
state_value: DOMAIN_DETAILS.message,
},
},{ Authorization: `Bearer ${DOMAIN_DETAILS.token}`})
const webhookResponse = await action.call_api(`${process.env.BASE_URL}/webhook-ps`, 'POST',{...updateStatusResponse, orderId:orderId});
logger.info(JSON.stringify(webhookResponse));
return res.status(200).send({ message: `Status Updated to: ${updateStatusResponse.data.data.attributes.state_value}`, status:true })
}

Expand Down
8 changes: 3 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import dotenv from 'dotenv'
import cors from 'cors'
import path from 'path'
import { fileURLToPath } from 'url';
dotenv.config()
import express from 'express'
import bodyParser from 'body-parser'
Expand All @@ -27,7 +25,7 @@ app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }))
app.use('/public', express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json())
app.use('/static', express.static(path.join(__dirname, 'public')));

// Define endpoints here
// app.post('/act', actions.act)
app.post('/webhook', messageController.process_text)
Expand All @@ -38,11 +36,11 @@ app.post('/trigger-exception', triggerExceptionOnLocation)
app.post('/update-status', updateStatus)
app.post('/unpublish-item', unpublishItem)
app.post('/webhook-ps', messageController.webhookControl)
app.get('/download', messageController.downloadFile);
// Reset all sessions
export const db = new DBService()

// await db.clear_all_sessions()
await db.clear_all_sessions()

await db.set_data('orderDetails', ORDER_DETAILS)

// Start the Express server
Expand Down
76 changes: 0 additions & 76 deletions services/CronService.js

This file was deleted.

0 comments on commit 1fd4a1f

Please sign in to comment.