skip to Main Content

I want to make a Telegram bot to notify korea school meal but It has a problem

AttributeError: ‘list’ object has no attribute ‘data_filter’

I tried to modify the source code, but I was quite a beginner, so another error occurred when I tried to modify it.
I’m sorry to write such an unhelpful word.

koreans are not the cause of the error

import time
import logging
import os
import random

# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)

logger = logging.getLogger(__name__)

def help(bot, update):
  msg = "오늘의 급식 , /Todayn월요일 급식, /Monn화요일 급식, /Tuen수요일 급식, /Wedn목요일 급식, /Thun금요일 급식, /Fri"
  bot.sendMessage(update.message.chat_id, text=msg)

def iam(bot, update):
  chat_id = update.message.chat_id
  msg = "my chat_id is %d" %(chat_id)
  bot.sendMessage(chat_id=update.message.chat_id, text=msg)

def start(bot, update):
  chat_id = update.message.chat_id
  user = update.message.from_user
  msg = "오늘의 급식 , /Todayn월요일 급식, /Monn화요일 급식, /Tuen수요일 급식, /Wedn목요일 급식, /Thun금요일 급식, /Fri"
  bot.sendMessage(update.message.chat_id, text=msg)
### 여기를 수정하세요 ###

#급식 파싱 부분
def Today(bot, update):
  n = time.localtime().tm_wday
  user = update.message.from_user
  f = open(str(n) + ".txt", 'r')
  bot.sendMessage(update.message.chat_id, text= "오늘 급식n" + f.read())
  f.close()

def Mon(bot, update):
  n = time.localtime().tm_wday
  user = update.message.from_user
  f = open("0.txt", 'r')
  bot.sendMessage(update.message.chat_id, text= "이번주 월요일 급식n" + f.read())
  f.close()

def Tue(bot, update):
  n = time.localtime().tm_wday
  user = update.message.from_user
  f = open("1.txt", 'r')
  bot.sendMessage(update.message.chat_id, text= "이번주 화요일 급식n" + f.read())
  f.close()

def Wed(bot, update):
  n = time.localtime().tm_wday
  user = update.message.from_user
  f = open("2.txt", 'r')
  bot.sendMessage(update.message.chat_id, text= "이번주 수요일 급식n" + f.read())
  f.close()

def Thu(bot, update):
  n = time.localtime().tm_wday
  user = update.message.from_user
  f = open("3.txt", 'r')
  bot.sendMessage(update.message.chat_id, text= "이번주 목요일 급식n" + f.read())
  f.close()

def Fri(bot, update):
  n = time.localtime().tm_wday
  user = update.message.from_user
  f = open("4.txt", 'r')
  bot.sendMessage(update.message.chat_id, text= "이번주 금요일 급식n" + f.read())
  f.close()

def query(msg) :
  return msg

def response(bot, update):
  chat_id = update.message.chat_id
  user = update.message.from_user
  user_name = "%s%s" %(user.last_name, user.first_name)

  r_msg = query(update.message.text)
  bot.sendMessage(chat_id,
                  text=r_msg)

def error(bot, update, error):
  logger.warn('Update "%s" caused error "%s"' % (update, error))

def main():
  # Create the EventHandler and pass it your bot's token.
  with open(os.path.join('/home/pi/Desktop/meal_bot','my.keyfile'), 'r') as f :
    token = f.readline().strip()

  updater = Updater(token)

  # Get the dispatcher to register handlers
  dp = updater.dispatcher

  # on different commands - answer in Telegram
  dp.add_handler(CommandHandler("start", start))
  dp.add_handler(CommandHandler("help", help))
  dp.add_handler(CommandHandler("iam", iam))
  dp.add_handler(CommandHandler("Today", Today))
  dp.add_handler(CommandHandler("Mon", Mon))
  dp.add_handler(CommandHandler("Tue", Tue))
  dp.add_handler(CommandHandler("Wed", Wed))
  dp.add_handler(CommandHandler("Thu", Thu))
  dp.add_handler(CommandHandler("Fri", Fri))

  # on noncommand i.e message - echo the message on Telegram
  dp.add_handler(MessageHandler([Filters.text], response))

  # log all errors
  dp.add_error_handler(error)

  # Start the Bot
  updater.start_polling()

  # Run the bot until you press Ctrl-C or the process receives SIGINT,
  # SIGTERM or SIGABRT. This should be used most of the time, since
  # start_polling() is non-blocking and will stop the bot gracefully.
  updater.idle()

if __name__ == '__main__':
  main()

this code respond

  File "bot.py", line 123, in <module>
    main()
  File "bot.py", line 109, in main
    dp.add_handler(MessageHandler([Filters.text], response))
  File "/home/pi/.local/lib/python3.7/site-packages/telegram/ext/messagehandler.py", line 150, in __init__
    self.filters = Filters.update & filters
  File "/home/pi/.local/lib/python3.7/site-packages/telegram/ext/filters.py", line 123, in __and__
    return MergedFilter(self, and_filter=other)
  File "/home/pi/.local/lib/python3.7/site-packages/telegram/ext/filters.py", line 257, in __init__
    and self.and_filter.data_filter
AttributeError: 'list' object has no attribute 'data_filter' 

it can be solved?

2

Answers


  1. You are converting Filters.text to a list. Remove the square brackets in MessageHandler method.

    dp.add_handler(MessageHandler(Filters.text, response))
    
    Login or Signup to reply.
  2. Your issue is in all probability coming from this piece of code
    dp.add_handler(MessageHandler([Filters.text], response)).

    You might want to change the MessageHandler to receive a single object. Something like
    dp.add_handler(MessageHandler(Filters.text, response))

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search