M
M
mcjohnyx2020-01-30 15:43:01
Python
mcjohnyx, 2020-01-30 15:43:01

How to make a newsletter in VK bot every day at a certain time?

Hello.
I am writing a bot that should send text to everyone at a certain time.
The logic is already there. As well as binding to the database to take user_id from there.
Here are the code snippets from my bot:

import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
from vk_api.utils import get_random_id
import sqlite3
import time
from datetime import datetime as dt

token = токен
vk_session = vk_api.VkApi(token=token)
vk = vk_session.get_api()
longpool = VkBotLongPoll(vk_session, id группы)

__connection = None
def get_connection():  # Производится соединение с базой
    global __connection
    if __connection is None:
        __connection = sqlite3.connect('name.db')
    return __connection
def get_id(): #Проход по колонке user_id с пользователями из таблицы user || Возвращает список с кортежами с пользователями
    conn = get_connection()
    c = conn.cursor()
    c.execute('SELECT user_id FROM user')
    res = c.fetchall()
    return res

def send_message(user_id):
    vk.messages.send(
                user_id = user_id,
                message =ТЕКСТ,
                random_id = get_random_id() 
    )

while True:
    if dt.now().strftime('%H:%M') == 'Ч:М': #Ч-час, М-минута -- время отправки сообщений
        list_with_id = get_id() #
        for id in list_with_id:
            send.message(int(id[0]))
        time.sleep(60)
    for event in longpool.listen():
        #Логика бота

There may be a bug in the code, or I'm dumb. How to implement a mailing list in a bot?
PLEASE don't offer any mailings with callbacks etc. Mailing should come from this code.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2020-01-30
@anerev

It does not work because it refers to an infinite generator and therefore the time check occurs only once, at startup for event in longpool.listen():

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question