H
H
HDefalt2019-06-20 16:28:48
Python
HDefalt, 2019-06-20 16:28:48

Why does python 3 throw an error when using vk_api?

Code - here https://pastebin.com/EnpTEqnN
Error - here https://pastebin.com/9QcCuLHg
Tested on three machines - two - Windows, one - Linux
The first 5 minutes it works fine, after which it gives out this pile of errors. Python -3.5.3.
On the Internet, it was advised to make a delay (now it is at the end of the code). Tell me what to do, please!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Ternick, 2019-06-20
@Ternick

Try this :)
I don't really want to test it, so here it is:

# -*- 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)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question