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

wa7juj8i  于 5个月前  发布在  Python
关注(0)|答案(2)|浏览(69)

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

from instabot import Bot 
from datetime import datetime,timedelta 

HOURS = 18 
def send_message():
    bot = Bot()  
    today = datetime.now()
    hours_added = timedelta(hours = HOURS)
    future_date_and_time = today + hours_added

    melbourne_time = future_date_and_time.strftime("%H:%M %p")

    if(melbourne_time== "09:00 AM"):
        bot.login(username="myusername", password="pass")
        bot.send_message("My message",["Personame"])

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

z31licg0

z31licg01#

使用Python time模块,如:

import time

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

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

wj8zmpe1

wj8zmpe12#

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

pip install schedule

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

from instabot import Bot 
from datetime import datetime, timedelta
import schedule
import time

HOURS = 18 

def send_message():
    bot = Bot()  
    bot.login(username="myusername", password="pass")
    bot.send_message("My message", ["Personame"])

def schedule_job():
    # Schedule the job to run every day at the specified time
    schedule.every().day.at("Your Time").do(send_message)

    # Run the scheduler continuously
    while True:
        schedule.run_pending()
        time.sleep(1)

if __name__ == "__main__":
    schedule_job()


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

相关问题