如何使用Python(Instabot)每天在特定时间发送Instagram DM?

wa7juj8i  于 2024-01-05  发布在  Python
关注(0)|答案(2)|浏览(143)

我正在使用Instagram库在Instagram上发送消息,但是我想制作一个Python程序,每天在特定时间在Instagram上发送直接消息。下面的代码演示了如何发送消息,但是如果我每天在特定时间发送它,我会怎么做?

  1. from instabot import Bot
  2. from datetime import datetime,timedelta
  3. HOURS = 18
  4. def send_message():
  5. bot = Bot()
  6. today = datetime.now()
  7. hours_added = timedelta(hours = HOURS)
  8. future_date_and_time = today + hours_added
  9. melbourne_time = future_date_and_time.strftime("%H:%M %p")
  10. if(melbourne_time== "09:00 AM"):
  11. bot.login(username="myusername", password="pass")
  12. bot.send_message("My message",["Personame"])

字符串
任何帮助是赞赏!!

z31licg0

z31licg01#

使用Python time模块,如:

  1. import time
  2. if("1:30" in time.ctime()):
  3. """Enter your message"""

字符串
您还可以使用while循环来检查时间

wj8zmpe1

wj8zmpe12#

有一个名为Schedule的库,可以帮助您实现这一点:

  1. pip install schedule

字符串
下面是如何修改代码。

  1. from instabot import Bot
  2. from datetime import datetime, timedelta
  3. import schedule
  4. import time
  5. HOURS = 18
  6. def send_message():
  7. bot = Bot()
  8. bot.login(username="myusername", password="pass")
  9. bot.send_message("My message", ["Personame"])
  10. def schedule_job():
  11. # Schedule the job to run every day at the specified time
  12. schedule.every().day.at("Your Time").do(send_message)
  13. # Run the scheduler continuously
  14. while True:
  15. schedule.run_pending()
  16. time.sleep(1)
  17. if __name__ == "__main__":
  18. schedule_job()


用实际的时间替换(“你的时间”)。用户名和真实的用户名同样适用于密码。如果你有任何疑问,请随时询问。

展开查看全部

相关问题