Answer the question
In order to leave comments, you need to log in
How to send messages to users of the VK bot?
Hello, maybe you will say that there are a lot of such topics on the Internet, but it does not help me, still I would like to get help from you.
I have a regular VK bot that responds to messages (Hello -> hi). How can I implement sending a message to all users of the bot at a certain time, I tried many options for shedul, realtime, nothing happened.
I want to make the bot, for example, at 12:00 write to all users "Good day", and then in the evening at 18:00 "Good evening"
Code:
import random
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
def write_msg(user_id, message):
vk.method('messages.send', {'user_id': user_id, 'message': message, 'random_id': random.randint(0, 2048)})
# API-ключ созданный ранее
token = "key"
# Авторизуемся как сообщество
vk = vk_api.VkApi(token=token)
# Работа с сообщениями
longpoll = VkLongPoll(vk)
print("Бот запущен")
# Основной цикл
for event in longpoll.listen():
# Если пришло новое сообщение
if event.type == VkEventType.MESSAGE_NEW:
# Если оно имеет метку для меня( то есть бота)
if event.to_me:
# Сообщение от пользователя
request = event.text
# Каменная логика ответа
if request == "привет".title():
write_msg(event.user_id, "Хай")
elif request == "пока":
write_msg(event.user_id, "Пока((")
else:
write_msg(event.user_id, "что?")
Answer the question
In order to leave comments, you need to log in
You create a thread, it has an infinite loop that checks the time and sends out if necessary
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question