[英]Why can't my bot reply to telegram messages anymore?
I have 2 group chats, GC A and B. The bot is an admin to both and just forwards new messages to another group then leave a reply like "Forwarded Thanks."我有 2 个群聊,GC A 和 B。该机器人是两者的管理员,只是将新消息转发到另一个组,然后留下诸如“转发谢谢”之类的回复。 in the original group for notification.在原始组中进行通知。
In GC A the bot is able to reply to new messages just fine HOWEVER the bot is unable to reply to messages in GC A.在 GC A 中,机器人能够很好地回复新消息,但是机器人无法回复 GC A 中的消息。
I can't help but think this is a bug since the bot works fine the first time I ever ran it but after editing the code to add new features, it doesn't reply anymore.我不禁认为这是一个错误,因为机器人在我第一次运行它时运行良好,但在编辑代码以添加新功能后,它不再回复。
What's happening here and how do I solve it?这里发生了什么,我该如何解决?
As a bonus: The bot is able to notice when a message was edited and replies to it.作为奖励:机器人能够注意到消息何时被编辑并回复。 That part still works in both groups but when I just send a new message it doesn't work.这部分在两个组中仍然有效,但是当我只是发送一条新消息时它不起作用。
I replaced the bot entirely as in I deleted it and made a new.我完全替换了机器人,就像我删除它并制作了一个新的一样。 Still didn't work仍然没有工作
I had the same issue before and just created a new group chat which worked but I cannot do that now.我之前遇到过同样的问题,刚刚创建了一个新的群聊,但我现在不能这样做。
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 15 16:28:19 2021
@author: erdze
"""
from datetime import datetime
import telegram
from telegram import Update
import logging
from telegram.ext import Updater, CallbackContext, CommandHandler, MessageHandler, Filters
from CONSTANTS import "some stuff"
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
bot = telegram.Bot(TOKEN_TEST)
updater = Updater(TOKEN_TEST, use_context=True)
dispatcher = updater.dispatcher
job = updater.job_queue
def status(update: Update, context: CallbackContext):
context.bot.send_message(chat_id=update.effective_chat.id, text="I am alive. Thanks!")
def test(update: Update, context: CallbackContext):
context.bot.send_message(chat_id=USER_TEST, text="I'm a bot not a person")
#forward to group "RECEIVER"
def forward_edits_to_group(update: Update, context: CallbackContext):
check = 0
#this section breaks up the sentence into individual words and makes the lowercase
sent_message = update.edited_message.text
sent_message = sent_message.lower()
sent_message = sent_message.split()
for keyword in keyword_list:
if keyword in sent_message:# checks if message contains keyword
check += 1
if check == 3:
bot.forward_message(chat_id = TEST_RECEIVER,
from_chat_id=update.edited_message.chat_id,
message_id=update.edited_message.message_id)
context.bot.send_message(chat_id=TEST_RECEIVER, text="Forwarded thanks :D")
check = 0
#forward to group "RECEIVER"
def forward_to_group(update: Update, context: CallbackContext):
check = 0
#this section breaks up the sentence into individual words and makes the lowercase
sent_message = update.message.text
sent_message = sent_message.lower()
sent_message = sent_message.split()
for keyword in keyword_list:
if keyword in sent_message:# checks if message contains keyword
check += 1
if check == 3:
bot.forward_message(chat_id = TEST_RECEIVER,
from_chat_id=update.message.chat_id,
message_id=update.message.message_id)
context.bot.send_message(chat_id=TEST_RECEIVER, text="Forwarded thanks :D")
check = 0
#forward to group "karama dj"
def forward_edits_to_dj(update: Update, context: CallbackContext):
check = 0
#this section breaks up the sentence into individual words and makes the lowercase
sent_message = update.edited_message.text
sent_message = sent_message.lower()
sent_message = sent_message.split()
for keyword in keyword_list:
if keyword in sent_message:# checks if message contains keyword
check += 1
if check == 3:
bot.forward_message(chat_id = "a chat id",
from_chat_id=update.edited_message.chat_id,
message_id=update.edited_message.message_id)
context.bot.send_message(chat_id="a chat id", text="Forwarded thanks :D")
check = 0
#forward to group "karama dj"
def forward_to_dj(update: Update, context: CallbackContext):
check = 0
#this section breaks up the sentence into individual words and makes the lowercase
sent_message = update.message.text
sent_message = sent_message.lower()
sent_message = sent_message.split()
for keyword in keyword_list:
if keyword in sent_message:# checks if message contains keyword
check += 1
if check == 3:
bot.forward_message(chat_id = "a chat id",
from_chat_id=update.message.chat_id,
message_id=update.message.message_id)
context.bot.send_message(chat_id="a chat id", text="Forwarded thanks :D")
check = 0
#forward to group "g4 strong"
def forward_edits_to_g4(update: Update, context: CallbackContext):
check = 0
#this section breaks up the sentence into individual words and makes the lowercase
sent_message = update.edited_message.text
sent_message = sent_message.lower()
sent_message = sent_message.split()
for keyword in keyword_list:
if keyword in sent_message:# checks if message contains keyword
check += 1
if check == 3:
bot.forward_message(chat_id = "a chat id",
from_chat_id=update.edited_message.chat_id,
message_id=update.edited_message.message_id)
bot.forward_message(chat_id = "a chat id",
from_chat_id=update.edited_message.chat_id,
message_id=update.edited_message.message_id)
context.bot.send_message(chat_id=G4, text="Forwarded thanks :D")
check = 0
#forward to group "g4 strong"
def forward_to_g4(update: Update, context: CallbackContext):
check = 0
#this section breaks up the sentence into individual words and makes the lowercase
sent_message = update.message.text
sent_message = sent_message.lower()
sent_message = sent_message.split()
for keyword in keyword_list:
if keyword in sent_message:# checks if message contains keyword
check += 1
if check == 3:
bot.forward_message(chat_id = "a chat id",
from_chat_id=update.message.chat_id,
message_id=update.message.message_id)
bot.forward_message(chat_id = "a chat id",
from_chat_id=update.message.chat_id,
message_id=update.message.message_id)
context.bot.send_message(chat_id=G4, text="Forwarded thanks :D")
check = 0
#checks what the time is every second
def callback_second(context: telegram.ext.CallbackContext):
current_time = datetime.now()
time_now = current_time.strftime("%H,%M,%S")
day = datetime.today().weekday()
gawain_remind_times = [6,0,2,5]
#sends gawain link at 9:50pm everyday
if time_now == "21,50,00":
context.bot.send_message(chat_id=G4, text=text)
context.bot.send_message(chat_id=G4, text=text)
#reminder about gawain at 1pm everyday
if time_now == "13,00,00" and day in gawain_remind_times:
context.bot.send_message(chat_id=G4, text=text)
context.bot.send_message(chat_id=G4, text=text)
#ask who attended gawain at 9:30pm
if time_now == "21,30,00" and day in gawain_remind_times:
context.bot.send_message(chat_id=G4, text=text)
context.bot.send_message(chat_id=G4, text=text)
#ask who attended gawain at 8:30pm
if time_now == "20,30,00" and day in gawain_remind_times:
context.bot.send_message(chat_id=G4, text=text)
context.bot.send_message(chat_id=G4, text=text)
#ask who attended gawain at 7:30pm
if time_now == "19,30,00" and day in gawain_remind_times:
context.bot.send_message(chat_id=G4, text=text)
context.bot.send_message(chat_id=G4, text=text)
def main():
#checks time every second
job_minute = job.run_repeating(callback_second, interval=1)
#status command
start_handler = CommandHandler('status', status)
dispatcher.add_handler(start_handler)
#tester command
test_handler = CommandHandler('test', test)
dispatcher.add_handler(test_handler)
#forwards edited messages from group TESTER
test_forward_edits_handler = MessageHandler(Filters.chat(TEST_SENDER) & Filters.update.edited_message, forward_edits_to_group)
dispatcher.add_handler(test_forward_edits_handler)
#forwards messages from group TESTER
test_forward_handler = MessageHandler(Filters.chat(TEST_SENDER), forward_to_group)
dispatcher.add_handler(test_forward_handler)
#forwards edited messages from group
dj_forward_edits_handler = MessageHandler(Filters.chat(KARAMA_DJ) & Filters.update.edited_message, forward_edits_to_dj)
dispatcher.add_handler(dj_forward_edits_handler)
#forwards messages from group
dj_forward_handler = MessageHandler(Filters.chat(KARAMA_DJ), forward_to_dj)
dispatcher.add_handler(dj_forward_handler)
#forwards edited messages from group
g4_forward_edits_handler = MessageHandler(Filters.chat(G4) & Filters.update.edited_message, forward_edits_to_g4)
dispatcher.add_handler(g4_forward_edits_handler)
#forwards messages from group
g4_forward_handler = MessageHandler(Filters.chat(G4), forward_to_g4)
dispatcher.add_handler(g4_forward_handler)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
After leaving it for a while WITHOUT changing any of the code, it works now.在没有更改任何代码的情况下离开它一段时间后,它现在可以工作了。 I have no idea why this happened but I will leave this here for future reference.我不知道为什么会发生这种情况,但我将把它留在这里以供将来参考。
Note: I don't know if the problem is;注:不知道是不是问题; 1. a bug in ptb, 2. Telegram lag or error, and 3. my own incompetence. 1. ptb 中的错误,2. 电报延迟或错误,以及 3. 我自己的无能。 But I'm very new so its probably my incompetence但我很新,所以这可能是我的无能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.