Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from CoolCoderCarl/feature/FUNC_4
Browse files Browse the repository at this point in the history
Remove familiar lists; fix issue while send message to everyone too much
  • Loading branch information
CoolCoderCarl authored Dec 26, 2022
2 parents d1c179f + 2f15325 commit 0d224b2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 42 deletions.
20 changes: 20 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Git
.git
.github
.gitignore
.gitattributes

# Docker
docker-compose.yml
docker-compose.yaml
**/Dockerfile
**/Containerfile*
.docker
.dockerignore

# Meta
README.md
LICENSE

# Configuration
changelog.json
4 changes: 3 additions & 1 deletion Containerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
FROM python:3.9-alpine as builder

WORKDIR /opt

COPY ["bot.py", "/opt/"]
COPY requirements.txt requirements.txt

RUN pip3 install --no-cache-dir -r requirements.txt

FROM builder

CMD ["python3", "/opt/bot.py"]
CMD ["python3", "bot.py"]
61 changes: 27 additions & 34 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
import codecs
# import codecs
import logging
import os
import random
Expand All @@ -15,17 +15,12 @@

CLIENT = TelegramClient(SESSION, API_ID, API_HASH)

# Users files
FRIENDS_IDS_FILE = Path("friends_ids_list.txt")
FAMILIAR_IDS_FILE = Path("familiar_ids_list.txt")
# Users ID file
CIRCULATION_IDS_FILE = Path("circulation_ids_list.txt")

# Response files
FRIENDS_RESPONSE_FILE = Path("friend_response.txt")
FAMILIAR_RESPONSE_FILE = Path("familiar_response.txt")
HR_RESPONSE_FILE = Path("hr_response.txt")
# Response
CIRCULATION_RESPONSE_FILE = Path("circulation_response.txt")

# Key words file
HR_KEY_WORDS_FILE = Path("hr_key_words_list.txt")

# Logging
logging.basicConfig(
Expand Down Expand Up @@ -54,8 +49,7 @@ def load_ids_from_files(file: Path) -> List[int]:
logging.error(file_not_found_err)


FRIENDS_IDS = load_ids_from_files(FRIENDS_IDS_FILE)
FAMILIAR_IDS = load_ids_from_files(FAMILIAR_IDS_FILE)
CIRCULATION_IDS = load_ids_from_files(CIRCULATION_IDS_FILE)


def load_responses_from_files(file: Path) -> str:
Expand All @@ -65,7 +59,7 @@ def load_responses_from_files(file: Path) -> str:
:return:
"""
try:
with codecs.open(file, "r", "utf-8") as response_file:
with open(file, "r") as response_file:
result = response_file.read()
logging.info(
f"Uploaded response from the {response_file.name} done successfully."
Expand All @@ -75,30 +69,25 @@ def load_responses_from_files(file: Path) -> str:
logging.error(file_not_found_err)


FRIEND_RESPONSE = load_responses_from_files(FRIENDS_RESPONSE_FILE)
FAMILIAR_RESPONSE = load_responses_from_files(FAMILIAR_RESPONSE_FILE)
HR_RESPONSE = load_responses_from_files(HR_RESPONSE_FILE)

HR_KEY_WORDS = load_responses_from_files(HR_KEY_WORDS_FILE)
CIRCULATION_RESPONSE = load_responses_from_files(CIRCULATION_RESPONSE_FILE)


async def show_selected_users():
async for dialog in CLIENT.iter_dialogs():
if dialog.id in FRIENDS_IDS:
logging.info(f"Selected friends username: {dialog.name}; ID: {dialog.id}")
elif dialog.id in FAMILIAR_IDS:
logging.info(f"Selected familiar username: {dialog.name}; ID: {dialog.id}")
if dialog.id in CIRCULATION_IDS:
logging.info(f"Selected username: {dialog.name}; ID: {dialog.id}")


async def send_message_template(
user_data, event, start_range, end_range, response_type
):
logging.info(
f"Contact: {user_data.contact} -"
f"username: {user_data.first_name} - "
f"Contact: {user_data.contact} - "
f"first name: {user_data.first_name} - "
f"ID: {user_data.id} - "
f"sent message: {event.message.message}"
)

logging.info("Waiting for response...")
async with CLIENT.action(user_data.id, "typing"):
await asyncio.sleep(random.randrange(start_range, end_range))
Expand All @@ -122,22 +111,26 @@ async def response_to_group(event):
logging.info(f"Raw sender data: {user_data}")

try:
if user_data.id in FRIENDS_IDS:
await send_message_template(user_data, event, 5, 10, FRIEND_RESPONSE)
elif user_data.id in FAMILIAR_IDS:
await send_message_template(user_data, event, 15, 20, FAMILIAR_RESPONSE)
if user_data.id in CIRCULATION_IDS:
await send_message_template(user_data, event, 5, 10, CIRCULATION_RESPONSE)
elif not user_data.contact:
for key_words in HR_KEY_WORDS:
if key_words in event.message.message:
logging.info("Looks like HR is on the line.")
await send_message_template(user_data, event, 1, 5, HR_RESPONSE)
logging.info("Looks like someone unfamiliar is on the line.")
logging.info(
f"Contact: {user_data.contact} - "
f"first name: {user_data.first_name} - "
f"ID: {user_data.id} - "
f"sent message: {event.message.message}"
)
except ValueError as val_err:
logging.error(f"Sender is {user_data.first_name}")
logging.error(f"Sender is {user_data.first_name}.")
logging.error(val_err)
except TypeError as type_err:
logging.error("That maybe sticker was sent, not text.")
logging.error(f"Sender is {user_data.first_name}")
logging.error(f"Sender is {user_data.first_name}.")
logging.error(type_err)
except BaseException as base_exception:
logging.error(f"Sender is {user_data.first_name}.")
logging.error(base_exception)


if __name__ == "__main__":
Expand Down
10 changes: 3 additions & 7 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ services:
- API_HASH=YOURAPIHASH
- SESSION=YOURSESSION
volumes:
- "/root/herold_bot/friends_ids_list.txt:/friends_ids_list.txt"
- "/root/herold_bot/friend_response.txt:/friend_response.txt"
- "/root/herold_bot/familiar_ids_list.txt:/familiar_ids_list.txt"
- "/root/herold_bot/familiar_response.txt:/familiar_response.txt"
- "/root/herold_bot/hr_response.txt:/hr_response.txt"
- "/root/herold_bot/hr_key_words.txt:/hr_key_words.txt"
- "/root/herold_bot/JustTalk.session:/JustTalk.session"
- "/root/herold_bot/circulation_ids_list.txt:/opt/circulation_ids_list.txt"
- "/root/herold_bot/circulation_response.txt:/opt/circulation_response.txt"
- "/root/herold_bot/JustTalk.session:/opt/JustTalk.session"
volumes:
herold_bot:

0 comments on commit 0d224b2

Please sign in to comment.