G
G
GetLuck022017-10-30 14:24:42
Python
GetLuck02, 2017-10-30 14:24:42

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)

please help how to do it right and beautiful)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GetLuck02, 2017-10-31
@GetLuck02

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)

fixed the error,
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 question

Ask a Question

731 491 924 answers to any question