英文:
How to send message from bot to user at a fixed time or at intervals through python telegram bot library?
问题
我正在尝试在从第二次对话开始时每天发送消息给机器人,而不需要从用户端触发(例如,commandhandler)。
我已经为机器人建立了一个基本的菜单来与用户交互。
但我也在尝试通过job_queue每天发送消息。
我参考了使用commandhandlers的代码:
dp.add_handler(CommandHandler("set", set_timer,
pass_args=True,
pass_job_queue=True,
pass_chat_data=True))
这是在用户输入/set命令后设置的。但我正在尝试找到一种自动发送消息的方式,例如每30秒或每天定时发送消息。
我的代码如下:
from telegram.ext import Updater, CommandHandler
from telegram.ext import MessageHandler, Filters, InlineQueryHandler
import logging
import telegram
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger()
def start(bot, update):
update.message.reply_text("Hello, Thanks for choosing us!!")
def callback_minute(context: telegram.ext.CallbackContext):
chat_id = ?
context.bot.send_message(chat_id=chat_id,
text='Hi User, Add Fund to your account to start trading')
def main():
updater = Updater(token, use_context=True)
dp = updater.dispatcher
j = updater.job_queue
dp.add_handler(CommandHandler("start", start))
job_minute = j.run_repeating(callback_minute, interval=10, first=0)
updater.start_polling()
updater.idle()
如何获取chat_id?
如果我使用以下代码:
def callback_minute(update, context: telegram.ext.CallbackContext):
chat_id = update.message.chat.id
我会得到以下错误:
TypeError: callback_minute() missing 1 required positional argument: 'context'
英文:
I am trying to send messages to bot daily without trigger from user side (eg commandhadler) from second conversation onwards.
I have build a basic menu for bot to interact with user
But i am also trying to send messages daily through job_queue
I have refered codes which are using commandhandlers
dp.add_handler(CommandHandler("set", set_timer,
pass_args=True,
pass_job_queue=True,
pass_chat_data=True))
This is being set after user types /set .
But I am trying to find a way to automatically send messages every 30 seconds or set a fixed time for message to be sent daily
My code
from telegram.ext import Updater,CommandHandler
from telegram.ext import MessageHandler,Filters,InlineQueryHandler
import logging
import telegram
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger()
def start(bot, update):
update.message.reply_text("Hello , Thanks for choosing us!!")
def callback_minute(context: telegram.ext.CallbackContext):
chat_id = ?
context.bot.send_message(chat_id=chat_id,
text='Hi User, Add Fund to your account to start trading')
def main():
updater = Updater(token,use_context=True)
dp = updater.dispatcher
j = updater.job_queue
dp.add_handler(CommandHandler("start",start))
job_minute = j.run_repeating(callback_minute, interval=10, first=0)
updater.start_polling()
updater.idle()
How to get chat_id?
If i am using
def callback_minute(update, context: telegram.ext.CallbackContext):
chat_id = update.message.chat.id
I am getting this error
TypeError: callback_minute() missing 1 required positional argument: 'context'
答案1
得分: 3
以下是您要翻译的内容:
# 在下面进行了重新处理,以便从“/start”命令调用run_repeating()(如评论中的Gagan T K建议的)。在这个示例中,`first=30`,所以它将在30秒后开始。
# 在GitHub的[JobQueue的维基文档][1]底部有一个使用作业队列的很好的示例。
from telegram.ext import Updater, CommandHandler
from telegram.ext import MessageHandler, Filters, InlineQueryHandler
import logging
import telegram
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger()
bot = telegram.Bot(token=token)
def start(update, context):
context.bot.send_message(chat_id=update.message.chat_id,
text="Hello , Thanks for choosing us!!")
context.job_queue.run_repeating(callback_minute, interval=10, first=30,
context=update.message.chat_id)
def callback_minute(context):
chat_id = context.job.context
context.bot.send_message(chat_id=chat_id,
text="Hi User, Add Fund to your account to start trading")
def main():
updater = Updater(token, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start, pass_job_queue=True))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
[1]: https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions-%E2%80%93-JobQueue
英文:
It is reworked below so that run_repeating() is called from the /start
command (as suggested by Gagan T K in the comments). In this example first=30
so it will start after 30 seconds.
There is a good example of using the job queue in this way at the bottom of the wiki documentation for JobQueue on GitHub.
from telegram.ext import Updater,CommandHandler
from telegram.ext import MessageHandler,Filters,InlineQueryHandler
import logging
import telegram
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger()
bot = telegram.Bot(token=token)
def start(update, context):
context.bot.send_message(chat_id=update.message.chat_id,
text="Hello , Thanks for choosing us!!")
context.job_queue.run_repeating(callback_minute, interval=10, first=30,
context=update.message.chat_id)
def callback_minute(context):
chat_id=context.job.context
context.bot.send_message(chat_id=chat_id,
text="Hi User, Add Fund to your account to start trading")
def main():
updater = Updater(token,use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start",start, pass_job_queue=True))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
答案2
得分: 1
你需要使用context.job_queue.run_repeating()
来在特定时间间隔后连续重复执行作业。
如果你希望作业每天执行一次,你可以使用context.job_queue.run_daily()
并指定时间。
这些是python-telegram-bot
文档中两种情况的链接:
job_queue.run_repeating(),job_queue.run_daily()
这些文档包含非常有用的信息,将帮助你的查询。
英文:
You have to use context.job_queue.run_repeating()
to repeat the job continuously after specific time interval.
If you want the job to execute once everyday, you can use context.job_queue.run_daily()
and specify the time.
These are python-telegram-bot
docs links for both of the cases:
job_queue.run_repeating(), job_queue.run_daily()
These docs have very good information which will help your query.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论