Friday, April 17, 2026

📱Build WhatsApp Bot using .NET + OpenAI

  In this article, you will learn how to build a real-world WhatsApp chatbot using ASP.NET Core and OpenAI. This bot can automatically reply to customer messages and can be turned into a SaaS product.


🚀 What You Will Build

  • WhatsApp webhook API
  • AI auto-reply system
  • OpenAI integration
  • Real-time message processing

📌 Prerequisites

  • ASP.NET Core Web API
  • OpenAI API Key
  • WhatsApp Business API (Meta Cloud API or Twilio)

1. Setup WhatsApp Cloud API

Go to Meta Developer Portal and create WhatsApp Business app.


https://developers.facebook.com/

Get:

  • Access Token
  • Phone Number ID
  • Webhook Verify Token

2. Create ASP.NET Core Web API


dotnet new webapi -n WhatsAppBot

3. Configure appsettings.json


"WhatsApp": {
  "AccessToken": "YOUR_TOKEN",
  "PhoneNumberId": "YOUR_PHONE_ID"
},
"OpenAI": {
  "ApiKey": "YOUR_OPENAI_KEY"
}

4. Create OpenAI Service


public class OpenAIService
{
    private readonly HttpClient _http;

    public OpenAIService(HttpClient http)
    {
        _http = http;
    }

    public async Task<string> GetReply(string message, string apiKey)
    {
        var request = new
        {
            model = "gpt-4o-mini",
            messages = new[]
            {
                new { role = "user", content = message }
            }
        };

        _http.DefaultRequestHeaders.Authorization =
            new AuthenticationHeaderValue("Bearer", apiKey);

        var response = await _http.PostAsJsonAsync(
            "https://api.openai.com/v1/chat/completions", request);

        dynamic result = await response.Content.ReadFromJsonAsync<dynamic>();

        return result.choices[0].message.content;
    }
}

5. Create WhatsApp Webhook Controller


[ApiController]
[Route("api/webhook")]
public class WebhookController : ControllerBase
{
    private readonly IConfiguration _config;
    private readonly OpenAIService _ai;
    private readonly HttpClient _http;

    public WebhookController(IConfiguration config, OpenAIService ai, IHttpClientFactory factory)
    {
        _config = config;
        _ai = ai;
        _http = factory.CreateClient();
    }

    [HttpGet]
    public IActionResult Verify(
        [FromQuery] string hub_mode,
        [FromQuery] string hub_verify_token,
        [FromQuery] string hub_challenge)
    {
        if (hub_verify_token == "my_verify_token")
            return Ok(hub_challenge);

        return Unauthorized();
    }

    [HttpPost]
    public async Task Receive([FromBody] dynamic data)
    {
        try
        {
            var message = data.entry[0].changes[0].value.messages[0].text.body;
            var from = data.entry[0].changes[0].value.messages[0].from;

            var reply = await _ai.GetReply(message, _config["OpenAI:ApiKey"]);

            await SendWhatsAppMessage(from, reply);

            return Ok();
        }
        catch
        {
            return Ok();
        }
    }

    private async Task SendWhatsAppMessage(string to, string message)
    {
        var url = $"https://graph.facebook.com/v18.0/{_config["WhatsApp:PhoneNumberId"]}/messages";

        var payload = new
        {
            messaging_product = "whatsapp",
            to = to,
            text = new { body = message }
        };

        var request = new HttpRequestMessage(HttpMethod.Post, url);
        request.Headers.Authorization =
            new AuthenticationHeaderValue("Bearer", _config["WhatsApp:AccessToken"]);

        request.Content = JsonContent.Create(payload);

        await _http.SendAsync(request);
    }
}

6. Register Services


builder.Services.AddHttpClient<OpenAIService>();
builder.Services.AddHttpClient();

7. Test Flow

  • User sends WhatsApp message
  • Webhook receives message
  • OpenAI generates reply
  • Bot sends reply back

💡 Real Use Cases

  • Customer support automation
  • Order tracking bot
  • FAQ chatbot
  • Lead generation system

💰 Monetization Idea (IMPORTANT)

Turn this into SaaS:

  • Charge ₹499/month per business
  • Provide dashboard
  • Add analytics
  • Multi-business support

🔐 Best Practices

  • Validate webhook requests
  • Use logging (Serilog)
  • Store chat history
  • Add rate limiting
  • Use retry policy

📌 Conclusion

You now have a real-world WhatsApp AI bot using ASP.NET Core and OpenAI. This can be scaled into a powerful business product.

Sunday, April 12, 2026

Integrating OpenAI / ChatGPT in ASP.NET Core Web API

 This guide shows how to build a production-ready AI API using ASP.NET Core with: Clean Architecture, streaming responses, database storage, and secure configuration.


🏗️ Architecture Overview


API Layer → Application Layer → Infrastructure Layer → Database
  • API: Controllers
  • Application: Business logic
  • Infrastructure: OpenAI + DB
  • Database: Chat history

1. Secure Configuration (Environment Variable)


builder.Configuration.AddEnvironmentVariables();

var apiKey = builder.Configuration["OPENAI_API_KEY"];

⚠️ Never store API keys in appsettings.json in production.


2. Database Model (Chat History)


public class ChatMessage
{
    public int Id { get; set; }
    public string UserMessage { get; set; }
    public string BotResponse { get; set; }
    public DateTime CreatedAt { get; set; }
}

3. DbContext (EF Core)


public class AppDbContext : DbContext
{
    public DbSet<ChatMessage> ChatMessages { get; set; }

    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options) { }
}

4. OpenAI Service (Production Ready)


public class OpenAIService
{
    private readonly HttpClient _httpClient;
    private readonly IConfiguration _config;

    public OpenAIService(HttpClient httpClient, IConfiguration config)
    {
        _httpClient = httpClient;
        _config = config;
    }

    public async Task<string> GetResponseAsync(string prompt)
    {
        var request = new
        {
            model = "gpt-4o-mini",
            messages = new[]
            {
                new { role = "user", content = prompt }
            }
        };

        _httpClient.DefaultRequestHeaders.Authorization =
            new AuthenticationHeaderValue("Bearer", _config["OPENAI_API_KEY"]);

        var response = await _httpClient.PostAsJsonAsync(
            "https://api.openai.com/v1/chat/completions", request);

        if (!response.IsSuccessStatusCode)
            throw new Exception("OpenAI API error");

        var result = await response.Content.ReadFromJsonAsync<dynamic>();
        return result.choices[0].message.content;
    }
}

5. Streaming Response (Real-Time)


[HttpPost("stream")]
public async Task Stream([FromBody] string prompt)
{
    Response.ContentType = "text/plain";

    var response = await _service.GetResponseAsync(prompt);

    foreach (var word in response.Split(" "))
    {
        await Response.WriteAsync(word + " ");
        await Response.Body.FlushAsync();
        await Task.Delay(50);
    }
}

✅ Simulates real-time typing (like ChatGPT)


6. Save Chat History


public async Task SaveChat(string userMsg, string botMsg)
{
    var chat = new ChatMessage
    {
        UserMessage = userMsg,
        BotResponse = botMsg,
        CreatedAt = DateTime.UtcNow
    };

    _context.ChatMessages.Add(chat);
    await _context.SaveChangesAsync();
}

7. Controller (Final Version)


[ApiController]
[Route("api/chat")]
public class ChatController : ControllerBase
{
    private readonly OpenAIService _service;
    private readonly AppDbContext _context;

    public ChatController(OpenAIService service, AppDbContext context)
    {
        _service = service;
        _context = context;
    }

    [HttpPost]
    public async Task<IActionResult> Ask(string prompt)
    {
        var response = await _service.GetResponseAsync(prompt);

        await _context.ChatMessages.AddAsync(new ChatMessage
        {
            UserMessage = prompt,
            BotResponse = response,
            CreatedAt = DateTime.UtcNow
        });

        await _context.SaveChangesAsync();

        return Ok(response);
    }
}

8. Logging (Serilog)


builder.Host.UseSerilog((ctx, lc) =>
    lc.WriteTo.Console()
);

9. Rate Limiting


builder.Services.AddRateLimiter(options =>
{
    options.AddFixedWindowLimiter("api", opt =>
    {
        opt.PermitLimit = 50;
        opt.Window = TimeSpan.FromMinutes(1);
    });
});

🔐 Enterprise Best Practices

  • Use Redis for caching responses
  • Store chat history per user
  • Use JWT authentication
  • Add retry policy (Polly)
  • Use Azure Key Vault
  • Implement circuit breaker

💡 Real Product Ideas

  • AI Customer Support API
  • WhatsApp Auto Reply Bot
  • AI Code Review Tool
  • Content Generator SaaS

📌 Conclusion

You now have a complete enterprise-grade AI API using ASP.NET Core. This can be extended into a scalable SaaS product.

How to Improve API Performance in ASP.NET Core

Performance is critical for modern APIs. A slow API leads to poor user experience and scalability issues. In this article, we will explore proven techniques to improve API performance in ASP.NET Core with real examples.


1. Use Asynchronous Programming

Always use async/await for I/O operations to avoid blocking threads.


[HttpGet]
public async Task GetUsers()
{
    var users = await _context.Users.ToListAsync();
    return Ok(users);
}

Benefit: Improves scalability and thread utilization.


2. Enable Response Caching

Caching reduces repeated processing and database calls.


[HttpGet]
[ResponseCache(Duration = 60)]
public IActionResult GetData()
{
    return Ok("Cached Data");
}

Also enable in Program.cs:


builder.Services.AddResponseCaching();

app.UseResponseCaching();


3. Use In-Memory Caching


public class UserService
{
    private readonly IMemoryCache _cache;

    public UserService(IMemoryCache cache)
    {
        _cache = cache;
    }

    public async Task> GetUsers()
    {
        if (!_cache.TryGetValue("users", out List users))
        {
            users = await _context.Users.ToListAsync();

            _cache.Set("users", users, TimeSpan.FromMinutes(5));
        }

        return users;
    }
}


4. Optimize Database Queries

Use projection instead of fetching full entities.


var users = await _context.Users
    .Select(u => new { u.Id, u.Name })
    .ToListAsync();

Use AsNoTracking() for read-only queries:


var users = await _context.Users
    .AsNoTracking()
    .ToListAsync();


5. Use Pagination


[HttpGet]
public async Task GetUsers(int page = 1, int pageSize = 10)
{
    var users = await _context.Users
        .Skip((page - 1) * pageSize)
        .Take(pageSize)
        .ToListAsync();

    return Ok(users);
}


6. Enable Compression

Reduces response size.


builder.Services.AddResponseCompression();

app.UseResponseCompression();


7. Use Minimal APIs (Faster Execution)


app.MapGet("/users", async (AppDbContext db) =>
{
    return await db.Users.ToListAsync();
});


8. Avoid Blocking Calls

❌ Bad Practice:

var data = _service.GetData().Result;

✅ Good Practice:

var data = await _service.GetData();

9. Use Connection Pooling

Configured automatically in EF Core, but ensure proper DB connection string usage.


"ConnectionStrings": {
  "DefaultConnection": "Server=.;Database=TestDb;Trusted_Connection=True;Pooling=true;"
}


10. Use Logging & Monitoring

Use tools like Application Insights or Serilog.


builder.Logging.ClearProviders();
builder.Logging.AddConsole();


11. Use Rate Limiting


builder.Services.AddRateLimiter(options =>
{
    options.AddFixedWindowLimiter("fixed", opt =>
    {
        opt.PermitLimit = 100;
        opt.Window = TimeSpan.FromMinutes(1);
    });
});

app.UseRateLimiter();


12. Use Background Tasks for Heavy Work

Offload heavy tasks using background services.


public class BackgroundJob : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            // Background work
            await Task.Delay(1000);
        }
    }
}


📌 Conclusion

Improving API performance in ASP.NET Core requires a combination of best practices:

  • Use async programming
  • Apply caching strategies
  • Optimize database queries
  • Enable compression
  • Avoid unnecessary data processing

By applying these techniques, you can significantly improve your API speed, scalability, and user experience.

📱Build WhatsApp Bot using .NET + OpenAI

   In this article, you will learn how to build a real-world WhatsApp chatbot using ASP.NET Core and OpenAI. This bot can automatically repl...