Skip to content

chen-tf/price-tracker-bot

Repository files navigation

price-tracker-bot

linting: pylint Code style: black Imports: isort

目錄

簡介

price-tracker-bot is a telegram bot that can trace the price on momoshop.

Demo Bot

Telegram bot search @momo_price_tracker_bot

功能

  • LINE 通知服務
    LINE 通知服務
  • 降價通知 降價通知
  • 上架通知 上架通知
  • 收藏商品 收藏商品
  • 清空已收藏商品 清空已收藏商品
  • 顯示目前已收藏商品 顯示目前已收藏商品

如何在本機端使用

下載專案

  • 從 Github clone 此專案
git clone https://github.com/chen-tf/price-tracker-bot.git 

建立 Python 環境

  • 安裝 Python,推薦版本 3.9
  • 在專案資料夾內,安裝相關套件
    • pip install -r requirements.txt

建立 PostgreSQL

  • 安裝 Docker
  • 安裝 PostgreSQL 環境
    • 在專案資料夾內,執行 docker-compose up,創建並啟動 PostgreSQL 環境

建立 Telegram bot

  • 加入 Telegram
  • 在對話視窗內呼叫 @BotFather,依照提示建立一個機器人,取得建立的 Telegram bot 的 Token

在本機端運行

  • 在專案資料夾內建立一個檔名為 .env 檔案,內容如下
  • 將 BOT_TOKEN 內容變更為建立 Telegram bot 時取得的 Token
BOT_TOKEN = "{replace_me}"
DB_HOST = "localhost"
DB_NAME = "postgres"
DB_PASSWORD = "postgres"
DB_USER = "postgres"
DB_PORT = 5432
LOGGING_LEVEL = "INFO"
TELEGRAM_BOT_MODE = "polling"
  • 在專案資料夾內執行檔案
python app.py

新建立的Telegram聊天頻道中已有的內建指令

/my 顯示追蹤清單
/clearall 清空全部追蹤清單
/clear 刪除指定追蹤商品
/add 後貼上momo商品連結可加入追蹤清單或是可以直接使用指令選單方便操作

環境建置

Database

  • PostgreSQL 10

執行 sqls folder底下放置的SQL file,或者可以自行使用其他db migration 工具

環境變數

環境變數 說明
DB_HOST Database host
DB_NAME Database name
DB_USER Database user
DB_PASSWORD Database user's password
BOT_TOKEN Telegram bot token
WEBHOOK_URL(Optional) For deploy your app to cloud provides
PERIOD_HOUR Resync latest good's price time period
TELEGRAM_BOT_MODE default: polling, [polling,webhook]

Telegram bot

加入 telegram botfather並建立一個機器人,取得 token ,後續須設定環境變數 BOT_TOKEN

Bot 接收訊息方式

設定環境變數 TELEGRAM_BOT_MODE

  • polling 測試使用上方便,不需要對外網址,效能上較webhook差
  • webhook 如果要在local 測試 webhook,可使用 ngrok

fly.io

This is indeed a timeout due to the fact that Fly.io put HAproxy in front of the PostgreSQL instance, and the HAproxy has a 30m timeout. (This was confirmed via email support with Fly.io)

This means you will either want to a make sure your PostgreSQL adapter can deal with (or automatically) reconnects, or fire some kind of idle timer that runs a query at least once every 30 min, to keep the connection alive.

環境變數可以設定至 fly.toml 中

Supabase (Free Postgres Online)

Linter

使用 isort (sort imports)

# cheat
echo "$(git diff --name-only --diff-filter=ACMR HEAD *.py)" | xargs isort

# usage - never use
isort **/*.py

使用 black (formatter)

# cheat
echo "$(git diff --name-only --diff-filter=ACMR HEAD *.py)" | xargs black

# find py file which place would be reformatted 
echo "$(git diff --name-only --diff-filter=ACMR HEAD *.py)" | xargs black --diff

# use black for specific file
black foo.py

# usage - never use, it will reformat py file at all
black *.py

使用 pylint (lint)

# cheat
echo "$(git diff --name-only --diff-filter=ACMR HEAD *.py)" | xargs pylint -sn

# usage - never use, cause too many output
pylint *.py

pre-commit

確認 pre-commit 是否已安裝至專案環境底下

# check  
pre-commit -V  
  
# install with requirements.txt  
pip install -r requirements.txt  
  
# or directly install 
pip install pre-commit  

將 pre-commit hook 安裝至專案裡的 .git/hooks 資料夾底下

# init hook, will create .git/hooks/pre-commit  
pre-commit install --install-hooks  

執行 git commit

git commit -m 'feat: hello world'

成功時畫面如下

succeful-pre-commit

失敗時,需要調整相對應的檔案,並重新將檔案加入 git 版控,再重新進行 commit

fail-pre-commit


機器人指令


對話輸入momo商品網址可直接加入追蹤

  • /start 顯示目前所支援的指令
  • /my 顯示目前所有追蹤物品清單,以及當下所記錄價格
  • /clear 清除目前所有追蹤物品清單

Command menu

You need to say these to @BotFather.

Bot can show command menu

如何貢獻

歡迎所有貢獻、提交錯誤報告、協助錯誤修復、文檔改進、功能增強和提供你的想法。

你可以移至"Issue"選項並開始查看你認為有趣的問題。文檔下列出了許多問題,您可以從這裡開始。

或者,您有了自己的想法,或者正在程式碼中尋找某些東西並認為“這可以改進”……您可以為此做點什麼!

作為本項目的貢獻者和維護者,您應該遵守 price-tracker-bot 的行為準則。

貢獻作者

授權條款

MIT