First commit
This commit is contained in:
31
apps/integration-layer/README.md
Normal file
31
apps/integration-layer/README.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# Integration Layer CLI
|
||||
|
||||
This package provides a command-line interface (CLI) to interact with the integration layer API deployed on Cloud Run.
|
||||
|
||||
## Installation
|
||||
|
||||
Install the package and its dependencies using `uv`:
|
||||
|
||||
```bash
|
||||
uv pip install -e .
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
The CLI provides two main commands: `send` and `chat`.
|
||||
|
||||
### `send`
|
||||
|
||||
Sends a single message to the API.
|
||||
|
||||
```bash
|
||||
int-layer send "My message" --telefono "1234567890"
|
||||
```
|
||||
|
||||
### `chat`
|
||||
|
||||
Starts an interactive chat session.
|
||||
|
||||
```bash
|
||||
int-layer chat --telefono "1234567890"
|
||||
```
|
||||
21
apps/integration-layer/pyproject.toml
Normal file
21
apps/integration-layer/pyproject.toml
Normal file
@@ -0,0 +1,21 @@
|
||||
[project]
|
||||
name = "integration-layer"
|
||||
version = "0.1.0"
|
||||
description = "A CLI to interact with the integration layer API."
|
||||
readme = "README.md"
|
||||
authors = [
|
||||
{ name = "Anibal Angulo", email = "a8065384@banorte.com" }
|
||||
]
|
||||
requires-python = ">=3.12"
|
||||
dependencies = [
|
||||
"requests",
|
||||
"typer",
|
||||
"rich"
|
||||
]
|
||||
|
||||
[project.scripts]
|
||||
int-layer = "integration_layer.cli:app"
|
||||
|
||||
[build-system]
|
||||
requires = ["uv_build>=0.8.12,<0.9.0"]
|
||||
build-backend = "uv_build"
|
||||
79
apps/integration-layer/src/integration_layer/cli.py
Normal file
79
apps/integration-layer/src/integration_layer/cli.py
Normal file
@@ -0,0 +1,79 @@
|
||||
import random
|
||||
import string
|
||||
import subprocess
|
||||
|
||||
import typer
|
||||
from rich import print
|
||||
from rich.prompt import Prompt
|
||||
|
||||
from .main import IntegrationLayerClient
|
||||
|
||||
app = typer.Typer()
|
||||
|
||||
def get_auth_token() -> str:
|
||||
"""Gets the gcloud auth token."""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["gcloud", "auth", "print-identity-token"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True,
|
||||
)
|
||||
return result.stdout.strip()
|
||||
except (subprocess.CalledProcessError, FileNotFoundError) as e:
|
||||
print(f"[bold red]Error getting gcloud token:[/bold red] {e}")
|
||||
print("Please ensure 'gcloud' is installed and you are authenticated.")
|
||||
raise typer.Exit(code=1)
|
||||
|
||||
@app.command()
|
||||
def send(
|
||||
message: str = typer.Argument(..., help="The message to send."),
|
||||
telefono: str = typer.Option(..., "--telefono", "-t", help="User's phone number (session ID)."),
|
||||
nickname: str = typer.Option("User", "--nickname", "-n", help="User's nickname."),
|
||||
canal: str = typer.Option("sigma", "--canal", "-c", help="Channel for the request."),
|
||||
):
|
||||
"""
|
||||
Sends a single message to the Integration Layer.
|
||||
"""
|
||||
try:
|
||||
client = IntegrationLayerClient()
|
||||
token = get_auth_token()
|
||||
response = client.call(token=token, mensaje=message, telefono=telefono, nickname=nickname, canal=canal)
|
||||
print(response)
|
||||
except Exception as e:
|
||||
print(f"[bold red]Error:[/bold red] {e}")
|
||||
raise typer.Exit(code=1)
|
||||
|
||||
@app.command()
|
||||
def chat(
|
||||
telefono: str = typer.Option(None, "--telefono", "-t", help="User's phone number to start the session. If not provided, a random one will be generated."),
|
||||
nickname: str = typer.Option("User", "--nickname", "-n", help="User's nickname."),
|
||||
canal: str = typer.Option("sigma", "--canal", "-c", help="Channel for the request."),
|
||||
):
|
||||
"""
|
||||
Starts an interactive chat with the Integration Layer.
|
||||
"""
|
||||
if not telefono:
|
||||
telefono = "".join(random.choices(string.digits, k=10))
|
||||
print(f"[bold yellow]No phone number provided. Using random session ID:[/] {telefono}")
|
||||
|
||||
try:
|
||||
client = IntegrationLayerClient()
|
||||
print("[bold green]Starting a new chat session. Type 'exit' or 'quit' to end.[/bold green]")
|
||||
|
||||
while True:
|
||||
message = Prompt.ask("You")
|
||||
if message.lower() in ["exit", "quit"]:
|
||||
print("[bold yellow]Ending chat session.[/bold yellow]")
|
||||
break
|
||||
|
||||
token = get_auth_token()
|
||||
response = client.call(token=token, mensaje=message, telefono=telefono, nickname=nickname, canal=canal)
|
||||
print(f"Agent: {response}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"[bold red]Error:[/bold red] {e}")
|
||||
raise typer.Exit(code=1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app()
|
||||
43
apps/integration-layer/src/integration_layer/main.py
Normal file
43
apps/integration-layer/src/integration_layer/main.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import requests
|
||||
|
||||
|
||||
class IntegrationLayerClient:
|
||||
"""A class to interact with the Integration Layer API."""
|
||||
|
||||
def __init__(self):
|
||||
"""Initializes the IntegrationLayerClient."""
|
||||
self.endpoint = "https://34.111.169.196/api/v1/dialogflow/detect-intent"
|
||||
|
||||
def call(self, token: str, mensaje: str, telefono: str, nickname: str, canal: str) -> dict:
|
||||
"""
|
||||
Sends a message to the Integration Layer.
|
||||
|
||||
Args:
|
||||
token: The gcloud auth token.
|
||||
mensaje: The message to send.
|
||||
telefono: The user's phone number (acts as session ID).
|
||||
nickname: The user's nickname.
|
||||
canal: The channel (e.g., 'sigma').
|
||||
|
||||
Returns:
|
||||
A dictionary containing the server's response.
|
||||
"""
|
||||
headers = {
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
data = {
|
||||
"mensaje": mensaje,
|
||||
"usuario": {
|
||||
"telefono": telefono,
|
||||
"nickname": nickname,
|
||||
},
|
||||
"canal": canal,
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(self.endpoint, headers=headers, json=data, timeout=60)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
except requests.exceptions.RequestException as e:
|
||||
raise RuntimeError(f"Failed to connect to Integration Layer: {e}") from e
|
||||
Reference in New Issue
Block a user