L
L
loadi2017-08-10 12:24:12
Python
loadi, 2017-08-10 12:24:12

How to count vk message in python?

How to count the number after the message?
A person writes to a bot in VK, for example / play 20. How to read his message and pull out a number from there?

def write_msg(chat_id, s):
    vk.method('messages.send', {'chat_id':9,'message':s})
while True:
    response = vk.method('messages.get', values)
    if response['items']:
         values['last_message_id'] = response['items'][0]['id']
    for item in response['items']:        
        if response['items'][0]['body'] == '/play':         #play
            bet = ????

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Max Payne, 2017-08-11
@YardalGedal

You can use LongPull vk.com/dev/using_longpoll , suitable for bot pages and bot groups. But there is some chance of losing messages (based on personal experience).
You can use the CallBack API vk.com/dev/callback_api , only group bots are suitable, higher stability, but the response speed is lower due to the additional layer from the web server.
Specifically, there are two methods to pull out a number:
The first one is through re , the second one is through a string search ( ~2 times faster ):
Let's test

>>> f('/play 20', '/play')
'20'

U
userfordownload, 2017-08-13
@userfordownload

s = '/play 20'
 integ = ''.join([i if i.isdigit() else ' ' for i in s])
 print integ.split()

c3cd8ffb37244d329bbe9fd821dfc45d.PNG

J
jasonOk, 2017-08-10
@jasonOk

https://vk.com/dev/callback_api

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question