diff --git a/Docker.md b/Docker.md index 54d9cf1..cd09b77 100644 --- a/Docker.md +++ b/Docker.md @@ -4,13 +4,23 @@ To get started, install Docker and Docker Compose on your server. -You can choose to use either the legacy version, which is powered by MongoDB, by using the docker-compose.legacy.yml -file -or the latest version, which is powered by MeiliSearch, by using the docker-compose.yml file. +Download the `docker-compose.yml` to your favorite directory. + +```shell +wget https://raw.githubusercontent.com/tgbot-collection/SearchGram/master/docker-compose.yml +``` + +In this `docker-compose.yml`, you need to decide which search engine to use, available options are: + +* MeiliSearch +* MongoDB +* ZincSearch + +Comment out the search engine you don't want to use. # 2. (Optional) Prepare the Encrypted Data Volume -For added security, it's highly recommended to use an encrypted data volume. +For added security, it's recommended to use an encrypted data volume. You can use LUKS for this purpose. @@ -93,7 +103,7 @@ cryptsetup luksClose sg_data To get started with SearchGram, you'll need to -1. obtain your APP_ID and APP_HASH from https://core.telegram.org/, +1. get your APP_ID and APP_HASH from https://core.telegram.org/, 2. get your bot token by contacting @BotFather 3. get your user ID and bot ID by contacting @blog_update_bot. @@ -103,6 +113,8 @@ The MEILI_MASTER_KEY is a credential used to access the Web UI of MeiliSearch. To simplify things, you can use your bot token instead. +All the environment variables are stored in `env/gram.env` and you can see the comments in `config.py` for more details. + ```shell # vim env/gram.env TOKEN=token @@ -126,7 +138,7 @@ python client.py Follow the instruction to log in to your account. -When you see 'started xxx handlers', Ctrl + D to exit. You should find session file +When you see 'started xxx handlers', Ctrl + C to exit. You should find session file under `searchgram/session/client.session`. # 6. (optional)setup sync id diff --git a/Dockerfile b/Dockerfile index d0132d8..8d2018f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,12 @@ -FROM python:3.10-alpine as builder +FROM python:3.11-alpine as builder RUN apk update && apk add --no-cache tzdata alpine-sdk ca-certificates ADD requirements.txt /tmp/ RUN pip3 install --user -r /tmp/requirements.txt && rm /tmp/requirements.txt -FROM python:3.10-alpine +FROM python:3.11-alpine WORKDIR /SearchGram/searchgram -ENV TZ=Asia/Shanghai COPY --from=builder /root/.local /usr/local COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ diff --git a/docker-compose.yml b/docker-compose.yml index 8aa093f..25c4cc7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,7 +25,7 @@ services: image: getmeili/meilisearch:v1.1.1 restart: always volumes: - - ./sg_data/:/meili_data + - ./sg_data/meili:/meili_data env_file: - env/gram.env ports: @@ -35,13 +35,13 @@ services: image: mongo:6 restart: always volumes: - - ./sg_data/mongodb:/data/db + - ./sg_data/mongo:/data/db logging: driver: none ports: - "127.0.0.1:27017:27017" - zincsearch: + zinc: image: public.ecr.aws/zinclabs/zincsearch:latest ports: - "127.0.0.1:4080:4080" @@ -52,4 +52,4 @@ services: env_file: - env/gram.env volumes: - - ./sg_data/:/data + - ./sg_data/zinc:/data diff --git a/requirements.txt b/requirements.txt index 83a2e22..63bf418 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,9 @@ pyrogram==2.0.106 tgcrypto==1.2.5 fakeredis==2.20.1 -tqdm==4.65.0 -meilisearch==0.28.1 +tqdm==4.66.1 +meilisearch==0.28.4 coloredlogs==15.0.1 -pymongo==4.6.0 +pymongo==4.6.1 zhconv==1.4.3 zincsearch-sdk==0.3.3 diff --git a/searchgram/__init__.py b/searchgram/__init__.py index c2ba6f2..53f5b04 100644 --- a/searchgram/__init__.py +++ b/searchgram/__init__.py @@ -6,6 +6,10 @@ from config import ENGINE +AVAILABLE_ENGINES = ["meili", "mongo"] +if ENGINE not in AVAILABLE_ENGINES: + raise ValueError(f"Unsupported engine {ENGINE}, available engines are {AVAILABLE_ENGINES}") + if ENGINE == "meili": print("Using MeiliSearch as search engine") from meili import SearchEngine diff --git a/searchgram/client.py b/searchgram/client.py index 51cd077..9ba3541 100644 --- a/searchgram/client.py +++ b/searchgram/client.py @@ -28,23 +28,15 @@ r = fakeredis.FakeStrictRedis() -@app.on_message(filters.outgoing | filters.incoming) +@app.on_message((filters.outgoing | filters.incoming) & ~filters.chat(BOT_ID)) def message_handler(client: "Client", message: "types.Message"): - # don't know why `~filters.chat(int(BOT_ID))` is not working - if str(message.chat.id) == BOT_ID: - logging.debug("Ignoring message from bot itself") - return - + logging.info("Adding new message: %s-%s", message.chat.id, message.id) tgdb.upsert(message) -@app.on_edited_message() +@app.on_edited_message(~filters.chat(BOT_ID)) def message_edit_handler(client: "Client", message: "types.Message"): - # don't know why `~filters.chat(int(BOT_ID))` is not working - if str(message.chat.id) == BOT_ID: - logging.debug("Ignoring message from bot itself") - return - + logging.info("Editing old message: %s-%s", message.chat.id, message.id) tgdb.upsert(message) diff --git a/searchgram/config.py b/searchgram/config.py index 2cc7534..7f7af8d 100644 --- a/searchgram/config.py +++ b/searchgram/config.py @@ -13,15 +13,20 @@ APP_HASH = os.getenv("APP_HASH", "23231321") TOKEN = os.getenv("TOKEN", "1234") # id:hash +# MeiliSearch, by default it's meili in docker-compose MEILI_HOST = os.getenv("MEILI_HOST", "http://meili:7700") +# Using bot token for simplicity MEILI_PASS = os.getenv("MEILI_MASTER_KEY", TOKEN) +# If you want to use MongoDB as search engine, you need to set this MONGO_HOST = os.getenv("MONGO_HOST", "mongo") -ENGINE = os.getenv("ENGINE", "meili") +# available values: meili, mongo, zinc, default: meili +ENGINE = os.getenv("ENGINE", "meili").lower() +# Your own user id, for example: 260260121 OWNER_ID = os.getenv("OWNER_ID", "260260121") -BOT_ID = TOKEN.split(":")[0] +BOT_ID = int(TOKEN.split(":")[0]) PROXY = os.getenv("PROXY") # example proxy configuration