Bot reading Data from Mongo DB

Hi ,

Can anyone help me with an example where Rasa is connected to Mongo Db for reading Data . Suppose we have an employee table with all the details, we need the Bot to read the data from there and utter to the user

Thanks in advance. :slight_smile:

Hi @pausali.biswas! Are you talking about saving conversations to MongoDB?

No, I am talking about reading data from Mongo DB for utter to the user

@pausali.biswas

You could manage assistant utterances using an external content message system, setting up an external HTTP server that uses MongoDB for storage. It looks like MongoDB recommends using Sleepy Mongoose (Python).

You could also write custom actions that call a REST API using the requests library to retrieve and set the response text from your MongoDB database. It looks like MongoDB recommends using Eve (Python).

Yes i understand, do you have any sample projects or code for this, that will be of great help as i am a newbie in this. :slight_smile:

@pausali.biswas It looks like Eve has a demo that will help you understand how to set up a REST API for your MongoDB instance.

Then you should be able to use the requests library to call this API from a custom action in order to retrieve data from your database (docs and example).

look at My code I am loading data from mongoDB

from typing import Any, Text, Dict, List
from pymongo.database import Database
from pymongo import MongoClient
from rasa_sdk import Action, Tracker 
from rasa_sdk.executor import CollectingDispatcher
import pymongo
# url="http://localhost:3000/api"
class CarAction(Action):
    def name(self):
        return "carAction"
    def run(self, dispatcher, tracker, domain):
        client = pymongo.MongoClient("localhost", 27017)
        db=client.sample
        res = db.datas.find({'action':'CarAction'})
        print(type(res))
        for i in res:
            dispatcher.utter_button_message(i['text'],i['buttons'])
        return []

I am using pymongo

this is for load data from API

import requests

import json

from typing import Any, Text, Dict, List

from pymongo.database import Database

from pymongo import MongoClient
from rasa_sdk import Action, Tracker
class NormalCarAction(Action):
    def name(self):
        return "normalCarAction"
    def run(self, dispatcher, tracker, domain):
        intent = tracker.latest_message['intent'].get('name')
        request = requests.get(url+"?action=normalCarAction").json()  
        if(len(request)>0):
            dispatcher.utter_button_message(request[0]['text'],request[0]['buttons'])
        else:
            dispatcher.utter_message("Try Again");
        return []

i want to fetch data from mysql database and display to user,but when i do that it diplays as json format but i want it in a list how can i do that