Added Argo Manifests

This commit is contained in:
Corrado Mulas
2026-06-19 11:33:42 +02:00
parent 2773db9e88
commit f62a24ecec
13 changed files with 236 additions and 47 deletions

View File

@@ -277,7 +277,6 @@ Make sure these are set in your `.env` file:
```bash
TELETHON_API_ID=your_api_id
TELETHON_API_HASH=your_api_hash
TELEGRAM_BOT_TOKEN=your_bot_token
TELETHON_TOKEN=your_session_token
MONGODB_URI=mongodb://localhost:27017
@@ -328,7 +327,6 @@ services:
environment:
TELETHON_API_ID: ${TELETHON_API_ID}
TELETHON_API_HASH: ${TELETHON_API_HASH}
TELEGRAM_BOT_TOKEN: ${TELEGRAM_BOT_TOKEN}
TELETHON_TOKEN: ${TELETHON_TOKEN}
MONGODB_URI: mongodb://mongo:27017
MONGODB_DATABASE: groupfactory

View File

@@ -205,7 +205,6 @@ The telegram-groupfactory bot now has a complete **admin-only configuration syst
# Telegram
TELETHON_API_ID=your_api_id
TELETHON_API_HASH=your_api_hash
TELEGRAM_BOT_TOKEN=your_bot_token
TELETHON_TOKEN=your_session_token
# MongoDB

View File

@@ -1,6 +1,6 @@
# telegram-groupfactory
A Telegram bot for managing user groups with MongoDB backend and admin-only configuration.
A Telegram user API service for managing groups with MongoDB backend and admin-only configuration.
## Features
@@ -10,7 +10,7 @@ A Telegram bot for managing user groups with MongoDB backend and admin-only conf
- ✅ GroupHelp settings backup QR storage for `.importbackup`
- ✅ Interactive admin role selection for group creators
- ✅ Modular architecture with separation of concerns
- ✅ Telegram bot integration using Telethon
- ✅ Telegram user API integration using Telethon
## Architecture
@@ -31,7 +31,6 @@ The application follows a modular architecture with the following components:
```
TELETHON_API_ID=your_api_id
TELETHON_API_HASH=your_api_hash
TELEGRAM_BOT_TOKEN=your_bot_token
TELETHON_TOKEN=your_session_token
MONGODB_URI=mongodb://localhost:27017
@@ -42,6 +41,15 @@ The application follows a modular architecture with the following components:
FACTORY_BOT_ID=your_bot_id
```
Generate `TELETHON_TOKEN` with:
```bash
TELETHON_API_ID=your_api_id TELETHON_API_HASH=your_api_hash \
python3 scripts/generate_telegram_session.py
```
The script performs a Telegram user login and prints the `TELETHON_TOKEN`
value to store in the Kubernetes Secret.
2. Install dependencies:
```bash
pip install -r requirements.txt

18
argocd/application.yaml Normal file
View File

@@ -0,0 +1,18 @@
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: groupfactory
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/YOUR_ORG/telegram-groupfactory.git
targetRevision: main
path: k8s
destination:
server: https://kubernetes.default.svc
namespace: groupfactory
syncPolicy:
syncOptions:
- CreateNamespace=true
- PruneLast=true

View File

@@ -1,35 +0,0 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: groupfactory
namespace: groupfactory
labels:
app: groupfactory
spec:
replicas: 1
selector:
matchLabels:
app: groupfactory
template:
metadata:
labels:
app: groupfactory
spec:
containers:
- name: groupfactory
image: <registry-url>/groupfactory:latest
envFrom:
- configMapRef:
name: telegram-config
- secretRef:
name: groupfactory-tg-auth
env:
- name: MONGODB_URI
valueFrom:
secretKeyRef:
name: groupfactory-tg-auth
key: MONGODB_URI
- name: MONGODB_DATABASE
value: "imsuserbot"
- name: MONGODB_COLLECTION
value: "ghconfig"

16
examples/configmap.yaml Normal file
View File

@@ -0,0 +1,16 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: telegram-config
namespace: groupfactory
labels:
app.kubernetes.io/name: groupfactory
data:
API_HOST: "0.0.0.0"
API_PORT: "8000"
LOG_LEVEL: "INFO"
MONGODB_DATABASE: "imsuserbot"
MONGODB_COLLECTION: "ghconfig"
TELETHON_API_ID: "123456"
STAFF_CHAT_ID: "-1001234567890"
FACTORY_BOT_ID: "123456789"

14
examples/secret.yaml Normal file
View File

@@ -0,0 +1,14 @@
apiVersion: v1
kind: Secret
metadata:
name: groupfactory-tg-auth
namespace: groupfactory
labels:
app.kubernetes.io/name: groupfactory
type: Opaque
stringData:
API_KEY: "change-me-api-key"
MONGODB_URI: "mongodb://mongo:27017"
TELETHON_API_HASH: "replace-with-telegram-api-hash"
# Generate with: python3 scripts/generate_telegram_session.py
TELETHON_TOKEN: "replace-with-telethon-session-token"

66
k8s/deployment.yaml Normal file
View File

@@ -0,0 +1,66 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: groupfactory
namespace: groupfactory
labels:
app.kubernetes.io/name: groupfactory
app.kubernetes.io/component: app
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: groupfactory
app.kubernetes.io/component: app
template:
metadata:
labels:
app.kubernetes.io/name: groupfactory
app.kubernetes.io/component: app
spec:
securityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
containers:
- name: groupfactory
image: registry.example.com/groupfactory:latest
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8000
envFrom:
- configMapRef:
name: telegram-config
- secretRef:
name: groupfactory-tg-auth
readinessProbe:
httpGet:
path: /health
port: http
initialDelaySeconds: 10
periodSeconds: 10
livenessProbe:
httpGet:
path: /health
port: http
initialDelaySeconds: 30
periodSeconds: 20
startupProbe:
httpGet:
path: /health
port: http
failureThreshold: 30
periodSeconds: 5
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL

17
k8s/service.yaml Normal file
View File

@@ -0,0 +1,17 @@
apiVersion: v1
kind: Service
metadata:
name: groupfactory
namespace: groupfactory
labels:
app.kubernetes.io/name: groupfactory
app.kubernetes.io/component: app
spec:
type: ClusterIP
selector:
app.kubernetes.io/name: groupfactory
app.kubernetes.io/component: app
ports:
- name: http
port: 8000
targetPort: http

View File

@@ -0,0 +1,82 @@
#!/usr/bin/env python3
"""Generate a Telethon StringSession for TELETHON_TOKEN.
Examples:
TELETHON_API_ID=123456 TELETHON_API_HASH=abc123 \
python3 scripts/generate_telegram_session.py
python3 scripts/generate_telegram_session.py \
--api-id 123456 \
--api-hash abc123 \
--phone +391234567890
"""
import argparse
import asyncio
import os
import sys
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Generate a Telethon StringSession for the TELETHON_TOKEN secret."
)
parser.add_argument(
"--api-id",
default=os.environ.get("TELETHON_API_ID"),
help="Telegram API ID. Defaults to TELETHON_API_ID.",
)
parser.add_argument(
"--api-hash",
default=os.environ.get("TELETHON_API_HASH"),
help="Telegram API hash. Defaults to TELETHON_API_HASH.",
)
parser.add_argument(
"--phone",
default=os.environ.get("TELETHON_PHONE"),
help="Telegram user phone number. Defaults to TELETHON_PHONE.",
)
args = parser.parse_args()
if not args.api_id:
parser.error("--api-id or TELETHON_API_ID is required")
try:
args.api_id = int(args.api_id)
except ValueError:
parser.error("--api-id or TELETHON_API_ID must be an integer")
if not args.api_hash:
parser.error("--api-hash or TELETHON_API_HASH is required")
return args
async def generate_session(args: argparse.Namespace) -> str:
from telethon import TelegramClient
from telethon.sessions import StringSession
client = TelegramClient(StringSession(), args.api_id, args.api_hash)
try:
phone = args.phone or input("Telegram phone number, including country code: ")
await client.start(phone=phone)
return client.session.save()
finally:
await client.disconnect()
def main() -> int:
args = parse_args()
print(
"Starting Telegram user login. The final stdout line is the TELETHON_TOKEN value.",
file=sys.stderr,
)
session_string = asyncio.run(generate_session(args))
if not session_string:
print("Failed to generate a session string.", file=sys.stderr)
return 1
print(session_string)
print("Store this value as TELETHON_TOKEN in the Kubernetes Secret.", file=sys.stderr)
return 0
if __name__ == "__main__":
raise SystemExit(main())

View File

@@ -2,11 +2,11 @@ import os
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure
from telethon import TelegramClient
from telethon.sessions import StringSession
# Telegram configuration
TELETHON_TOKEN = os.environ.get("TELETHON_TOKEN")
TELETHON_API_HASH = os.environ.get("TELETHON_API_HASH")
TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN")
TELETHON_API_ID = int(os.environ.get("TELETHON_API_ID", 0))
STAFF_CHAT_ID = int(os.environ.get("STAFF_CHAT_ID", 0))
FACTORY_BOT_ID = int(os.environ.get("FACTORY_BOT_ID", 0))
@@ -19,6 +19,12 @@ MONGODB_COLLECTION = os.environ.get('MONGODB_COLLECTION', '')
# Logging configuration
LOG_LEVEL = os.environ.get('LOG_LEVEL', 'INFO')
def get_telegram_session():
"""Return a Telethon session backed by TELETHON_TOKEN when configured."""
if TELETHON_TOKEN:
return StringSession(TELETHON_TOKEN)
return 'session'
# MongoDB helper functions
def get_mongo_client():
"""Create and return MongoDB client"""
@@ -34,7 +40,7 @@ def get_telegram_client():
"""Create and return Telegram client"""
try:
client = TelegramClient(
'session',
get_telegram_session(),
TELETHON_API_ID,
TELETHON_API_HASH
)
@@ -65,7 +71,7 @@ def load_config():
'telegram': {
'api_id': TELETHON_API_ID,
'api_hash': TELETHON_API_HASH,
'bot_token': TELEGRAM_BOT_TOKEN,
'session': get_telegram_session(),
'session_file': 'session',
'staff_chat_id': STAFF_CHAT_ID,
'factory_bot_id': FACTORY_BOT_ID
@@ -178,4 +184,4 @@ def get_user_admin_role(user_id: int) -> bool:
return False
except Exception as e:
print(f"Failed to get user admin role: {e}")
return False
return False

View File

@@ -44,7 +44,7 @@ async def main():
# Create Telegram client
client = TelegramClient(
config['telegram']['session_file'],
config['telegram']['session'],
config['telegram']['api_id'],
config['telegram']['api_hash']
)
@@ -350,4 +350,4 @@ async def main():
logger.info("Application shutdown complete")
if __name__ == "__main__":
asyncio.run(main())
asyncio.run(main())