R
R
ruhgweg2021-12-04 06:28:59
Python
ruhgweg, 2021-12-04 06:28:59

How can I make it so that I can write the message "b" only after I have written the message "a"?

The code:

import vk_api
from key import token

vk = vk_api.VkApi(token=token)
vk._auth_token()
while True:
  messages = vk.method("messages.getConversations", {"offset": 0, "count": 20, "filter": "unanswered"})
  if messages["count"] >= 1:
    id = messages["items"][0]["last_message"]["from_id"]
    text = messages["items"][0]["last_message"]["text"]
    if text.lower() == "a":
      vk.method("messages.send", {"peer_id": id, "message": "a", "random_id": 0})
    elif text.lower() == "b":
      vk.method("messages.send", {"peer_id": id, "message": "b", "random_id": 0})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nvlveu, 2021-12-04
@nvlveu

You create a list. If the user wrote "a", then add his id. If he wrote "b" and his id is in the list, send the desired message and remove it ( list.remove(user.id) )

import vk_api

from key import token


vk = vk_api.VkApi(token=token)
vk._auth_token()


my_list = []


while True:
  messages = vk.method(
    "messages.getConversations",
    {"offset": 0, "count": 20, "filter": "unanswered"},
  )

  if messages["count"]:
    user_id = messages["items"][0]["last_message"]["from_id"]
    message_text = messages["items"][0]["last_message"]["text"]

    if message_text.lower() == "a":
      if user_id not in my_list:
        my_list.append(user_id)

      vk.method("messages.send", {"peer_id": id, "message": "a", "random_id": 0})
    elif message_text.lower() == "b":
      if user_id in my_list:
        vk.method("messages.send", {"peer_id": id, "message": "b", "random_id": 0})
        my_list.remove(user_id)
      else:
        ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question