-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtg_bot.py
More file actions
51 lines (40 loc) · 1.67 KB
/
tg_bot.py
File metadata and controls
51 lines (40 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import random
import time
import argparse
import telegram
from dotenv import load_dotenv
def main():
load_dotenv()
tg_token = os.getenv('TG_TOKEN')
tg_chat_id = os.getenv('TG_CHAT_ID')
bot = telegram.Bot(token=tg_token)
parser = argparse.ArgumentParser(description='Отправка случайных фотографий в чат Telegram')
parser.add_argument('--time',
type=int,
default=14400,
help='Интервал времени (в секундах) между отправкой случайных фотографий')
parser.add_argument('--folder',
type=str,
default='folder',
help='Путь к папке, содержащей фотографии для отправки')
args = parser.parse_args()
folder = args.folder
periodicity = args.time
while True:
all_files = []
for root, _, files in os.walk(folder):
all_files.extend([os.path.join(root, file) for file in files])
if not all_files:
print(f'Папка {folder} не содержит файлов.')
break
random_file = random.choice(all_files)
try:
with open(random_file, 'rb') as file:
bot.send_document(chat_id=tg_chat_id, document=file)
print(f'Отправлена фотография: {random_file}')
except telegram.error.TelegramError as e:
print(f'Произошла ошибка при отправке: {e}')
time.sleep(periodicity)
if __name__ == '__main__':
main()