Answer the question
In order to leave comments, you need to log in
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
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'
s = '/play 20'
integ = ''.join([i if i.isdigit() else ' ' for i in s])
print integ.split()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question