机器人无法编辑消息(Telebot/多进程)
我确定我用的是同一个ID,但当send_message_process尝试用从队列中获取的消息ID去编辑消息时,我遇到了以下错误:
编辑消息时出错:向Telegram API发送请求失败。错误代码:400。描述:错误请求:找不到聊天... 正在重试
import telebot
import threading
import requests
import re
import multiprocessing
from queue import Queue
from telebot import types
from defs_helper import *
from datetime import datetime
import time as tm
gpid = 'xxxxx'
Token = "xxxxx"
bot = telebot.TeleBot(Token)
def startloop(queue):
global gpid
while True:
print('Starting search for new entry')
try:
bot.send_message('xxxxxx', 'Looking for new entry...')
# Restante do seu código...
except Exception as e:
print(f'Erro ao enviar mensagem Telegram: {e}')
tm.sleep(30)`
verify_m_number = get_m_number()
print(verify_m_number)
if verify_m_number == 1:
mlist = updatematchlist()
while len(mlist) == 0:
mlist = updatematchlist()
mlist = updatematchlist()
print(mlist)
pattern = r'\((.*?)\)'
matches = re.findall(pattern, mlist[0][2])
pl1 , pl2 = matches
patterntm = r'\b([A-Z][a-zA-Z\s\d]+) \((.*?)\)'
matchestm = re.findall(patterntm, mlist[0][2])
tm1 = matchestm[0][0]
tm2 = matchestm[1][0]
hEm = mlist[0][1].split('\n')[1].split(':')
print(hEm)
mint = str(getnt().minute).zfill(2)
hr = str(getnt().hour).zfill(2)
print("hEm[0]:", str(hEm[0]).zfill(2))
print("hEm[1]:", hEm[1])
print("hr:", hr)
print("mint:", mint)
med = overunderstat(pl1, pl2)
if str(hEm[0]).zfill(2) != hr: print('different hr, looking for more')
if str(hEm[0]).zfill(2) == hr and (int(mint) < int(hEm[1]) or (mint == '00' and hEm[1] != '00')):
print('got it')
event_minute = int(hEm[1])
print(event_minute)
event_second = 0
current_minute = int(mint)
current_second = int(getnt().second)
print(pl1, pl2)
# Aguardar até o próximo minuto
time_to_sleep = (event_minute - current_minute - 1) * 60 + (60 - current_second)
time_to_sleep = time_to_sleep - (time_to_sleep/3)
time_to_sleep = round(float(time_to_sleep), 1)
print(time_to_sleep)
tm.sleep(time_to_sleep)
# if (mint - hEm[1]) > 3:
# tm.sleep(60)
print('starting')
mtchdata = []
while len(mtchdata) == 0:
mtchdata = getmatchdata(pl1, pl2)
print(mtchdata)
print(f'med: | {med} > mtchdata: | {mtchdata} ?')
print(chkourate(med, mtchdata))
odd = mtchdata[0].split("-")[1]
if len(chkourate(med, mtchdata)) > 0:
calc = chkourate(med, mtchdata)
foundentry = (f"""
Over Asiático ({mtchdata[0].split("-")[2]}) - @{odd}\n
{pl1} vs {pl2}
({tm1}) vs ({tm2})\n""")
fnde_sent = bot.send_message(gpid, foundentry)
to_put = [fnde_sent.message_id, pl1, tm1, pl2, tm2, mtchdata, odd, calc]
queue.put(to_put)
tm.sleep(180)
else:
tm.sleep(60)
continue
elif verify_m_number == 2:
mlist = updatematchlist()
while len(mlist) == 0:
mlist = updatematchlist()
mlist = updatematchlist()
print(f'\n\n\n{mlist}')
pattern_pl = r'\((.*?)\)'
matchespl_fi = re.findall(pattern_pl, mlist[0][2])
pl1_fi , pl2_fi = matchespl_fi
# __________________________________________________
print(mlist)
matchespl_se = re.findall(pattern_pl, mlist[1][2])
pl1_se , pl2_se = matchespl_se
################################################
################################################
pattern_tm = r'\b([A-Z][a-zA-Z\s\d]+) \((.*?)\)'
matchestm_fi = re.findall(pattern_tm, mlist[0][2])
tm1_fi = matchestm_fi[0][0]
tm2_fi = matchestm_fi[1][0]
# __________________________________________________
matchestm_se = re.findall(pattern_tm, mlist[1][2])
tm1_se = matchestm_se[0][0]
tm2_se = matchestm_se[1][0]
hEm = mlist[0][1].split('\n')[1].split(':')
print(hEm)
mint = str(getnt().minute).zfill(2)
hr = str(getnt().hour).zfill(2)
print("hEm[0]:", str(hEm[0]).zfill(2))
print("hEm[1]:", hEm[1])
print("hr:", hr)
print("mint:", mint)
med_fi = overunderstat(pl1_fi, pl2_fi)
med_se = overunderstat(pl1_se, pl2_se)
if str(hEm[0]).zfill(2) != hr: print('different hr, looking for more')
if str(hEm[0]).zfill(2) == hr and (int(mint) < int(hEm[1]) or (mint == '00' and hEm[1] != '00')):
print('got it')
event_minute = int(hEm[1])
print(event_minute)
event_second = 0
current_minute = int(mint)
current_second = int(getnt().second)
print(f'First: ({pl1_fi},{pl2_fi})\nSecond:({pl1_se},{pl2_se})')
# Aguardar até o próximo minuto
time_to_sleep = (event_minute - current_minute - 1) * 60 + (60 - current_second)
time_to_sleep = time_to_sleep - (time_to_sleep/3)
time_to_sleep = round(float(time_to_sleep), 1)
print(time_to_sleep)
tm.sleep(time_to_sleep)
# ['Over-2.1-2.5', 'Under-1.67-2.5']
print('starting')
mtchdata_fi = []
mtchdata_se = []
fi_ver = 0
se_ver = 0
while len(mtchdata_fi) == 0 or len(mtchdata_se) == 0:
mtchdata_fi = getmatchdata(pl1_fi, pl2_fi)
mtchdata_se = getmatchdata(pl1_se, pl2_se)
print(f'mdata_fi: {mtchdata_fi} | mdata_se: {mtchdata_se}')
if len(mtchdata_fi) > 0 or len(mtchdata_se) > 0:
fi_ver = len(chkourate(med_fi, mtchdata_fi))
se_ver = len(chkourate(med_se, mtchdata_se))
if fi_ver > 0:
mtchdata_se = ['Over-1.85-0.5']
break
elif se_ver > 0:
mtchdata_fi = ['Over-1.85-0.5']
break
mtchdata_fi = getmatchdata(pl1_fi, pl2_fi)
mtchdata_se = getmatchdata(pl1_se, pl2_se)
xmidfi = float(med_fi) - 0.5
xmidse = float(med_se) - 0.5
print(f'med_fi: | {xmidfi} > mtchdata_fi: | {mtchdata_fi} ?')
print(f'med_se: | {xmidse} > mtchdata_se: | {mtchdata_se} ?')
# ['Over-2.1-2.5', 'Under-1.67-2.5']
if fi_ver > 0:
mtchdata = mtchdata_fi
med = med_fi
pl1 = pl1_fi
pl2 = pl2_fi
tm1 = tm1_fi
tm2 = tm2_fi
odd = mtchdata[0].split("-")[1]
calc = chkourate(med, mtchdata)
foundentry = (f"""
Over Asiático ({mtchdata[0].split("-")[2]}) - @{odd}\n
{pl1} vs {pl2}
({tm1}) vs ({tm2})\n""")
fnde_sent = bot.send_message(gpid, foundentry)
print("message suposed to be sent")
print(f'messageid: {fnde_sent.message_id}')
to_put = [fnde_sent.message_id, pl1, tm1, pl2, tm2, mtchdata, odd, calc]
queue.put(to_put)
tm.sleep(180)
elif se_ver > 0:
mtchdata = mtchdata_se
med = med_se
pl1 = pl1_se
pl2 = pl2_se
tm1 = tm1_se
tm2 = tm2_se
odd = mtchdata[0].split("-")[1]
calc = chkourate(med, mtchdata)
foundentry = (f"""
Over Asiático ({mtchdata[0].split("-")[2]}) - @{odd}\n
{pl1} vs {pl2}
({tm1}) vs ({tm2})\n""")
fnde_sent = bot.send_message(gpid, foundentry)
print("message suposed to be sent")
print(f'messageid: {fnde_sent.message_id}')
to_put = [fnde_sent.message_id, pl1, tm1, pl2, tm2, mtchdata, odd, calc]
queue.put(to_put)
tm.sleep(180)
else:
tm.sleep(60)
continue
# except Exception as e:
# bot.send_message('6162137618', f'Excpetion:\n\n{type(e)}\n{e}\n{e.__traceback__}')
# tm.sleep(4)
# continue
else:
print(verify_m_number)
tm.sleep(3)
def send_message_process(queue__):
global gpid
print('process started')
while True:
queue_data = queue__.get()
if queue_data is not None:
print(f"got queue_data: {queue_data}")
msg_id = queue_data[0]
pl1 = queue_data[1]
tm1 = queue_data[2]
pl2 = queue_data[3]
tm2 = queue_data[4]
mtchdata = queue_data[5]
odd = queue_data[6]
calc = queue_data[7]
tm.sleep(1200)
result = getfinalresult(pl1, tm1, pl2, tm2)
sc_tm1, sc_tm2 = map(int, result.split("x"))
totalgl = int(sc_tm1) + int(sc_tm2)
resultmsg_green = (f"""
Over Asiático ({mtchdata[0].split("-")[2]}) - @{odd}\n
{pl1} vs {pl2}
({tm1}) vs ({tm2})
\U00002705\U00002705\U00002705\U00002705
GREEN\n""")
resultmsg_red = (f"""
Over Asiático ({mtchdata[0].split("-")[2]}) - @{odd}\n
{pl1} vs {pl2}
({tm1}) vs ({tm2})
\U0000274C\U0000274C\U0000274C\U0000274C
RED\n""")
if float(totalgl) > float(calc):
try:
bot.edit_message_text(gpid, msg_id, resultmsg_green)
print("Message edited: GREEN")
except Exception as e:
print(f"Error editing message: {e}... Trying again")
try:
bot.edit_message_text(gpid, msg_id, resultmsg_green)
except Exception as e:
print('Failed to edit message... Continuing loop')
continue
elif float(totalgl) <= float(calc):
try:
bot.edit_message_text(gpid, msg_id, resultmsg_red)
print("Message edited: RED")
except Exception as e:
print(f"Error editing message: {e}... Trying again")
try:
bot.edit_message_text(gpid, msg_id, resultmsg_red)
except Exception as e:
print('Failed to edit message... Continuing loop')
continue
tm.sleep(3)
if __name__ == '__main__':
match_queue = multiprocessing.Queue()
search_process = multiprocessing.Process(target=startloop, args=(match_queue,))
send_message_process = multiprocessing.Process(target=send_message_process, args=(match_queue,))
search_process.start()
send_message_process.start()
search_process.join()
send_message_process.join()
1 个回答
0
请查看下面链接的文档,里面是关于telebot的,特别是edit_message_text这个方法。你传递参数的顺序不对,resultmsg应该是第一个传入的参数。修改后的代码应该是bot.edit_message_text(resultmsg_x, gpid, msg_id)