Python Telegram Bot Queue 在 Heroku 上没有 运行
Python Telegram Bot Queue not Running on 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 上的这段代码没有达到我的预期
2021-09-28T20:11:36.231963+00:00 app[web.1]:时间应该 post forex 15:14:10
2021-09-28T20:11:36.457226+00:00 app[web.1]: 运行宁外汇
但在此之后,它不会 运行 任何其他功能,并且它总是 post 在 /start 命令完成后立即进行外汇聊天。真的,我正在寻找一种方法让 /start 命令正确地进入 运行 并在每次重新启动时让该命令进入 运行 这样我就不必继续返回并手动执行它
关于您的第一个问题:您使用 run_daily
错误。如 wiki article and in the documentation, the first argument needs to be a function. Instead you pass purchase_forex(update, context)
, which is the return value of purchase_forex
, which is None
. Also the function that you pass must accept exactly one argument, not two. Please make sure to read the wiki article, the documentation and probably also this example 中所述,以更好地理解 JobQueue
在 PTB 中的工作原理。
关于无需调用 /start
命令即可安排作业:作业队列可用作 u.job_queue
和 u.dispatcher.job_queue
(两者都是与 [=18 相同的对象) =]。这样您就可以在 main
.
中进行正确的安排
免责声明:我目前是 python-telegram-bot
.
的维护者
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 上的这段代码没有达到我的预期
2021-09-28T20:11:36.231963+00:00 app[web.1]:时间应该 post forex 15:14:10 2021-09-28T20:11:36.457226+00:00 app[web.1]: 运行宁外汇
但在此之后,它不会 运行 任何其他功能,并且它总是 post 在 /start 命令完成后立即进行外汇聊天。真的,我正在寻找一种方法让 /start 命令正确地进入 运行 并在每次重新启动时让该命令进入 运行 这样我就不必继续返回并手动执行它
关于您的第一个问题:您使用 run_daily
错误。如 wiki article and in the documentation, the first argument needs to be a function. Instead you pass purchase_forex(update, context)
, which is the return value of purchase_forex
, which is None
. Also the function that you pass must accept exactly one argument, not two. Please make sure to read the wiki article, the documentation and probably also this example 中所述,以更好地理解 JobQueue
在 PTB 中的工作原理。
关于无需调用 /start
命令即可安排作业:作业队列可用作 u.job_queue
和 u.dispatcher.job_queue
(两者都是与 [=18 相同的对象) =]。这样您就可以在 main
.
免责声明:我目前是 python-telegram-bot
.