0

Im trying to get my Telegram bot to reply news via rss feed. For this I'm using the module Feedparser. Now I have managed to get it to work however the bot sends 2 separate messages per feed item. The first one has the summary of the feed and the second one has the link. I want to change the code so that it sends it in 1 message. I have tried 2 different methods and both came up with errors.

Working method with 2 msg per item:

elif text == "/news":
            for i in range(3):
                reply (feed.entries[i].summary)
                reply (feed.entries[i].link)

My failed fixes:

Fix1

elif text == "/news":
            for i in range(3):
                reply ((feed.entries[i].summary)
                       (feed.entries[i].link))

error1

Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/e~thalia-bot/1.391503855076259816/main.py", line 169, in post
(feed.entries[i].link))
TypeError: 'unicode' object is not callable

Fix2

elif text == "/news":
            for i in range(3):
                reply (feed.entries[i].summary.link)

Error2

Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/e~thalia-bot/1.391503917377816930/main.py", line 168, in post
reply (feed.entries[i].summary.link)
AttributeError: 'unicode' object has no attribute 'link'

I'm not sure what i have done wrong here, looked up the errors to try and get a fix but haven't found a working solution yet. Would appreciate if someone could point me in the right direction.

Kevin
  • 37
  • 10

1 Answers1

1

You can try

elif text == "/news":
  for i in range(3):
    reply("{} {}".format(feed.entries[i].summary, feed.entries[i].link))

You can replace the whitespace in "{} {}" with "\n" (newline character) if you want to have the link on the next line.

henadzit
  • 332
  • 2
  • 8