gpt4 book ai didi

python-3.x - 无需用户输入即可向 Telegram 组发送消息

转载 作者:行者123 更新时间:2023-12-03 11:25:39 26 4
gpt4 key购买 nike

我正在尝试构建一个机器人,它会在使用 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有关如何自动执行此操作的任何建议?

最佳答案

使用python-telegram-bot api,您可以发送这样的消息

bot.send_message(id, text='Message')

你需要“bot”和“id”

我将这些保存在名为“mybots”的字典中,当人们第一次与机器人交互/或以后与机器人进行通信时,我会填写/更新该词典。可以腌制这本字典以保持其持久性。
mybots = {}

def start(bot, update):
"""Send a message when the command /start is issued."""
mybots[update.message.chat_id] = bot
update.message.reply_text('Hello{}!'.format(
update.effective_chat.first_name))

def send_later():
for id, bot in mybots.items():
bot.send_message(id, text='Beep!')

关于python-3.x - 无需用户输入即可向 Telegram 组发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49214875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com