M
M
madwayz13372017-10-12 12:03:53
Python
madwayz1337, 2017-10-12 12:03:53

A few questions. How to get username via vk_api and how to use these lists?

I have a write_msg function which is responsible for sending a message to the chat. So, the bot responds only to those messages that are in the condition (For example:

if response['items'] [0] ['body'] == 'Бот, привет!':
). I want the bot to respond with "Hi %Name_who_greeted_the_bot%!". That is, I should get something like this:
write_msg(item['chat_id'], 'Привет, ' + First_Name + '!')

Second question. I have a list:
{'count': 26432, 'items': [{'id': 26937, 'date': 1507798270, 'out': 0, 'user_id': 120120065, 'read_state': 0, 'title': 'TEBE YASNO', 'body': 'Бот, привет!', 'chat_id': 1, 'chat_active': [120120065, 209290083, 105369018, 296949410, 77650655, 135621310, 379105922, 440779645, 83224695, 158586845, 82260744, 321894043, 120053992, 65727257, 153087155, 144552756, 209915865, 203559835, 268289836, 136539852, 152911879, 245883365, 166219354, 207565891, 136471997, 43424577, 143311426, 193283710, 99372495, 152500956, 351643366, 183801556, 136775206, 105291381, 3260004, 100677153, 210047227, 249947860, 400898877, 400916678, 440888304], 'users_count': 42, 'admin_id': 82260744, 'photo_50': 'https://pp.userapi.com/c840334/v840334790/ef87/GE-dxCQs4jc.jpg', 'photo_100': 'https://pp.userapi.com/c840334/v840334790/ef86/RkVLuv9BOe4.jpg', 'photo_200': 'https://pp.userapi.com/c840334/v840334790/ef84/8LcffcsFScs.jpg'}]}

and code
response = vk.method('messages.get', values)
    print(response)
    if response['items']:
        values['last_message_id'] = response['items'][0]['id']

    for item in response['items']:
        print('> Новое сообщение: ' + item['body'])

        if response['items'] [0] ['body'] == 'Бот, привет!':

I would like an explanation with the code below.
if response['items'] [0] ['body'] == 'Бот, привет!':

So, what does [0] and [items] do?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question