Chatbot.js 3.82 KB
Newer Older
Malith Anjana's avatar
Malith Anjana committed
1 2 3
import axios from 'axios';
import React, { useCallback, useEffect, useState } from 'react'
import { Text, View } from 'react-native'
Malith Anjana's avatar
Malith Anjana committed
4
import { Bubble, GiftedChat, Send } from 'react-native-gifted-chat';
Malith Anjana's avatar
Malith Anjana committed
5
import { getChats, sendToRasa } from '../api';
Malith Anjana's avatar
Malith Anjana committed
6
import { ChatHeader } from '../components/chatbot/ChatHeader'
Malith Anjana's avatar
Malith Anjana committed
7 8 9 10
import uuid from 'react-native-uuid';
import {IMAGE} from '../assets/images/chatbotImage'
import { COLOR, FONT } from '../themes';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons'
Malith Anjana's avatar
Malith Anjana committed
11 12
import { unixConvertion } from '../assets/utils/UnixConverter';
import { AlanView } from '@alan-ai/alan-sdk-react-native';
Malith Anjana's avatar
Malith Anjana committed
13 14 15 16

export const Chatbot = () => {
  const [messages, setMessages] = useState([]);

Malith Anjana's avatar
Malith Anjana committed
17 18 19 20 21 22 23 24 25 26
  const BOT_USER = {
    _id: 2,
    name: 'FAQ Bot',
    avatar: IMAGE.chatbot
  };
  const USER = {
    _id:1,
    name: 'USER',
  };

Malith Anjana's avatar
Malith Anjana committed
27 28 29 30 31 32 33
  const BOT_MSG = [{
    _id: uuid.v4(),
    text: `Hi! I am the FAQ bot 🤖 from TEST.\n\nHow may I help you with today?`,
    createdAt: new Date(),
    user: BOT_USER
  }]

Malith Anjana's avatar
Malith Anjana committed
34
  useEffect(() => {
Malith Anjana's avatar
Malith Anjana committed
35
    getChatForUser();
Malith Anjana's avatar
Malith Anjana committed
36 37
  }, [])

Malith Anjana's avatar
Malith Anjana committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
const getChatForUser=async() => {
    try{
      const res = await getChats(USER._id)
      const filter1 = res.data[0].events

      //Filter the User and Bot messaages
      const chats = filter1.filter((n)=>{
        return (n.event == "user" || n.event=="bot") && n
      }) 
      console.log(chats)
      let conv = [];
      chats.map((c)=>{
        if(c.event=="bot"){
          const chat = {
           ...c,
           _id:uuid.v4(),
           createdAt:new Date(unixConvertion(c.timestamp)),
           user: BOT_USER
           }
           conv.push(chat)
        }
        else{
          const chat = {
            ...c,
            _id:uuid.v4(),
            createdAt:new Date(unixConvertion(c.timestamp)),
            user:USER
          }
          conv.push(chat)
        }
      }
      )
      console.log(conv)
      setMessages(conv.concat(BOT_MSG));
    }
    catch(err){
      console.log(err)
    }
  }


Malith Anjana's avatar
Malith Anjana committed
79 80 81 82
  const onSend = useCallback(async(msg = []) => {
    console.log(msg[0].text);
    setMessages(previousMessages => (GiftedChat.append(previousMessages, msg)));
    try{
Malith Anjana's avatar
Malith Anjana committed
83 84
      const req = {message:msg[0].text, sender: USER._id}
      const res = await sendToRasa(req);
Malith Anjana's avatar
Malith Anjana committed
85 86 87 88
      let reply = [];
      res.data.map((d)=>{
        console.log(d.text);
         const rp = {
Malith Anjana's avatar
Malith Anjana committed
89
          _id:uuid.v4(),
Malith Anjana's avatar
Malith Anjana committed
90 91
          text: d.text,
          createdAt: new Date(),
Malith Anjana's avatar
Malith Anjana committed
92
          user: BOT_USER
Malith Anjana's avatar
Malith Anjana committed
93 94 95 96 97 98 99 100 101 102 103
          }
          reply.push(rp)
      }
      )
      setMessages(prev=> prev.concat(reply));
    }
    catch(err){
      console.log(err)
    }
  }, [])

Malith Anjana's avatar
Malith Anjana committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

  //Customize bubbles
  const renderBubble = (props)=>{
    return(
    <Bubble
      {...props}
      wrapperStyle={{
        right:{
          backgroundColor: COLOR.primary
        },
        left:{
          backgroundColor: COLOR.white
        }
      }}
      textStyle={{
        right:{
          fontFamily:FONT.Regular,
          color:COLOR.white,
          fontSize:14,
        },
        left:{
          fontFamily:FONT.Regular,
          fontSize:14,
          color:COLOR.greyFont
        }
      }}
    />
    )
  }


  //Change the send button
  const renderSend = (props) => {
    return(
      <Send {...props}>
        <View>
          <MaterialCommunityIcons name='send-circle' size={42} style={{marginBottom:5, marginRight:5}} color={COLOR.primary}/>
        </View>
      </Send>
    )

  }

Malith Anjana's avatar
Malith Anjana committed
147 148 149
  return (
    <View style={{flex:1}}>
    <ChatHeader/>
Malith Anjana's avatar
Malith Anjana committed
150
    <AlanView projectid={'900e11c36e836f117bff78fe3fac34872e956eca572e1d8b807a3e2338fdd0dc/stage'}/>
Malith Anjana's avatar
Malith Anjana committed
151 152 153
    <GiftedChat
      messages={messages.reverse()}
      onSend={msg => onSend(msg)}
Malith Anjana's avatar
Malith Anjana committed
154 155 156 157 158 159
      user={USER}
      alwaysShowSend={true}
      renderAvatarOnTop={true}
      renderBubble={renderBubble}
      renderSend={renderSend}
      scrollToBottom
Malith Anjana's avatar
Malith Anjana committed
160 161 162 163
    />
    </View>
  )
}