-
Notifications
You must be signed in to change notification settings - Fork 1
/
vk_bot.py
51 lines (38 loc) · 1.32 KB
/
vk_bot.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
import logging
import os
import random
import vk_api as vk
from dotenv import load_dotenv
from vk_api.longpoll import VkLongPoll, VkEventType
from dialog import detect_intent_texts
from logs_handler import configure_handler
logger = logging.getLogger('vk_bot')
def reply_to_message(event, vk_api):
"""Responding to user messages using dialogflow."""
project_id = os.getenv('PROJECT_ID')
answer = detect_intent_texts(
project_id,
event.user_id,
event.text,
'ru-RU')
if not answer['is_fallback']:
vk_api.messages.send(
user_id=event.user_id,
message=answer['answer_text'],
random_id=random.randint(0, 100000))
def main():
load_dotenv()
configure_handler(logger, os.getenv('TG_SERVICE_BOT'), os.getenv('TG_CHAT_ID'))
logger.info('vk_bot запущен!')
vk_session = vk.VkApi(token=os.getenv('VK_GROUP_TOKEN'))
vk_api = vk_session.get_api()
longpoll = VkLongPoll(vk_session)
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW and event.to_me:
# noinspection PyBroadException
try:
reply_to_message(event, vk_api)
except Exception:
logger.exception('vk_bot поймал ошибку: ')
if __name__ == '__main__':
main()