Answer the question
In order to leave comments, you need to log in
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():
#Логика бота
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