Have you ever wanted to build an AI chatbot that remembers what users said earlier and responds intelligently just like a human? You’re not alone. 

In this blog, I’ll show you how to create exactly that using three powerful tools: 

Whether you’re working in healthcare, e-commerce, IT support, or just experimenting with AI, this approach will help you build a chatbot that feels truly conversational. 

🧰 What You’ll Need 

Before we start, make sure you have: 

🗃️ Step 1: Store Chat History (Any Database Works) 

To generate accurate, personalized responses, we first need to store past conversations. In this example, we’re using Azure Cosmos DB, but you can use any database that Power Automate can connect to — like Azure SQL, Dataverse, or even SharePoint

For Cosmos DB

  1. In the Azure Portal, create a new Cosmos DB for NoSQL account. 
  1. Add a database (e.g., ChatDB) and a container (e.g., ChatHistory). 
  1. Each message gets saved as a document. Here’s an example format: 

    Json: 

  “id”: “msg001”, 

  “userId”: “user123”, 

  “timestamp”: “2025-06-24T10:30:00Z”, 

  “message”: “I want to reschedule my appointment”, 

  “sender”: “user” 

You can group messages by userId or even use a sessionId if you want to track multiple conversations. 

🔁 Step 2: Create a Power Automate Flow 

Now let’s connect everything using Power Automate. 

  1. Go to Power Automate and create a new Instant Cloud Flow (or Automated Flow if you’re triggering it based on an event). 
  1. Add a trigger – this could be from Power Apps, an HTTP request, or even a chatbot message. 
  1. Next, add the Cosmos DB – Get Items action: 

Sql: 

SELECT TOP 5 * FROM c WHERE c.userId = ‘user123’ ORDER BY c.timestamp DESC 

  1. Add a Select action to format the results into a readable string like this: 

User: I want to reschedule my appointment. 

Bot: Sure, please share a preferred date. 

User: Tomorrow morning. 

This will be the conversation history you send to OpenAI. 

🧠 Step 3: Call Azure OpenAI for a Response 

Now it’s time to get that smart reply from GPT-4. 

  1. Add a new HTTP action in Power Automate. 
  1. Fill it in like this: 

https://.openai.azure.com/openai/deployments//chat/completions?api-version=2024-03-01-preview

Json

  “Content-Type”: “application/json”, 

  “api-key”: “<your-openai-api-key>” 

Json

  “messages”: [ 

    { 

      “role”: “system”, 

      “content”: “You are a helpful assistant. Be friendly and respond clearly.” 

    }, 

    { 

      “role”: “user”, 

      “content”: “User: I want to reschedule my appointment.\nBot: Sure, please share a preferred date.\nUser: Tomorrow morning.” 

    } 

  ], 

  “temperature”: 0.7, 

  “max_tokens”: 400 

  1. Add a Parse JSON step to read the response and extract just the message part (usually found in choices[0].message.content). 

💬 Step 4: Return or Store the Response 

You can now use this response however you like: 

Example of how the bot reply would look in Cosmos DB: 

Json: 

  “userId”: “user123”, 

  “message”: “Your appointment is rescheduled for tomorrow at 10 AM.”, 

  “sender”: “bot”, 

  “timestamp”: “2025-06-24T10:35:00Z” 

🧠 Bonus Tips 

🌍 Where Can You Use This? 

This setup isn’t just for one industry — it can be used anywhere: 

Once your chat has memory, the possibilities really open up. 

🏁 Conclusion 

And there you have it! You just learned how to: 
✅ Store chat history in any DB 
✅ Use Power Automate to retrieve that history 
✅ Call Azure OpenAI to generate a relevant response 
✅ Return or store that response for ongoing conversations 

This kind of integration brings real conversational intelligence into your apps and it’s easier to build than ever before. 

If you’re still finding it tricky or would like some help setting this up, feel free to reach out we’d be happy to work with you and build it together. Whether it’s customizing it for your business, integrating with your existing systems, or scaling it further we’ve got you covered.