I
I
iliaaaaaaaaaaaaaa2021-08-16 18:51:07
Python
iliaaaaaaaaaaaaaa, 2021-08-16 18:51:07

How to make a newsletter in a telegram bot?

I wrote the code, I can not understand what exactly is wrong.
The first part of the code should, when activating /start, write the user id to joined.txt
The second part of the code:
Starts sending text to all id in joined.txt, if the admin writes the command /special + text

import telebot
bot = telebot.TeleBot('')
joinedFile = open("joined.txt", "r")
joinedUsers = set ()
for line in joinedFile:
  joinedUsers.add(line.strip())
joinedFile.close()

@bot.message_handler(commands=['start'])
def startjoin(message):
  if not str(message.chat.id) in joinedUsers:
    joinedFile = open("joined.txt", "a")
    joinedFile.write(str(message.chat.id) + "\n")
    joinedUsers.add(message.chat.id)

@bot.message_handler(commands=['special'])
def mess(message):
  for user in joinedUsers:
    bot.send_message(user, message.text[message.text.find(' '):])

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Krostelev, 2021-08-16
@twistfire92

First, it would be wiser to use databases for such cases.
Secondly, after writing the file, the file is not closed (study the construction with open(...) as... ), this may be the problem.
Well, most likely you only have a typo in the attached code and some quotes are missing.
Yes, and the cart has restrictions on sending messages for a period of time.

Z
Zagir Majidov, 2021-08-16
@Zagir-vip

In a team:

@bot.message_handler(commands=['special'])
def mess(message):
  for user in joinedUsers:
    bot.send_message(user, message.text[message.text.find(' '):])

Вы открыли файл joinedUsers и закрыли попробуйте этот код:

@bot.message_handler(commands=['special'])
def mess(message):
  joinedFile = open("joined.txt", "r")
  joinedUsers = set ()
  for user in joinedUsers:
    bot.send_message(user, message.text[message.text.find(' '):])

if it doesn't work please send me an error or contact me in discord: Xpeawey#6098

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question