V
V
Vinogradov2022-03-11 21:36:38
Python
Vinogradov, 2022-03-11 21:36:38

How to make a vkbottle mailing list by time?

It is necessary to send out a timer: I use the schedule library for this .
As soon as the time reaches the desired time, an error appears in the console:
NameError: name 'message' is not defined

Here is the code:

vk_session = VkApi(token=tok)
vk = vk_session.get_api()
bot = Bot(token=tok)

def main_loop():
  thread = Thread(target=do_schedule)
  thread.start()

def NAME():
  vk.messages.send(
    
        message ="TEXT",
        chat_id = message.chat_id,# Если нужно выслать в чат
        #peer_id = user_id,  # Если нужно выслать в ЛС
        random_id=0)

def do_schedule():
  schedule.every().day.at("21:29").do(NAME)
  while True:
    schedule.run_pending()
    time.sleep(1)

if __name__ == '__main__':
  main_loop()
  bot.run_forever()


I kind of understand that the bot cannot find the conversation to which you want to send a message, but I don’t know how to specify it dynamically so that it can make such mailings in any conversation

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wearant, 2022-03-11
@VinogradovDionis

Vinogradov ,

from vk_api import VkApi
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType


group_id = "*****"
TOKEN = "*****"

vk_session = VkApi(token=TOKEN)
longpoll = VkBotLongPoll(vk_session, group_id)
vk = vk_session.get_api()

def main():
        for event in longpoll.listen(): 
            if event.type == VkBotEventType.MESSAGE_NEW and event.from_chat:
                chat_id = event.chat_id
                print(chat_id)
                
if __name__ == '__main__':
    main()

With the help of this code, you can find out from which conversation the message ( id ) came from in the console
622b9b0598d0d212979730.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question