【发布时间】:2018-08-19 06:31:10
【问题描述】:
我正在尝试构建一个机器人,它会在使用 python 的最新消息有更新时自动发送消息。以下是我所做的。
companies = {
"name_1": {
"rss": "name_1 rss link",
"link": "name_1 link"
}
}
import feedparser as fp
import time, telebot
token = <TOKEN>
bot = telebot.TeleBot(token)
LIMIT = 1
while True:
def get_news():
count = 1
news = []
for company, value in companies.items():
count = 1
if 'rss' in value:
d = fp.parse(value['rss'])
for entry in d.entries:
if hasattr(entry, 'published'):
if count > LIMIT:
break
news.append(entry.link)
count = count + 1
return (news)
val = get_news()
time.sleep(10)
val2 = get_news()
try:
if val[0]!=val2[0]:
bot.send_message(chat_id= "Hardcoded chat_id", text=val2[0])
except Exception:
pass
如何更新我的代码,以便机器人将最新消息发布到它添加到的所有组?
我得到了 chat_id 使用:
bot.get_updates()[-1].message.chat.id
关于如何实现自动化的任何建议?
【问题讨论】:
-
如果你没有缩进那么深(7 级!...使用函数来分块),你的代码可以提高可读性。Linus 说:news.ycombinator.com/item?id=3414637
标签: python-3.x telegram telegram-bot python-telegram-bot