Answer the question
In order to leave comments, you need to log in
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
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.
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(' '):])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question