Files
pos-system/services/mkt-facebook-service-net
..

Marketing Facebook Service - Dịch Vụ Marketing Facebook

EN: Facebook Messenger Marketing Service with Automation Chatbot, AI-Powered Chatbot, and Customer Management
VI: Dịch vụ Marketing Facebook Messenger với Chatbot Tự Động, Chatbot AI, và Quản Lý Khách Hàng

.NET Version License

Features / Tính Năng

1. 🤖 CHATBOT Automation Messenger

EN: Rules-based chatbot with predefined conversation flows

  • Visual flow builder với nodes (Message, Question, Condition, Action)
  • Keyword triggers và postback handlers
  • Quick replies và button templates
  • Multi-language support
  • A/B testing cho flows

VI: Chatbot dựa trên quy tắc với luồng hội thoại định sẵn

  • Trình tạo flow trực quan với các nodes (Message, Question, Condition, Action)
  • Triggers theo từ khóa và xử lý postback
  • Quick replies và button templates
  • Hỗ trợ đa ngôn ngữ
  • A/B testing cho các flows

2. 🧠 CHATBOT AI Messenger

EN: Intelligent chatbot powered by OpenAI/Azure OpenAI

  • Natural language understanding và generation
  • Context-aware conversations
  • Customizable system prompts
  • Multi-turn dialogue support
  • Fallback to automation rules
  • Cost tracking và optimization

VI: Chatbot thông minh được hỗ trợ bởi OpenAI/Azure OpenAI

  • Hiểu và sinh ngôn ngữ tự nhiên
  • Hội thoại theo ngữ cảnh
  • Tùy chỉnh system prompts
  • Hỗ trợ đối thoại nhiều lượt
  • Fallback về automation rules
  • Theo dõi chi phí và tối ưu hóa

3. 👥 Quản Lý Khách Hàng / Customer Management

EN: Comprehensive customer data management

  • Customer profiles với Facebook user info
  • Conversation history tracking
  • Custom fields và tags
  • Segmentation by behavior
  • Export customer data
  • GDPR compliance

VI: Quản lý dữ liệu khách hàng toàn diện

  • Hồ sơ khách hàng với thông tin Facebook user
  • Theo dõi lịch sử hội thoại
  • Custom fields và tags
  • Phân khúc theo hành vi
  • Xuất dữ liệu khách hàng
  • Tuân thủ GDPR

Quick Start / Bắt Đầu Nhanh

Prerequisites / Yêu Cầu

  • .NET 8.0 SDK
  • PostgreSQL 15+ (hoặc Neon serverless)
  • Redis 7+
  • RabbitMQ 3.12+
  • Docker \u0026 Docker Compose (cho local development)
  • Facebook PageFacebook App với Messenger Platform
  • OpenAI API Key hoặc Azure OpenAI (cho AI Chatbot)

Configuration / Cấu Hình

# EN: Copy environment template / VI: Sao chép template môi trường
cp .env.example .env

# EN: Update configuration / VI: Cập nhật cấu hình
# - DATABASE_URL (PostgreSQL connection string)
# - REDIS_URL
# - RABBITMQ_URL
# - FACEBOOK_VERIFY_TOKEN (webhook verification)
# - FACEBOOK_PAGE_ACCESS_TOKEN (send messages)
# - OPENAI_API_KEY (AI chatbot)

Local Development / Phát Triển Local

# EN: Restore dependencies / VI: Khôi phục dependencies
dotnet restore

# EN: Apply migrations / VI: Áp dụng migrations
dotnet ef database update --project src/MktFacebookService.Infrastructure

# EN: Run service / VI: Chạy service
dotnet run --project src/MktFacebookService.API

# Service available at http://localhost:5000
# Swagger UI: http://localhost:5000/swagger

Docker Deployment / Triển Khai Docker

# EN: Build and run with Docker Compose / VI: Build và chạy với Docker Compose
cd deployments/local
docker-compose up -d mkt-facebook-service-net

# EN: View logs / VI: Xem logs
docker-compose logs -f mkt-facebook-service-net

# EN: Access via Traefik / VI: Truy cập qua Traefik
# http://localhost/api/v1/mkt-facebook

Architecture / Kiến Trúc

graph TD
    subgraph "External / Bên Ngoài"
        FB[Facebook Messenger Platform]
        OAI[OpenAI / Azure OpenAI]
        User[End User on Messenger]
    end
    
    subgraph "mkt-facebook-service-net"
        WH[Webhooks Controller]
        API[REST API Controllers]
        APP[Application Layer CQRS]
        DOM[Domain Layer]
        INFRA[Infrastructure Layer]
    end
    
    subgraph "Data Stores"
        PG[(PostgreSQL)]
        RD[(Redis Cache)]
        RMQ[RabbitMQ]
    end
    
    User -->|Send Message| FB
    FB -->|Webhook Event| WH
    WH -->|Process Message Command| APP
    API -->|Commands/Queries| APP
    APP -->|Business Logic| DOM
    APP -->|Persistence| INFRA
    INFRA -->|EF Core| PG
    INFRA -->|Caching| RD
    INFRA -->|Events| RMQ
    APP -->|Send Message| INFRA
    INFRA -->|API Call| FB
    APP -->|AI Completion| INFRA
    INFRA -->|API Call| OAI

Technology Stack / Công Nghệ

Layer Technology / Công Nghệ
Backend .NET 8, ASP.NET Core 8
Architecture Clean Architecture, DDD, CQRS
Database PostgreSQL 15+, EF Core 8
Cache Redis, StackExchange.Redis
Messaging RabbitMQ, MassTransit
HTTP Client HttpClient Factory, Polly
AI OpenAI SDK, Azure.AI.OpenAI
API Gateway Traefik
Documentation Swagger/OpenAPI 3.0

API Endpoints

Public API

Endpoint Method Description / Mô Tả
/api/v1/webhooks/facebook GET Facebook webhook verification
/api/v1/webhooks/facebook POST Receive Facebook Messenger events
/api/v1/conversations GET List conversations / Danh sách hội thoại
/api/v1/conversations/{id} GET Get conversation by ID
/api/v1/conversations/{id}/messages POST Send message / Gửi tin nhắn
/api/v1/customers GET List customers / Danh sách khách hàng
/api/v1/customers/{id} GET Customer details
/api/v1/customers/{id} PUT Update customer info

Admin API

Endpoint Method Description / Mô Tả
/api/v1/chatbots/flows GET List automation flows
/api/v1/chatbots/flows POST Create chatbot flow
/api/v1/chatbots/flows/{id} PUT Update flow
/api/v1/chatbots/flows/{id} DELETE Delete flow
/api/v1/chatbots/ai-config GET Get AI chatbot config
/api/v1/chatbots/ai-config PUT Update AI config

Health \u0026 Monitoring

Endpoint Description / Mô Tả
/health Health check endpoint
/swagger Swagger UI documentation

Documentation / Tài Liệu

Project Structure / Cấu Trúc Dự Án

mkt-facebook-service-net/
├── src/
│   ├── MktFacebookService.API/           # API Layer
│   │   ├── Controllers/                  # REST Controllers
│   │   ├── Application/                  # CQRS Commands/Queries
│   │   └── Program.cs                    # Entry point
│   ├── MktFacebookService.Domain/        # Domain Layer
│   │   ├── AggregatesModel/              # Aggregates, Entities
│   │   │   ├── ConversationAggregate/
│   │   │   ├── CustomerAggregate/
│   │   │   └── ChatbotAggregate/
│   │   └── SeedWork/                     # Base classes
│   └── MktFacebookService.Infrastructure/ # Infrastructure Layer
│       ├── Data/                         # EF Core DbContext
│       ├── Repositories/                 # Repository implementations
│       └── ExternalServices/             # Facebook, OpenAI clients
├── tests/
│   ├── MktFacebookService.UnitTests/
│   └── MktFacebookService.FunctionalTests/
├── docs/                                 # Documentation
├── Dockerfile
└── MktFacebookService.slnx

Environment Variables / Biến Môi Trường

Variable Description / Mô Tả Default
DATABASE_URL PostgreSQL connection string -
REDIS_URL Redis connection string localhost:6379
RABBITMQ_URL RabbitMQ connection string localhost:5672
FACEBOOK_VERIFY_TOKEN Webhook verification token -
FACEBOOK_PAGE_ACCESS_TOKEN Page access token for sending messages -
FACEBOOK_API_VERSION Facebook API version v21.0
OPENAI_API_KEY OpenAI API key -
OPENAI_MODEL OpenAI model name gpt-4-turbo
AZURE_OPENAI_ENDPOINT Azure OpenAI endpoint (optional) -
ASPNETCORE_ENVIRONMENT Environment (Development/Production) Development

Development Workflow / Quy Trình Phát Triển

Phase 1: Domain Layer

  1. Tạo Aggregates (Conversation, Customer, ChatbotFlow, AIChatbotConfig)
  2. Định nghĩa Entities và Value Objects
  3. Implement business rules trong domain methods
  4. Raise Domain Events

Phase 2: Infrastructure Layer

  1. Setup EF Core DbContext và entity configurations
  2. Create repositories implementing domain interfaces
  3. Implement FacebookMessengerClient
  4. Implement OpenAIClient với Polly resilience
  5. Apply database migrations

Phase 3: Application Layer (CQRS)

  1. Create Commands (ProcessIncomingMessage, SendMessage, CreateChatbotFlow)
  2. Create Queries (GetConversations, GetCustomers)
  3. Implement MediatR Handlers
  4. Add FluentValidation validators
  5. Setup Pipeline Behaviors

Phase 4: API Layer

  1. Implement WebhooksController cho Facebook events
  2. Create REST API Controllers
  3. Add Swagger annotations
  4. Configure authentication \u0026 authorization
  5. Setup health checks

Testing / Kiểm Thử

# EN: Run all tests / VI: Chạy tất cả tests
dotnet test

# EN: Run unit tests only / VI: Chỉ chạy unit tests
dotnet test tests/MktFacebookService.UnitTests/

# EN: Run with coverage / VI: Chạy với coverage
dotnet test /p:CollectCoverage=true /p:CoverageReportsFormat=opencover

Contributing / Đóng Góp

Please follow the GoodGo Project Rules and .NET Microservice Workflow.

License

MIT License

Resources / Tài Nguyên

Facebook Messenger Platform

OpenAI

Internal Skills