Z
Z
Zettt132019-01-21 14:01:05
Python
Zettt13, 2019-01-21 14:01:05

How to implement a response to the user from a text file with templates?

There is a script itself, it is necessary that when receiving a message from the user, the script looks for a suitable answer in a txt file, in which questions and answers will look like this:
Hello: Hello
How are you?: well
Wrote a script on the vk_api library

import vk_api # Библиотека VK
from datetime import datetime # Библиотека DateTime
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType # Библиотека VK LongPool

# Настройки
token = "" # Личный access_token сообщества (Желателен полный доступ)
group_id = "175481630" # ID сообщества
server = "@id" # Владелец кода
longpool_sleep = "0.5" # Задержка проверки сообщений, зависит от нагрузки на сервер (Рекомендуется значение от 0.1 до 10)

# VK LongPool
vk_session = vk_api.VkApi(token = token) # Обработка access_token
longpoll = VkBotLongPoll(vk_session, group_id) # Данные для работы в сообществе
vk = vk_session.get_api() # Работа с VK API

# Info
version = "release 7.0" # Версия бота
api_version = "5.92" # Версия VK API
system = "LongPoll" # LongPoll или CallBack
language = "Python 3.7.2" # Язык и версия языка

# Time
servertime = str(datetime.strftime(datetime.now(), "%H:%M")) # Время сервера

# Сообщения
for event in longpoll.listen():

    if event.type == VkBotEventType.MESSAGE_NEW and event.obj.text.lower() == "test" or event.obj.text.lower() == "тест":
        vk.messages.send(peer_id = event.obj.peer_id, random_id = 0, message = "Tested!")
        vk.messages.send(peer_id = event.obj.peer_id, random_id = 0, message = "Отправьте команду help для получения списка комманд бота")
 
    if event.type == VkBotEventType.MESSAGE_NEW and event.obj.text.lower() == "help" or event.obj.text.lower() == "помощь":
        vk.messages.send(peer_id = event.obj.peer_id, random_id = 0, message = "Список комманд:\n\ntest или тест\nТестовая проверка\n\nhelp или помощь\nСписок комманд\n\ntime или время\nВремя сервера\n\nserver или сервер\nКто запустил сервер\n\ninfo или инфо\nИнформация о боте")

    if event.type == VkBotEventType.MESSAGE_NEW and event.obj.text.lower() == "time" or event.obj.text.lower() == "время":
        vk.messages.send(peer_id = event.obj.peer_id, random_id = 0, message = "Время сервера: " + servertime)

    if event.type == VkBotEventType.MESSAGE_NEW and event.obj.text.lower() == "server" or event.obj.text.lower() == "сервер":
    	vk.messages.send(peer_id = event.obj.peer_id, random_id = 0, message = "Сервер запустил: " + server)

    if event.type == VkBotEventType.MESSAGE_NEW and event.obj.text.lower() == "info" or event.obj.text.lower() == "инфо":
    	vk.messages.send(peer_id = event.obj.peer_id, random_id = 0, message = "Версия: " + version + "\nVK API: " + api_version + "\nСистема: " + system + "\nЯзык: " + language + "\n\nРазработчики")

time.sleep(longpool_sleep)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sudo rm -rf /, 2019-01-21
@Zettt13

The easiest option
Let's call the file qa.txt
When the file is completely translated into the db dictionary, it will be possible to receive responses like this PS Correct the answer by submitting the code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question