我尝试让机器人每 1 分钟自动发送一次消息,如果我作为所有者向机器人写入消息,它会将消息发送到订阅该机器人的所有频道。
def send_to_channel(message):
bot = Bot(token=BOT_TOKEN)
bot.send_message(chat_id=CHANNEL_ID, text=message)
def send_random_ayah(context: CallbackContext):
response = requests.get(QURAN_API_URL)
if response.status_code == 200:
data = response.json()
ayah = data.get('verse_key', '')
send_to_channel(f"Random Ayah: {ayah}")
else:
print(f"Failed to fetch Ayah. Status Code: {response.status_code}")
def main():
updater = Updater(BOT_TOKEN, update_queue=None)
dp = updater.dispatcher
# Command handler to manually send a message to the channel
dp.add_handler(CommandHandler("send", send_to_channel))
# Job to send a random Ayah every 1 minute
updater.job_queue.run_repeating(send_random_ayah, interval=2, first=0)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
我收到了这个错误
AttributeError Traceback (most recent call last)
<ipython-input-107-174f78df48e2> in <cell line: 1>()
1 if __name__ == '__main__':
----> 2 main()
<ipython-input-106-8197ed041923> in main()
2 def main():
3 updater = Updater(BOT_TOKEN, update_queue=None)
----> 4 dp = updater.dispatcher
5
6 # Command handler to manually send a message to the channel
AttributeError: 'Updater' object has no attribute 'dispatcher'`
我更新了库并重新安装了它,但它没有解决问题。 如果您对此有任何其他提示,请告诉我
'dispatcher'
在版本20
中不再使用。
如果你想使用你的代码,那么你可以将 python-telegram-bot 降级为:
pip install python-telegram-bot==13.3
如果您正在使用
v20
那么您必须以不同的方式构建您的应用程序。