this is a simple code for telegram bot.
I have a list, When the user types “m”, its name is added to the list and the robot publishes the list,
When the user types “n”, the name is deleted from the list and publishes the list,
When the number of the list is equal to 2, the bot should say “Done: and not receive another amount When the number of the list is equal to 0 The robot should say “No one” and publishes the list.
but when I run this code,when list equals to 0 nothing happens!
# -*- coding:utf-8 -*-
#coding=UTF-8
from telegram.ext import Updater , CommandHandler , Filters ,
CommandHandler , MessageHandler
from telegram import MessageEntity
from telegram import ParseMode , InputTextMessageContent
updater = Updater("BOT TOKEN")
listt = []
def msg_filter(bot , update):
wordsp = ['m']
wordsn = ['n']
if any (i in update.message.text for i in wordsp) and "
{}".format(update.message.from_user.first_name) not in listt:
listt.append("{}".format(update.message.from_user.first_name))
bot.send_message(chat_id = update.message.chat_id , text =
"n".join(listt))
bot.send_message(chat_id = update.message.chat_id , text = len(listt))
if len(listt)==2:
bot.send_message(chat_id = update.message.chat_id , text =
"Done")
if any (i in update.message.text for i in wordsn) and "
{}".format(update.message.from_user.first_name) in listt:
listt.remove("{}".format(update.message.from_user.first_name))
bot.send_message(chat_id = update.message.chat_id , text =
"n".join(listt))
bot.send_message(chat_id = update.message.chat_id , text = len(listt))
if len(listt)==0:
bot.send_message(chat_id = update.message.chat_id , text =
"nobody")
print(listt)
print(len(listt))
updater.dispatcher.add_handler(MessageHandler(Filters.text , msg_filter ))
updater.start_polling()
3
Answers
I’m guessing this will solve your indentation issues:
Think about the order of execution.
I.e. you have not changed the list before you print it.
You need to move the print statements inside the function. I also cleaned up your code as it appears broken in your question.
Your function takes 2 arguments but never called ? or am I missing something here. You can declare the list as global like this so it wont end up empty, after the function is called.