Answer the question
In order to leave comments, you need to log in
How to send messages by VK bot?
Good day! I am new to Python programming and VK API. I want to send messages to users of the VKontakte bot.
That is, at some specific time, the bot will send a message to all people who have ever written a message to the bot. There is a lot of rubbish on the Internet and I did not find an answer to this question. I will be grateful for help.
import vk_api
import time
from vk_api.longpoll import VkLongPoll, VkEventType
key = "токен"
vk_session = vk_api.VkApi(token = key)
longpoll = VkLongPoll(vk_session)
vk = vk_session.get_api()
users = vk.users
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW and event.to_me and event.text :
if(event.text.lower() == "привет"):
name = users.get(user_ids = event.user_id,fields='first_name')[0]['first_name']
vk.messages.send(
user_id=event.user_id,
random_id=time.time(),
message="Здравствуй " + name
)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question