Skip to content

Commit

Permalink
feat: add nearby requests list in chatbot
Browse files Browse the repository at this point in the history
  • Loading branch information
geeekgod committed Apr 14, 2023
1 parent 73969a1 commit 575ed09
Showing 1 changed file with 67 additions and 13 deletions.
80 changes: 67 additions & 13 deletions app/screens/ChatBot/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,82 @@ import React, { useState, useCallback, useContext } from 'react'
import { GiftedChat } from 'react-native-gifted-chat'
import bloodLineApi from '../../api';
import { AuthContext } from '../../context/AuthContext';
import { DataContext } from '../../context/DataContext';

const ChatBot = () => {

const {user} = useContext(AuthContext);
const { user } = useContext(AuthContext);
const { nearByRequests } = useContext(DataContext);

const [messages, setMessages] = useState([]);

const sendMessage = useCallback(async (message) => {
bloodLineApi.post('/chatbot', { query: message }).then((res) => {
setMessages(previousMessages => GiftedChat.append(
previousMessages,
{
_id: previousMessages.length + 1,
text: res.data.response,
createdAt: new Date(),
user: {
_id: 2,
name: 'Blood Line',
avatar: require('../../assets/logo.png'),
}

const responseFromChatbot = res.data.response.split("\n");

if (responseFromChatbot.length > 1 && responseFromChatbot[0] == "nearby_req") {
if (nearByRequests.length > 0) {
setMessages(previousMessages => GiftedChat.append(
previousMessages,
{
_id: previousMessages.length + 1,
text: responseFromChatbot[1],
createdAt: new Date(),
user: {
_id: 2,
name: 'Blood Line',
avatar: require('../../assets/logo.png'),
}
}
))
nearByRequests.map((request) => {
setMessages(previousMessages => GiftedChat.append(
previousMessages,
{
_id: previousMessages.length + 1,
text: `Name: ${request.name}\nBlood Group: ${request.bloodGroup}\nContact: ${request.phone}\nLocation: ${request.address} ${request.city} ${request.pin}\nView on Map: https://maps.google.com/maps?q=${request.location[1]},${request.location[0]}`,
createdAt: new Date(),
user: {
_id: 2,
name: 'Blood Line',
avatar: require('../../assets/logo.png'),
}
}
))
})
}
else {
setMessages(previousMessages => GiftedChat.append(
previousMessages,
{
_id: previousMessages.length + 1,
text: responseFromChatbot[2],
createdAt: new Date(),
user: {
_id: 2,
name: 'Blood Line',
avatar: require('../../assets/logo.png'),
}
}
))
}
))
}
else {
setMessages(previousMessages => GiftedChat.append(
previousMessages,
{
_id: previousMessages.length + 1,
text: responseFromChatbot[0],
createdAt: new Date(),
user: {
_id: 2,
name: 'Blood Line',
avatar: require('../../assets/logo.png'),
}
}
))
}
}).catch((err) => {
console.log("Error while sending message", err);
})
Expand Down

0 comments on commit 575ed09

Please sign in to comment.