Python Telegram Bot队列在Heroku上不运行


from telegram.ext import Updater, CommandHandler
from telegram import bot, ParseMode
import os
import datetime
import pytz
import logging
from dotenv import load_dotenv
from flask import Flask
load_dotenv()
TOKEN = os.environ.get('API_KEY')
CHAT_ID = os.environ.get('CHAT_ID')
PORT = int(os.environ.get('PORT', '8443'))
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)

def daily_job(update, context):
""" Running on Mon, Tue, Wed, Thu, Fri = tuple(range(5)) """
sport = datetime.time(15, 12, 10, 000000, tzinfo=pytz.timezone('America/Chicago'))
trading = datetime.time(15, 13, 10, 000000, tzinfo=pytz.timezone('America/Chicago'))
forex = datetime.time(15, 14, 10, 000000, tzinfo=pytz.timezone('America/Chicago'))
print("Time its supposed to post sport", sport)
print("Time its supposed to post trading", trading)
print("Time its supposed to post forex", forex)
context.bot.send_message(chat_id=CHAT_ID, text='Activating daily notification!')
context.job_queue.run_daily(purchase_forex(update, context), forex, days=tuple(range(7)), context=update)
context.job_queue.run_daily(purchase_sports(update, context), sport, days=tuple(range(7)), context=update)
context.job_queue.run_daily(purchase_trading(update, context), trading, days=tuple(range(7)), context=update)
def purchase_forex(update, context):
print('running forex')
context.bot.send_message(chat_id=CHAT_ID, text="Daily Forex", parse_mode=ParseMode.HTML)
def purchase_sports(update, context):
print('running sports')
context.bot.send_message(chat_id=CHAT_ID, text="Daily Text", parse_mode=ParseMode.HTML)
def purchase_trading(update, context):
print('running trading')
context.bot.send_message(chat_id=CHAT_ID, text="Daily Trading Text", parse_mode=ParseMode.HTML)
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)


def main():
u = Updater(TOKEN, use_context=True)
u.dispatcher.add_handler(CommandHandler('start', daily_job, pass_job_queue=True))
u.dispatcher.add_error_handler(error)
# Start the Bot
u.start_webhook(listen="0.0.0.0",
port=PORT,
url_path=TOKEN,
webhook_url="https://<appname>.herokuapp.com/" + TOKEN)

# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
u.idle()
if __name__ == '__main__':
main()

在heroku上运行这段代码并没有得到我想要的结果

0 2021 - 09 - 28 - t20:11:36.231963 +应用程序web。1]:时间应该是外汇15:14:102021 - 09 - 28 - t20:11:36.457226 + 00:00应用(网络。1]: running forex

但是在此之后,它不运行任何其他函数,并且它总是在/start命令完成后立即发布外汇聊天。真的,我正在寻找一种方法,使/start命令正常运行,并使该命令在每次重启时运行,这样我就不必一直返回并手动执行

关于你的第一个问题:你使用run_daily错误。正如wiki文章和文档中所解释的,第一个参数需要是一个函数。取而代之的是传递purchase_forex(update, context),它是purchase_forex的返回值,None。另外,传递的函数必须只接受一个参数,而不是两个。请务必阅读维基文章、文档以及这个示例,以更好地理解JobQueue在PTB中的工作原理。

关于调度作业而不需要调用/start命令:作业队列作为u.job_queueu.dispatcher.job_queue可用(两者都是与context.job_queue相同的对象)。这样你就可以在你的main中正确地进行调度。


免责声明:我目前是python-telegram-bot的维护者。

相关内容

  • 没有找到相关文章

最新更新