A
A
Arthur2021-10-06 22:19:51
Python
Arthur, 2021-10-06 22:19:51

Newsletter on telebot?

Hello. There is a database where the id of users who used the bot

is stored, the file name is user.db
The table is called login_id and the id is stored in id

Tell me how you can make a newsletter?

@bot.message_handler(commands=['start'])
def start(message):
    bot.send_message(message.chat.id, 'Пᴘивᴇт, мᴇня зᴀвʏт   \n Я являюсь твоим помоωником в иrᴘᴇ с чᴇм тᴇҕᴇ помочь ? ', reply_markup=markup)
    bot.send_photo(message.chat.id, get("https://i.imgur.com/qyn4g24.jpg").content)
 
    #data base
    connect = sqlite3.connect('users.db')
    cursor = connect.cursor()
    cursor.execute("""CREATE TABLE IF NOT EXISTS login_id(
        id INTEGER
    )""")
    connect.commit()
#повтор ид
    people_id = message.chat.id
    cursor.execute(f"SELECT id FROM login_id WHERE id = {people_id}")
    data = cursor.fetchone()
    if data is None :
        user_id = [message.chat.id]
        cursor.execute("INSERT INTO login_id VALUES(?);", user_id)
        connect.commit()
 
    else:
        bot.send_message(message.chat.id, 'С возврашением')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
InternetMaster, 2021-10-06
@TellS

Telegram does not have a function to send a message to all users at once. Therefore, it is necessary to store the user_id of all users. If this is already there, then implement the simplest distribution mechanism via sendMessage (according to the official Telegram documentation )

def rassilka(message):
    cursor.execute('SELECT id FROM login_id')
    result = cursor.fetchall()
    msg = '[Тут рассылаемое сообщение]'
    for x in result:
        bot.send_message(x[YOU_BD_NUMBER], str(msg))

PS [YOU_BD_NUMBER] is the number of the id column in login_id starting from 0. If there is only 1 column in login_id then write result[0], if the column comes second then result[1], etc.
Also, users can block the bot (leave it), in this case, the telegram will not deliver the message and notify about it. You can, for example, implement code that removes id from the user base if they failed to deliver the message. So even the database will be updated, and there will be actual users in it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question