0

我试图让我的 Telegram 机器人通过 rss 提要回复新闻。为此,我使用模块 Feedparser。现在我已经设法让它工作了,但是机器人会为每个提要项目发送 2 条单独的消息。第一个包含提要的摘要,第二个包含链接。我想更改代码,使其在 1 条消息中发送。我尝试了 2 种不同的方法,但都出现了错误。

每项 2 msg 的工作方法:

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

我失败的修复:

修复1

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

错误1

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

修复2

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

错误2

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'

我不确定我在这里做错了什么,查找错误以尝试修复,但尚未找到有效的解决方案。如果有人能指出我正确的方向,将不胜感激。

4

1 回答 1

1

你可以试试

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

如果您希望链接位于下一行,可以将“{} {}”中的空格替换为“\n”(换行符)。

于 2016-03-20T09:48:37.420 回答