winget install ngrok.ngrok; winget install pnpm.pnpm; winget install Python.Python.3.12Nhập Y để chấp nhận
Tạo một terminal mới với quyền Admin và cập nhật ngrok
ngrok updateTạo một project tại đây
Xác thực ngrok theo hướng dẫn và khởi chạy ngrok với cổng 8080
ngrok http 8080Lấy google cloud project id và tên miền ngrok lần lượt dán vào {ID_project} và {Web_URL} vào script ở dưới
provider "google" {
project = "{ID_project}"
}
# 1. Bật các API cần thiết
resource "google_project_service" "services" {
for_each = toset([
"gmail.googleapis.com",
"pubsub.googleapis.com"
])
service = each.key
disable_on_destroy = false
}
# 2. Tạo Pub/Sub Topic
resource "google_pubsub_topic" "gmail_topic" {
name = "gmail-notifications-topic"
depends_on = [google_project_service.services]
}
# 3. Cấp quyền cho hệ thống Gmail push tin nhắn vào Topic
resource "google_pubsub_topic_iam_member" "gmail_publisher" {
topic = google_pubsub_topic.gmail_topic.name
role = "roles/pubsub.publisher"
member = "serviceAccount:gmail-api-push@system.gserviceaccount.com"
}
# 4. Tạo Push Subscription trỏ về Webhook của bạn
resource "google_pubsub_subscription" "gmail_sub" {
name = "gmail-notifications-sub"
topic = google_pubsub_topic.gmail_topic.name
push_config {
push_endpoint = "{Web_URL}/api/webhooks/gmail"
}
}
Vào web này và chạy lệnh sau đây trong cloud shell
cat << 'EOF' > main.tf
{Script_ở_bước_3}
EOFCài terraform trên Google cloud shell
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(grep -oP '(?<=UBUNTU_CODENAME=).*' /etc/os-release || lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
bashKích hoạt service và chạy lệnh sau đây trên cloud shell
terraform init
terraform applyNhập yes để xác nhận
- Đầu tiên, cấu hình OAuth consent screen tại Google Auth Platform và chọn External.
- Tiếp theo, vào trang web. Chọn Create Credentials -> OAuth client ID -> Desktop app. Tải file json và đặt vào thư mục root với tên
credentials.json - Cuối cùng, thêm email vào test user tại đây
Từ thư mục gốc repository, tạo một môi trường ảo riêng cho script:
python -m venv .venv
source .venv/bin/activate # Linux
.venv/Scripts/Activate.ps1 # Windows
python -m pip install -r requirements.txtpython -m backend.helper create-user --name "Nguyen Viet Anh" --username vanh --password test.com.vn --email anhvietng62@gmail.comTruyền giá trị google cloud project id và 2 địa chỉ email tương ứng vào .env trước khi khởi chạy ứng dụng
pubsub_oidc_topic=projects/{ID_project}/topics/gmail-notifications-topic
manager_email={Email_người_quản_lý}
sender_email={Email_hệ_thống} Biên dịch frontend trước khi chạy lệnh
cd frontend
pnpm build
cd ..python -m backend.app.mainpython -m "script.send&recieve" --help