Answer the question
In order to leave comments, you need to log in
Bot vk how to find the right word in a line?
The essence of the problem : the
user sends a message to the bot , the bot must check if the message contains a certain string . Example:
a message was received: blablabla
bot checks if "blablabla" contains "a", the bot answers "yeah"
I tried to do it this way, I didn't get an error, but there was no result either:
# -*- coding: utf-8 -*-
import time
import vk_api
import string
vk = vk_api.VkApi(login = '********', password = '*****')
vk.auth()
values = {'out': 0,'count': 100,'time_offset': 60}
response = vk.method('messages.get', values)
def write_msg(user_id, s):
vk.method('messages.send', {'user_id':user_id,'message':s})
while True:
if response['items']:
values['last_message_id'] = response['items'][0]['id']
for item in response['items']:
string = response['items']['body']
if 'a' in string:
write_msg(item['user_id','ага'])
time.sleep(1)
Answer the question
In order to leave comments, you need to log in
I came up with the solution myself, it turned out everything was very simple
:
while True:
if response['items']:
values['last_message_id'] = response['items'][0]['id']
for item in response['items']:
string = response['items']['body']
if 'a' in string:
write_msg(item['user_id','ага'])
time.sleep(1)
while True:
if response['items']:
values['last_message_id'] = response['items'][0]['id']
for item in response['items']:
string = response['items'][0]['body'] #забыл ноль
if 'a' in string:
write_msg(item['user_id]','ага")
time.sleep(1)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question