-
Notifications
You must be signed in to change notification settings - Fork 0
/
vk_bot.py
49 lines (35 loc) · 1.39 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
import vk_api as vk
from vk_api.longpoll import VkLongPoll, VkEventType
import os
import random
from dialogflow_functions import smart_answer
from telegram import Bot
import logging
from MyLogsHandler import MyLogsHandler
LANGUAGE_CODE = "ru"
logger = logging.getLogger("VK Bot logger")
def vk_smart_answer(event, vk_api, project_id):
answer = smart_answer(project_id=project_id, session_id=event.user_id,
text=event.text,
language_code=LANGUAGE_CODE)
if not answer.intent.is_fallback:
vk_api.messages.send(
user_id=event.user_id,
message=answer.fulfillment_text,
random_id=random.randint(1, 1000)
)
if __name__ == "__main__":
project_id = os.environ.get('PROJECT_ID')
vk_token = os.environ.get('VK_TOKEN')
vk_session = vk.VkApi(token=vk_token)
vk_api = vk_session.get_api()
tg_bot_token = os.environ.get('TG_BOT_TOKEN')
tg_error_chat_id = os.environ.get('TG_ERROR_CHAT_ID')
bot = Bot(tg_bot_token)
logging.basicConfig(format="%(process)d %(levelname)s %(message)s")
logger.setLevel(logging.INFO)
logger.addHandler(MyLogsHandler(tg_error_chat_id, bot))
longpoll = VkLongPoll(vk_session)
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW and event.to_me:
vk_smart_answer(event, vk_api, project_id)