santhoshtr / telegram-rss-reader

A telegram bot to read RSS feeds
MIT License
14 stars 4 forks source link

Script starts but produces errors for any actions taken in Telegram #4

Open J-Wick4 opened 2 years ago

J-Wick4 commented 2 years ago

I'm new to Python and I've played around with multiple Python Telegram RSS scripts, this one I believe is the best in the world if it can actually work.

I've confirmed communications with the Telegram Channel I've created using another Python script to send messages to it.

When I run bot.py I'm getting the following error on any action from /help, /list /add feed, etc.

_2022-03-16 22:18:06,653 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception. Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/telegram/ext/dispatcher.py", line 442, in process_update handler.handle_update(update, self, check, context) File "/usr/local/lib/python3.6/site-packages/telegram/ext/handler.py", line 160, in handle_update return self.callback(update, context) File "bot.py", line 68, in text text_received = update.message.text AttributeError: 'NoneType' object has no attribute 'text' 2022-03-16 22:18:06,657 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception. Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/telegram/ext/dispatcher.py", line 442, in process_update handler.handle_update(update, self, check, context) File "/usr/local/lib/python3.6/site-packages/telegram/ext/handler.py", line 160, in handle_update return self.callback(update, context) File "bot.py", line 68, in text textreceived = update.message.text AttributeError: 'NoneType' object has no attribute 'text'

Steps:

System Rocky Linux 8.5 Python 3.6.8

Installed Modules: [root@localhost telegram-rss-reader]# pip3 list Package Version


appdirs 1.4.4 APScheduler 3.6.3 asgiref 3.4.1 async-lru 1.0.2 attrs 20.3.0 backports.zoneinfo 0.2.1 beautifulsoup4 4.9.3 bleach 3.3.0 cachetools 4.2.2 certifi 2020.12.5 chardet 3.0.4 click 7.1.2 configobj 5.0.6 cssselect 0.9.2 dbus-python 1.2.4 decorator 4.2.1 distlib 0.3.1 dj-database-url 0.5.0 Django 3.2.12 feedparser 6.0.2 filelock 3.0.12 gpg 1.13.1 html5lib 0.999999999 idna 2.5 importlib-metadata 3.7.0 importlib-resources 5.1.1 isc 2.0 Jinja2 2.11.3 lazy-object-proxy 1.5.2 libcomps 0.1.16 lxml 4.2.3 MarkupSafe 1.1.1 nftables 0.1 packaging 20.9 perf 0.1 pexpect 4.3.1 pip 21.3.1 pipenv 2022.1.8 platformdirs 2.4.0 ply 3.9 psutil 5.4.3 psycopg 3.0.10 psycopg2-binary 2.9.3 ptyprocess 0.5.2 pyaes 1.6.1 pyasn1 0.4.8 pycairo 1.16.3 pydbus 0.6.0 PyGObject 3.28.3 pyparsing 2.4.7 Pyrogram 1.0.1 PySocks 1.7.1 python-dateutil 2.8.1 python-dotenv 0.19.2 python-linux-procfs 0.6.3 python-telegram-bot 13.3 pytz 2021.1 pytz-deprecation-shim 0.1.0.post0 pyudev 0.21.0 PyYAML 3.12 regex 2020.11.13 requests 2.20.0 rpm 4.14.3 rsa 4.8 savepagenow 1.1.1 selinux 2.9 sepolicy 1.1 setools 4.3.0 setroubleshoot 1.1 setuptools 59.6.0 sgmllib3k 1.0.0 six 1.15.0 slip 0.6.4 slip.dbus 0.6.4 sos 4.1 soupsieve 2.2 SQLAlchemy 1.3.23 sqlparse 0.4.2 SSSDConfig 2.5.2 syspurpose 1.28.21 systemd-python 234 telebot 0.0.4 telegram-rss 0.8.1 Telethon 1.24.0 toml 0.10.2 tornado 6.1 typed-ast 1.4.2 typing_extensions 4.1.1 tzdata 2021.5 tzlocal 2.1 urllib3 1.24.2 virtualenv 20.4.2 virtualenv-clone 0.5.7 wcwidth 0.2.5 webencodings 0.5.1 wrapt 1.12.1 zipp 3.4.0

If you can provide a fix, I'll toss you a few dollars over PayPal.

santhoshtr commented 2 years ago

I suspect the problem is because of old python version. I have not tested this tool in python version below 3.8. Latest ubuntu comes wth python3.9 or even python3.10. I use this bot for many months with python 3.9

I just tried a fresh installation of this tool with python3.9 and found it is working as expected. To help you: I am giving exact output from the steps:

git clone https://github.com/santhoshtr/telegram-rss-reader
Cloning into 'telegram-rss-reader'...
remote: Enumerating objects: 48, done.
remote: Counting objects: 100% (48/48), done.
remote: Compressing objects: 100% (33/33), done.
remote: Total 48 (delta 23), reused 36 (delta 12), pack-reused 0
Receiving objects: 100% (48/48), 15.15 KiB | 7.57 MiB/s, done.
Resolving deltas: 100% (23/23), done.              

Now go to that directory and create a python virtual env

➜  ~/tmp cd telegram-rss-reader                                                                                                                                                                                  
➜  ~/tmp/telegram-rss-reader git:(master) python3.9 -m venv ENV_DIR
➜  ~/tmp/telegram-rss-reader git:(master) source ENV_DIR/bin/activate                                                                                                                                            
(ENV_DIR) ➜  ~/tmp/telegram-rss-reader git:(master) pip install -r requirements.txt 
Collecting feedparser
Using cached feedparser-6.0.8-py3-none-any.whl (81 kB)
Collecting python-telegram-bot
ca  Downloading python_telegram_bot-13.11-py3-none-any.whl (497 kB)
|████████████████████████████████| 497 kB 792 kB/s 
Collecting python-dotenv
Using cached python_dotenv-0.19.2-py2.py3-none-any.whl (17 kB)
Collecting savepagenow
Using cached savepagenow-1.1.1-py2.py3-none-any.whl (6.4 kB)
Collecting sgmllib3k
Using cached sgmllib3k-1.0.0.tar.gz (5.8 kB)
Collecting certifi
Using cached certifi-2021.10.8-py2.py3-none-any.whl (149 kB)
Collecting APScheduler==3.6.3
Using cached APScheduler-3.6.3-py2.py3-none-any.whl (58 kB)
Collecting cachetools==4.2.2
Using cached cachetools-4.2.2-py3-none-any.whl (11 kB)
RCollecting tornado>=6.1
Using cached tornado-6.1-cp39-cp39-manylinux2010_x86_64.whl (427 kB)
Collecting pytz>=2018.6
Using cached pytz-2021.3-py2.py3-none-any.whl (503 kB)
Collecting tzlocal>=1.2
Using cached tzlocal-4.1-py3-none-any.whl (19 kB)
ECollecting six>=1.4.0
Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Requirement already satisfied: setuptools>=0.7 in ./ENV_DIR/lib/python3.9/site-packages (from APScheduler==3.6.3->python-telegram-bot->-r requirements.txt (line 2)) (44.1.1)
Collecting pytz-deprecation-shim
Using cached pytz_deprecation_shim-0.1.0.post0-py2.py3-none-any.whl (15 kB)
Collecting click
Downloading click-8.0.4-py3-none-any.whl (97 kB)
|████████████████████████████████| 97 kB 5.0 MB/s 
Collecting requests>=2.20.0
Using cached requests-2.27.1-py2.py3-none-any.whl (63 kB)
ACollecting urllib3<1.27,>=1.21.1
Downloading urllib3-1.26.9-py2.py3-none-any.whl (138 kB)
|████████████████████████████████| 138 kB 18.9 MB/s 
Collecting idna<4,>=2.5
Using cached idna-3.3-py3-none-any.whl (61 kB)
Collecting charset-normalizer~=2.0.0
Using cached charset_normalizer-2.0.12-py3-none-any.whl (39 kB)
Collecting tzdata
Using cached tzdata-2021.5-py2.py3-none-any.whl (339 kB)
Using legacy 'setup.py install' for sgmllib3k, since package 'wheel' is not installed.
Installing collected packages: tzdata, pytz-deprecation-shim, urllib3, tzlocal, six, pytz, idna, charset-normalizer, certifi, tornado, sgmllib3k, requests, click, cachetools, APScheduler, savepagenow, python-telegram-bot, python-dotenv, feedparser
Running setup.py install for sgmllib3k ... done
Successfully installed APScheduler-3.6.3 cachetools-4.2.2 certifi-2021.10.8 charset-normalizer-2.0.12 click-8.0.4 feedparser-6.0.8 idna-3.3 python-dotenv-0.19.2 python-telegram-bot-13.11 pytz-2021.3 pytz-deprecation-shim-0.1.0.post0 requests-2.27.1 savepagenow-1.1.1 sgmllib3k-1.0.0 six-1.16.0 tornado-6.1 tzdata-2021.5 tzlocal-4.1 urllib3-1.26.9  

Create database with required tables:

(ENV_DIR) ➜  ~/tmp/telegram-rss-reader git:(master) sqlite3 reader.db
SQLite version 3.35.5 2021-04-19 18:32:05
Enter ".help" for usage hints.
sqlite> CREATE TABLE IF NOT EXISTS "sources" (
"userid"        TEXT NOT NULL,
"url"   TEXT NOT NULL,
"last_updated"  INTEGER NOT NULL,
PRIMARY KEY("url","userid")
);

Have the .env file with the bot tokens.

Run the bot

(ENV_DIR) ➜  ~/tmp/telegram-rss-reader git:(master) python bot.py
2022-03-17 10:04:36,631 - apscheduler.scheduler - INFO - Adding job tentatively -- it will be properly scheduled when the scheduler starts
2022-03-17 10:04:36,632 - apscheduler.scheduler - INFO - Added job "fetch_feeds" to job store "default"
2022-03-17 10:04:36,632 - apscheduler.scheduler - INFO - Scheduler started
2022-03-17 10:04:46,607 - apscheduler.executors.default - INFO - Running job "fetch_feeds (trigger: interval[0:30:00], next run at: 2022-03-17 04:34:46 UTC)" (scheduled at 2022-03-17 04:34:46.607086+00:00)
2022-03-17 10:04:46,608 - apscheduler.executors.default - INFO - Job "fetch_feeds (trigger: interval[0:30:00], next run at: 2022-03-17 05:04:46 UTC)" executed successfully
^C2022-03-17 10:05:05,066 - telegram.ext.updater - INFO - Received signal 2 (SIGINT), stopping...
2022-03-17 10:05:05,066 - apscheduler.scheduler - INFO - Scheduler has been shut down
^C2022-03-17 10:05:05,617 - telegram.ext.updater - WARNING - Exiting immediately!    

Python3.6 was releases in 2016 and it is not supported. Would it be possible to try with a recent python versions and see if it works?

J-Wick4 commented 2 years ago

Thank you for that detailed explanation of steps, I've cleaned up my system and installed Python 3.9, and created a virtual environment, mirroring all your steps.

(UWN) [root@localhost telegram-rss-reader]# python --version
Python 3.9.6

(UWN) [root@localhost telegram-rss-reader]# pip3.9 list
Package               Version
--------------------- -----------
APScheduler           3.6.3
cachetools            4.2.2
certifi               2021.10.8
charset-normalizer    2.0.12
click                 8.0.4
feedparser            6.0.8
idna                  3.3
pip                   22.0.4
python-dotenv         0.19.2
python-telegram-bot   13.11
pytz                  2021.3
pytz-deprecation-shim 0.1.0.post0
requests              2.27.1
savepagenow           1.1.1
setuptools            50.3.2
sgmllib3k             1.0.0
six                   1.16.0
tornado               6.1
tzdata                2021.5
tzlocal               4.1
urllib3               1.26.9

I still get errors when I try and run any of the commands from the Telegram Channel such as help, list, add, etc.

(UWN) [root@localhost telegram-rss-reader]# python bot.py
2022-03-17 01:27:44,634 - apscheduler.scheduler - INFO - Adding job tentatively -- it will be properly scheduled when the scheduler starts
2022-03-17 01:27:44,635 - apscheduler.scheduler - INFO - Added job "fetch_feeds" to job store "default"
2022-03-17 01:27:44,635 - apscheduler.scheduler - INFO - Scheduler started
2022-03-17 01:27:54,428 - apscheduler.executors.default - INFO - Running job "fetch_feeds (trigger: interval[0:01:00], next run at: 2022-03-17 06:27:54 UTC)" (scheduled at 2022-03-17 06:27:54.422964+00:00)
2022-03-17 01:27:54,434 - apscheduler.executors.default - INFO - Job "fetch_feeds (trigger: interval[0:01:00], next run at: 2022-03-17 06:27:54 UTC)" executed successfully

2022-03-17 01:28:17,043 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception.
Traceback (most recent call last):
  File "/root/telegram-rss-reader/UWN/lib64/python3.9/site-packages/telegram/ext/dispatcher.py", line 555, in process_update
    handler.handle_update(update, self, check, context)
  File "/root/telegram-rss-reader/UWN/lib64/python3.9/site-packages/telegram/ext/handler.py", line 198, in handle_update
    return self.callback(update, context)
  File "/root/telegram-rss-reader/bot.py", line 68, in text
    text_received = update.message.text
AttributeError: 'NoneType' object has no attribute 'text'

2022-03-17 01:28:39,097 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception.
Traceback (most recent call last):
  File "/root/telegram-rss-reader/UWN/lib64/python3.9/site-packages/telegram/ext/dispatcher.py", line 555, in process_update
    handler.handle_update(update, self, check, context)
  File "/root/telegram-rss-reader/UWN/lib64/python3.9/site-packages/telegram/ext/handler.py", line 198, in handle_update
    return self.callback(update, context)
  File "/root/telegram-rss-reader/bot.py", line 68, in text
    text_received = update.message.text
AttributeError: 'NoneType' object has no attribute 'text'

2022-03-17 01:28:51,031 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception.
Traceback (most recent call last):
  File "/root/telegram-rss-reader/UWN/lib64/python3.9/site-packages/telegram/ext/dispatcher.py", line 555, in process_update
    handler.handle_update(update, self, check, context)
  File "/root/telegram-rss-reader/UWN/lib64/python3.9/site-packages/telegram/ext/handler.py", line 198, in handle_update
    return self.callback(update, context)
  File "/root/telegram-rss-reader/bot.py", line 68, in text
    text_received = update.message.text
AttributeError: 'NoneType' object has no attribute 'text'

My .env file

TELEGRAM_BOT_TOKEN=22222222:xxxxxxxxxxxxxxxxxxxxxxxx
FEED_DATABASE=reader.db
FEED_UPDATE_INTERVAL=60
ARCHIVE_POSTS=false
EXCLUDE_WORDS=""

This is strange how we have mirrored systems and different results.

santhoshtr commented 2 years ago

I was able to reproduce the issue when edited the message after sending the input or used up arrow to retrieve previous message and send again. Note that the error happens at line 68, which is a handler for messages that are not identified by the bot.

Can you try sending the messages are fresh text instead of prev messages or edited text?

Meanwhile I will try to handle this special case.

J-Wick4 commented 2 years ago

Yes, I always was sending new messages.

Looking online this seems to be a problem with the python-telegram-bot. Transition Guide Python-Telegram-Bot

santhoshtr commented 2 years ago

I still get errors when I try and run any of the commands from the Telegram Channel such as help, list, add, etc.

Also, the commands should be send as /list, /hello, /add etc - Note the slash at the beginning

J-Wick4 commented 2 years ago

Yes, I use the slash in front of the commands with bots. This seems to be a widespread error many encounters with this module.

https://github.com/python-telegram-bot/python-telegram-bot/issues/2931

J-Wick4 commented 2 years ago

I remade the bot, new token, and everything is working properly now.

HOWEVER, this only goes into the private bot chat. Is there a way it can be modified to post to a channel when it's made admin?

I believe the message post just needs the channel ID ended to the end of the post.