-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
104 lines (83 loc) · 3.69 KB
/
main.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- coding: utf8 -*-
from asyncio.log import logger
import logging
from environs import Env
#
from vk_api import VkApi
from vk_api.utils import get_random_id
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
def send_message(event, text=None, attachment_arg=None):
vk_session.method(
method='messages.send',
values={'user_id': event.object['message']['from_id'],
'message': text,
'attachment': attachment_arg,
'random_id': get_random_id(),
}
)
def repeat_photos(event):
message_attachments = event.object['message']['attachments']
counter_for_non_photos = 0
attachment_arg = ""
for message_attachment in message_attachments:
media_type = message_attachment["type"]
if media_type != "photo":
counter_for_non_photos += 1
continue
owner_id = message_attachment["photo"]["owner_id"]
photo_id = message_attachment["photo"]["id"]
access_key = message_attachment["photo"]["access_key"]
attachment_arg += f"{media_type}{owner_id}_{photo_id}_{access_key},"
if counter_for_non_photos == len(message_attachments):
text = "Отправь мне фотографии, чтобы я помог тебе! 😊"
send_message(event=event, text=text)
else:
attachment_arg = attachment_arg[:-1] # Обрезаем запятую в конце
message_text = "Лови! 🥳"
send_message(event=event,
text=message_text,
attachment_arg=attachment_arg
)
user = vk_session.method(method='users.get',
values={'user_id': event.object['message']['from_id'],
'name_case': "gen",
}
)
user_first_name = user[0]["first_name"]
user_last_name = user[0]["last_name"]
logger.info(
msg=f"Обработаны фотографии {user_first_name} {user_last_name} "
f"(https://vk.com/id{event.object.from_id})."
)
def main():
# Главная функция инициализации и запуска бота.
global vk_session, logger
env = Env()
env.read_env()
TOKEN = env.str("TOKEN")
GROUP_ID = env.str("GROUP_ID")
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
)
logger = logging.getLogger(__name__)
vk_session = VkApi(token=TOKEN)
longpoll = VkBotLongPoll(vk_session, group_id=GROUP_ID)
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
if event.from_user:
message = event.object['message']
if message['text'] == "Начать":
start_text = "Привет! 😊\nЯ - чат-бот для добавления твоих фотографий в «Сохранёнки»!" \
"\nОтправь мне свои фотографии, а я перешлю их тебе, после чего ты сможешь добавить их к себе в альбом!"
send_message(event=event, text=start_text)
#
elif len(message['attachments']) == 0:
text = "Отправь мне фотографии, чтобы я помог тебе! 😊"
send_message(event=event, text=text)
#
else:
repeat_photos(event=event)
#
if __name__ == '__main__':
main()