gpt4 book ai didi

Method to send messages to a channel using telegram bot API, python 3 and JobQueue(使用电报机器人API、Python3和JobQueue向通道发送消息的方法)

转载 作者:bug小助手 更新时间:2023-10-25 19:13:29 33 4
gpt4 key购买 nike



Basically I'm trying to understand how this API works, by doing the following .I'm not able to get the message to my channel (I'm sending the /test command from my telegram account). Also, how I'm supposed to use JobQueue and send auto-msgs every 3 hours for example? The documentation explanation doesn't works for me.

基本上,我试图通过执行以下操作来理解此API的工作原理。我无法将消息发送到我的通道(我正在从我的电报帐户发送/test命令)。另外,例如,我应该如何使用JobQueue和每3小时发送一次自动消息?文档解释对我不起作用。


def test(bot, update):
update.send_message(chat_id='@channelid', text='this is a test')

def main():
# Create the EventHandler and pass it your bot's token.
updater = Updater("457160310:AAFlxrH2uAaOMGrgO0suOXFM2gVKywsUL0E")
dp = updater.dispatcher

dp.add_handler(CommandHandler("test", test))

dp.add_error_handler(error)

updater.start_polling()

updater.idle()


if __name__ == '__main__':
main()

更多回答

Note that you added the token. If it is still valid, consider to change it via @botfather

请注意,您添加了令牌。如果该名称仍然有效,请考虑通过@bot父号进行更改

优秀答案推荐

For the job implementation to send messages on intervals you can read this page that is full of examples. Someone should be the very same thing you want to do.

为了让作业实现按时间间隔发送消息,您可以阅读此页面,其中充满了示例。有些人应该和你想做的事情完全一样。



About the command thing, I think that the library doesn’t get commands from channels so you may consider to:

关于命令,我认为该库不从通道获取命令,因此您可以考虑:




  • send the command in private chat to the bot and the bot sends the reply to the channel (better thing in my opinion)

    在私人聊天中向机器人发送命令,机器人将回复发送到频道(在我看来,这是更好的做法)


  • handle messages from channels (not commands) and checking if the text of the message contains the command

    处理来自通道的消息(不是命令),并检查消息文本是否包含命令




I would suggest the first solution so you can even keep clean the channel

我建议第一个解决方案,这样你甚至可以保持渠道畅通



You can send messages directly to a channel or a conversation using the Telegram REST API by knowing its chat_id.

According to the Telegram documentation, methods such as webhooks require users to send a message to the bot first before the bot can reply. These methods essentially aim to obtain a chat_id, which is different for different recipients.

通过知道其chat_id,您可以使用Telegram rest API将消息直接发送到通道或会话。根据Telegram的文档,网络挂钩等方法要求用户首先向机器人发送消息,然后机器人才能回复。这些方法本质上是为了获取chat_id,对于不同的接收者来说,这是不同的。


However, if you want to send messages to a specific channel without going through that process, you only need to acquire the fixed chat_id of the channel and the bot's token. Then, you can use curl to send the message:

然而,如果您想不经过该过程就将消息发送到特定的通道,您只需要获取该通道的固定chat_id和机器人的令牌。然后,您可以使用cURL发送消息:


// get
curl -i -X GET \
'https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text=hello'

// post
curl -i -X POST \
-H "Content-Type:application/json" \
-d \
'{
"chat_id": "{1}",
"text": "here is bot"
}' \
'https://api.telegram.org/bot{0}/sendMessage'

Please replace {0} with the bot's token and {1} with the fixed chat_id of the channel.

请将{0}替换为机器人的令牌,将{1}替换为频道的固定chat_id。


How to obtain the chat_id of a channel?


You can send the channel's link to the @username_to_id_bot, and it will provide you with the chat_id of the channel.
Link: https://t.me/username_to_id_bot

您可以将频道的链接发送到@username_to_id_bot,它将为您提供频道的chat_id。链接:https://t.me/username_to_id_bot


更多回答

Yeah, I aware of this page, the problem is that the exemples simply don't work. As for your first consideration, that's the thing I'm trying to do with this code I posted: send the command in private from my personal account, so the bot can, instead of respond to me, respond to the channel.

是的,我知道这个页面,问题是这些例子根本不起作用。至于你的第一个考虑,这就是我试图用我发布的代码做的事情:从我的个人账户私下发送命令,这样机器人就可以响应频道,而不是响应我。

@user3927607 for the first thing I don’t see jobs in your code so I can’t say what’s wrong. For the last thing in test function use bot.send_message instead of update. It should work

@user3927607首先,我没有在您的代码中看到作业,所以我不能说出哪里出了问题。对于测试函数中的最后一件事,使用bot.endMessage而不是UPDATE。它应该能行得通

using "bot.send_message" fix'd this problem! Many thanks. Now about the job: there's no job in the code because I don't know where to implement it: should I put the job in the main function and make this job call another function (test for exemple)?

使用“bot.end_Message”解决了这个问题!非常感谢。现在关于作业:代码中没有作业,因为我不知道在哪里实现它:我是否应该将作业放在主函数中,并使该作业调用另一个函数(例如测试)?

That's it, I fixed the problem by adding a job in the main function that calls the test function :) thanks for the help!!

就是这样,我通过在调用测试函数的main函数中添加一个作业修复了这个问题:)谢谢您的帮助!!

@user3927607 yes it has to be in the main if it should be run when the bot is turned on.

@user3927607是的,如果它应该在机器人打开时运行,它必须在主系统中。

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com