R
R
Renat Ataev2019-12-12 19:58:05
Python
Renat Ataev, 2019-12-12 19:58:05

I don't understand where is the error in the code?

There is an error in the code, I can not understand where it is ..

Traceback (most recent call last):
  File "v2.py", line 14, in <module>
    for event in longpoll.listen():
  File "C:\Users\DNS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\vk_api\longpoll.py", line 621, in listen
    for event in self.check():
  File "C:\Users\DNS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\vk_api\longpoll.py", line 560, in check
    timeout=self.wait + 10
TypeError: can only concatenate str (not "int") to str

The code itself
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
import random
import time
from datetime import datetime, timedelta

vk_session = vk_api.VkApi(token='Токен')
longpoll = VkLongPoll(vk_session, 'Ид группы')
vk = vk_session.get_api()


for event in longpoll.listen():
    if event.type == VkEventType.MESSAGE_NEW:
        print('Сообщение пришло в: ' + str(event.datetime + timedelta(hours=3)))
        print('Текст сообщения: ' + str(event.text))
        print(event.peer_id)
        
        response = event.text.lower()
        if event.from_chat or event.from_user:
            try:
                if response == '/status' or response == '/статус':

                    today = datetime.strftime(datetime.now(), "%d%m%Y")
                    tomorrow = datetime.strftime(datetime.now() + timedelta(days=1), "%d%m%Y")
    
                    vk.messages.send(
                        peer_id=event.peer_id,
                        random_id=random.randint(-2147483648, +2147483648),
                        message='Всего сообщений в данном диалоге : ' +
                                str(vk.messages.search(peer_id=event.peer_id, date=tomorrow, count=1)['count'] + 1) + '\nЗа сегодня: ' +
                                str( vk.messages.search(peer_id=event.peer_id, date=tomorrow, count=1)['count'] + 1 - vk.messages.search(peer_id=event.peer_id, date=today, count=1)['count'] + 1),
                    )
            except:
                pass

            finally:
                time.sleep(3)

Please explain where they are, as well as their solution.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2019-12-12
@anerev

can only concatenate str (not "int") to str

G
German Averin, 2019-12-13
@averines

timeout=self.wait + 10
here, with the help of a plus sign, concatenation (combining strings) occurs, since one of the elements is taken as a string.
if we want to add, both terms must be digits.
let's do it like this:
timeout=int(self.wait) + 10

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question