S
S
shuzzixd2021-06-07 19:00:15
Python
shuzzixd, 2021-06-07 19:00:15

How to make a newsletter for users in a telegram bot?

Well, for example, I write like this: /send Text. After that, a Text message is sent to all users.

Important: the command should be accessible only to the admin.
I have a database with the id of people who use the bot. (All/id.txt)

Using telebot

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
UberPool, 2021-06-07
@UberPool

You send a message in a cycle, what's the difficulty?

A
Alexander, 2021-06-07
@shabelski89

import telebot

API_TOKEN = ""

bot = telebot.TeleBot(API_TOKEN)
admins = [111, 222]

@bot.message_handler(commands=['send'])
def notify(message):
    command_sender = message.from_user.id
    if command_sender in admins:
        with open(r'C:\id.txt') as ids:
            for line in ids:
                user_id = int(line.strip("\n"))
                try:
                    bot.send_message(user_id,  f'уведомление от {command_sender}')
                except Exception as e:
                    bot.send_message(command_sender, f'ошибка отправки сообщения юзеру - {user_id}')
    else:
        bot.send_message(command_sender, f'у вас нет прав для запуска команды')


if __name__ == "__main__":
    try:
        bot.polling(none_stop=True)
    except Exception as e:
        pass

fileid.txt _
12345
777
34521
56213

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question