Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .github/workflows/generator-generic-ossf-slsa3-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# This workflow lets you generate SLSA provenance file for your project.
# The generation satisfies level 3 for the provenance requirements - see https://slsa.dev/spec/v0.1/requirements
# The project is an initiative of the OpenSSF (openssf.org) and is developed at
# https://github.com/slsa-framework/slsa-github-generator.
# The provenance file can be verified using https://github.com/slsa-framework/slsa-verifier.
# For more information about SLSA and how it improves the supply-chain, visit slsa.dev.

name: SLSA generic generator
on:
workflow_dispatch:
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
outputs:
digests: ${{ steps.hash.outputs.digests }}

steps:
- uses: actions/checkout@v4

# ========================================================
#
# Step 1: Build your artifacts.
#
# ========================================================
- name: Build artifacts
run: |
# These are some amazing artifacts.
echo "artifact1" > artifact1
echo "artifact2" > artifact2
# ========================================================
#
# Step 2: Add a step to generate the provenance subjects
# as shown below. Update the sha256 sum arguments
# to include all binaries that you generate
# provenance for.
#
# ========================================================
- name: Generate subject for provenance
id: hash
run: |
set -euo pipefail
# List the artifacts the provenance will refer to.
files=$(ls artifact*)
# Generate the subjects (base64 encoded).
echo "hashes=$(sha256sum $files | base64 -w0)" >> "${GITHUB_OUTPUT}"
provenance:
needs: [build]
permissions:
actions: read # To read the workflow path.
id-token: write # To sign the provenance.
contents: write # To add assets to a release.
uses: slsa-framework/slsa-github-generator/.github/workflows/[email protected]
with:
base64-subjects: "${{ needs.build.outputs.digests }}"
upload-assets: true # Optional: Upload to a new release
29 changes: 29 additions & 0 deletions .github/workflows/huggingface.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

name: Sync to Hugging Face space

on:
push:
branches:
- main # Or whichever branch you want to deploy from

jobs:
sync-to-hub:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
lfs: true

- name: Push to HF Space
env:
HF_SPACE_GIT_REPO: "https://huggingface.co/spaces/nssuwan186/Bot_telegram"
run: |
git remote add space "https://USER:${{ secrets.HF_TOKEN }}@huggingface.co/spaces/nssuwan186/Bot_telegram"
git push --force space main

# IMPORTANT:
# 1. Replace 'YOUR_HF_USERNAME' with your Hugging Face username.
# 2. Replace 'YOUR_HF_SPACE_NAME' with your Hugging Face Space name.
# 3. You must create a secret in your GitHub repository named 'HF_TOKEN'.
# This token should be a Hugging Face access token with 'write' permissions.
50 changes: 7 additions & 43 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.streamlit/secrets.toml

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand All @@ -22,6 +20,7 @@ parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
Expand Down Expand Up @@ -51,7 +50,6 @@ coverage.xml
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
Expand All @@ -74,7 +72,6 @@ instance/
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
Expand All @@ -85,35 +82,9 @@ profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
.python-version

# PEP 582; used by Poetry, Flit, and PDM
__pypackages__/

# Celery stuff
Expand All @@ -129,8 +100,8 @@ celerybeat.pid
env/
venv/
ENV/
env.bak/
venv.bak/
env.bak
venv.bak

# Spyder project settings
.spyderproject
Expand All @@ -150,15 +121,8 @@ dmypy.json
# Pyre type checker
.pyre/

# pytype static type analyzer
# pytype static analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Use the official Python image
FROM python:3.10-slim

# Set the working directory
WORKDIR /app

# Install system dependencies
# - Tesseract for OCR
# - Cron for scheduled tasks
# - Git for Hugging Face integration
RUN apt-get update && apt-get install -y \
tesseract-ocr \
cron \
git \
&& rm -rf /var/lib/apt/lists/*

# Copy the requirements file and install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code
COPY . .

# Make the startup script executable
RUN chmod +x /app/start.sh

# Expose the port FastAPI will run on
EXPOSE 7860

# Set the entrypoint to the startup script
CMD ["/app/start.sh"]
120 changes: 108 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,115 @@
# 💬 Chatbot template
# Hotel OS Telegram Bot

A simple Streamlit app that shows how to build a chatbot using OpenAI's GPT-3.5.
This is a comprehensive Telegram bot designed to act as a "Hotel Operating System". It assists with managing bookings, verifying payments via OCR, tracking expenses, and controlling room hardware (lights, AC) via MQTT.

[![Open in Streamlit](https://static.streamlit.io/badges/streamlit_badge_black_white.svg)](https://chatbot-template.streamlit.app/)
The bot is built with Python, using `python-telegram-bot`, `FastAPI`, and `SQLAlchemy`. It's designed to be deployed as a container on Hugging Face Spaces.

### How to run it on your own machine
## Features

1. Install the requirements
- **AI Assistant**: Powered by Google Gemini for general-purpose questions.
- **Payment Verification**: Upload a payment slip image, and the bot uses Tesseract OCR to extract the customer name and amount, then matches it against an unpaid booking in the database.
- **Expense Tracking**: Add expenses via the Telegram Web App.
- **Hardware Control**: Control IoT devices (like lights and AC) using the MQTT protocol, either via commands or the Web App.
- **Daily Reports**: Get a summary of daily income and expenses with the `/daily_report` command.
- **Database**: Uses SQLite to store all data, with automatic daily backups sent to your Telegram.
- **Automated Deployment**: Automatically deploys to a configured Hugging Face Space on push to the `main` branch via GitHub Actions.

```
$ pip install -r requirements.txt
```
## Project Structure

2. Run the app
```
/hotel-os-bot
├── .github/workflows/huggingface.yml # GitHub Action for auto-deployment
├── app/ # Main application source code
│ ├── __init__.py
│ ├── main.py # FastAPI app and Telegram bot logic
│ ├── config.py # Configuration and environment variables
│ ├── database.py # SQLAlchemy models and DB functions
│ ├── ocr.py # Tesseract OCR processing logic
│ └── mqtt.py # MQTT publishing logic
├── scripts/
│ └── backup_db.py # Script for daily DB backups
├── webapp/ # Telegram Web App files
│ ├── index.html
│ ├── style.css
│ └── app.js
├── .gitignore
├── Dockerfile # Docker container definition for HF Spaces
├── README.md # This file
├── requirements.txt # Python dependencies
└── start.sh # Container startup script
```

```
$ streamlit run streamlit_app.py
```
## Deployment Guide

Follow these steps to get your bot running on Hugging Face Spaces, with automated deployments from your GitHub repository.

### Step 1: Push to GitHub

1. Create a new **private** repository on GitHub.
2. In your local terminal, add the GitHub repository as a remote:
```bash
git remote add origin https://github.com/YOUR_USERNAME/YOUR_REPO_NAME.git
git branch -M main
```
3. Commit and push all the generated files:
```bash
git add .
git commit -m "Initial project setup"
git push -u origin main
```

### Step 2: Create a Hugging Face Space

1. Go to [Hugging Face](https://huggingface.co/new-space) and create a new **Space**.
2. **Owner/Space name**: Choose a name for your project (e.g., `hotel-os-bot`).
3. **License**: Select `mit` or another license.
4. **Select the Space SDK**: Choose **Docker** and `Blank` template.
5. **Space hardware**: The free `CPU basic` is sufficient to start.
6. **Secrets**: You do not need to set secrets here initially if you are using the GitHub Action workflow, but you will need to add them later for the bot to run.
7. Click **Create Space**. The initial build will likely fail, which is normal.

### Step 3: Configure GitHub Actions for Auto-Deployment

1. **Get Hugging Face Token**: In Hugging Face, go to `Your Profile -> Settings -> Access Tokens`. Create a new token with the `write` role. Copy this token.
2. **Add Secret to GitHub**: In your GitHub repository, go to `Settings -> Secrets and variables -> Actions`. Create a **New repository secret**:
* **Name**: `HF_TOKEN`
* **Value**: Paste the Hugging Face token you just copied.
3. **Update Workflow File**: Open `.github/workflows/huggingface.yml` in your local project. Replace the placeholder `YOUR_HF_USERNAME` and `YOUR_HF_SPACE_NAME` with your actual HF username and Space name.
```yaml
# ...
env:
HF_SPACE_GIT_REPO: "https://huggingface.co/spaces/your-hf-username/your-hf-space-name"
run: |
git remote add space "https://USER:${{ secrets.HF_TOKEN }}@huggingface.co/spaces/your-hf-username/your-hf-space-name"
git push --force space main
```
4. Commit and push this change. This will trigger the first deployment from GitHub Actions. You can monitor its progress in the "Actions" tab of your GitHub repository.

### Step 4: Configure Bot Secrets in Hugging Face Space

For your bot to run correctly, it needs its API keys. These **must** be set in the Hugging Face Space settings, not in your code.

1. In your HF Space, go to the `Settings` tab.
2. Scroll down to **Repository secrets** and click **New secret**.
3. Add the following secrets one by one:

* `TELEGRAM_BOT_TOKEN`: Your token from BotFather.
* `TELEGRAM_USER_ID`: Your numeric Telegram user ID. You can get this from a bot like `@userinfobot`.
* `GEMINI_API_KEY`: Your API key for Google Gemini.
* `MQTT_BROKER`: (Optional) The address of your MQTT broker. Defaults to `broker.hivemq.com`.
* `MQTT_TOPIC_PREFIX`: (Optional) The base topic for your MQTT devices. Defaults to `hotel/room1`.

### Step 5: Update Web App URL and Final Test

1. **Update URL**: Once your space is deployed, it will have a URL like `https://your-username-your-space-name.hf.space`. You need to put this URL into the `app/main.py` file.
```python
# in app/main.py, inside start_command function
web_app_button = KeyboardButton(
"Open Hotel OS Web App",
web_app=WebAppInfo(url=f"https://your-hf-username-your-hf-space-name.hf.space/static/index.html")
)
```
*Note: The URL should point to the static HTML file.*

2. Commit and push this final change. The GitHub Action will run again and deploy the updated code.
3. **Test**: Open Telegram and talk to your bot. Use the `/start` command to see the Web App button and test all features.
Loading