Попробуйте это :)
Мне не особо хочется тестить поэтому вот :
# -*- coding: utf-8 -*-
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
import vk_api
import requests
import json
import time
import sys
from random import randint
vk = vk_api.VkApi(token="xxxx")
vk._auth_token()
vk.get_api()
longpoll = VkBotLongPoll(vk, xxx)
while True:
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
# print(event.object.text)
# print()
text = event.object.text;
if text.lower() == "вики":
wiki_url = 'https://ru.wikipedia.org/w/api.php?action=opensearch&redirects=resolve&limit=1&format=json&search=';
try:
wiki_url = wiki_url + str(event.object.reply_message['text'])
wiki_response = requests.get(wiki_url)
wiki_json = json.loads(wiki_response.text)
wiki_subj = wiki_json[2][0]
wiki_link = wiki_json[3][0]
if wiki_subj == '':
wiki_message = "Нет такого на вики!";
else:
wiki_message = wiki_subj + "\n Подробнее: " + wiki_link
except TypeError:
wiki_message = 'Что виковать?'
except IndexError:
wiki_message = 'Нет этого на вики!!!'
vk.method("messages.send", {"peer_id": event.object.peer_id, "message": wiki_message,
"random_id": randint(1, 9999)})
elif text.lower() == "баш":
bash_url = "https://api.forismatic.com/api/1.0/?method=getQuote&key=457653&format=json&lang=ru"
bash_response = requests.get(bash_url)
bash_json = json.loads(bash_response.text)
bash_message = bash_json['quoteText']
try:
bash_message = bash_message + "\n Автор: " + bash_json['quoteAuthor']
except TypeError:
bash_message = bash_message + ' '
vk.method("messages.send", {"peer_id": event.object.peer_id, "message": bash_message,
"random_id": randint(1, 9999)})
elif text.lower() == "текст":
text_url = "https://fish-text.ru/get"
text_response = requests.get(text_url)
text_json = json.loads(text_response.text)
text_message = text_json["text"]
vk.method("messages.send", {"peer_id": event.object.peer_id, "message": text_message,
"random_id": randint(1, 9999)})
time.sleep(0.01)