D
D
Dmitry2017-03-12 16:42:51
Python
Dmitry, 2017-03-12 16:42:51

How to automatically respond to the interlocutor in Python + vk api when receiving a new letter?

Greetings, in general, I drank all the sedatives that were in the house and my legs hurt from squats
, there is a piece of code where I log in and then I need to track the appearance of a new letter, I use messages.getDialogs with parameters count=20 and unread=1 but when I run this I get a mat from ide in the form vk.exceptions.VkAPIError: 100.

import vk

session = vk.AuthSession(
                        'app_id',
                        'login',
                        'pass',
                        scope='messages'
                        )
vk_api = vk.API(session)

massages=vk_api.messages.getDialogs(count=20, unread=1)

print(massages)

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
Dmitry, 2017-03-13
@ipatov_dn

import vk
import time
import requests as req
from lxml import html

app_id, login, password = 'app_id', '[email protected]', 'password'
session = vk.AuthSession(app_id, login, password, scope='messages')
vk_api = vk.API(session, v='5.62')

while True:
    massages=vk_api.messages.getDialogs(count=20, unread=1)
   
    if massages['count']==1:
        print('Новое письмо', massages)
        id = massages['items'][0]['message']['user_id']
        body=massages['items'][0]['message']['body']
        print(id, body)
        if body=='bash':
            r = req.get('http://bash.im/random')
            doc = html.document_fromstring(r.text)
            bash = '\n'.join(doc.xpath('//*[@id="body"]/div[3]/div[@class="text"]/text()'))
            vk_api.messages.send(user_id=id, message=bash)
            print()
        else:
            vk_api.messages.send(user_id=id, message='Если вы введете "bash" то я вышлю вам цитату с bash.im')
    elif massages['count']==0:
        print('Новых писем нет')
    time.sleep(1)

thanks for the hint with the prawn-cake version

P
prawn-cake, 2017-03-13
@prawn-cake

Try https://github.com/prawn-cake/vk-requests
The out-of-the-box library handles a lot of cases and it has fixed a lot of bugs compared to vk. Well, she is also python 3 compatible.
In the case of replying to messages, for example, it might look like this

api = vk_requests.create_api(app_id=123, login='User', password='Password', scope=['offline', 'messages'])
messages = api.messages.getDialogs(count=20, unread=1)

for msg in messages['items']:
    api.messages.send(peer_id=msg['user_id'], message='Hello world')

G
gameover74, 2017-04-17
@gameover74

Using frequent requests of the same type to the VK API will, at best, manage to ignore these requests.
In your case, it is better to use this method for private messages - https://vk.com/dev/messages.getLongPollServer.
In case of messages to the community - https://vk.com/dev/callback_api.

D
Dmitry Eremin, 2017-12-19
@zetixl

2 == "2" //true
2 === "2" //false

M
maxsnw, 2017-12-19
@maxsnw

Ordinary equality and strict

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question