Skip to content

Commit

Permalink
code refine
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyThink committed Jan 1, 2024
1 parent 0b29f27 commit cbdbd34
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 30 deletions.
24 changes: 18 additions & 6 deletions Docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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/
Expand Down
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
Expand All @@ -52,4 +52,4 @@ services:
env_file:
- env/gram.env
volumes:
- ./sg_data/:/data
- ./sg_data/zinc:/data
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions searchgram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 4 additions & 12 deletions searchgram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
9 changes: 7 additions & 2 deletions searchgram/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cbdbd34

Please sign in to comment.