Python 电报机器人在我的私人聊天中回复,而不是在群聊中回复
我开发了一个简单的电报机器人,并将其添加到一个组中。每当发送命令\hey时,我尝试实现内联键盘。一切正常,除了当用户按下群组中的按钮时,机器人会回复他的私人聊天,而不是群组聊天。问题是Python 电报机器人在我的私人聊天中回复,而不是在群聊中回复,python,keyboard,telegram,telegram-bot,telepot,Python,Keyboard,Telegram,Telegram Bot,Telepot,我开发了一个简单的电报机器人,并将其添加到一个组中。每当发送命令\hey时,我尝试实现内联键盘。一切正常,除了当用户按下群组中的按钮时,机器人会回复他的私人聊天,而不是群组聊天。问题是telepot.glance(msg,flavor='callback\u query')返回用户的聊天id,而不是组的聊天id(比如telepot.glance(msg))。我希望能够在私人聊天和群组聊天中使用它,所以我尝试将chat\u id声明为一个全局变量,以便将其保存在chat\u message(msg
telepot.glance(msg,flavor='callback\u query')
返回用户的聊天id,而不是组的聊天id(比如telepot.glance(msg)
)。我希望能够在私人聊天和群组聊天中使用它,所以我尝试将chat\u id声明为一个全局变量,以便将其保存在chat\u message(msg)上的中,并在上的回调\u query(msg)
中重用。它可以工作,但如果我按下不是上次创建的键盘的按钮(例如,如果上次创建的键盘在私人聊天中,如果我按下之前在组中创建的键盘的按钮,则bot会在私人聊天中回复),它会变得混乱。有没有更干净的方法?
先谢谢你
from telepot.loop import MessageLoop
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
import time
import random
import emoji
from datetime import datetime
TOKEN="XXXXXXXXXXXX"
chat_id_SENSORI="XXXXXXXXXX"
link=["https://www.youtube.com/watch?v=ookGv44MMd4", "https://www.youtube.com/watch?v=D4MQZ-C2xZE", "https://www.youtube.com/watch?v=DgM85RuimG0", \
"https://www.youtube.com/watch?v=K9bf4PT-aEk&list=PLLnBnkDBWfVUk5_zlImdrOpZ6JRZ6wiwQ", "https://www.youtube.com/watch?v=YHMAdgr1t34", \
"https://www.youtube.com/watch?v=iRiWSmGz5E8", "https://www.youtube.com/watch?v=aHyOuMcaDdA", "https://www.youtube.com/watch?v=7WMaDyA8F0A"]
chat_id = ""
def on_chat_message(msg):
global chat_id
content_type, chat_type, chat_id = telepot.glance(msg)
keyboard = InlineKeyboardMarkup(inline_keyboard=[ [InlineKeyboardButton(text=emoji.emojize('Musica rilassante :musical_note:', use_aliases=True), callback_data='mus_ril'), \
InlineKeyboardButton(text=emoji.emojize('Lavori in corso :wrench:', use_aliases=True), callback_data='due')], \
[InlineKeyboardButton(text=emoji.emojize('Lavori in corso :tractor:', use_aliases=True), callback_data='tre')], ])
if content_type == 'text':
name = msg["from"]["first_name"]
txt = msg['text']
if '/start' in txt:
bot.sendMessage(chat_id,"Ciao! Sono LGbot. Sono stato creato per non farti annoiare durante le lezioni. Utilizza il comando /help per l'elenco di tutti i comandi.")
elif '/help' in txt:
bot.sendMessage(chat_id, "/start - Messaggio di benvenuto \n/help - Lista comandi \n/hey - Keyboard")
elif '/hey' in txt:
bot.sendMessage(chat_id, "Come te posso aiutà vellu?", reply_markup=keyboard)
return chat_id
def on_callback_query(msg):
global chat_id
query_id, chat_id_chat_privata, query_data = telepot.glance(msg, flavor='callback_query')
bot.sendMessage(chat_id_chat_privata, chat_id)
if query_data=='mus_ril':
bot.sendMessage(chat_id, link[random.randint(0,len(link)-1)])
bot.answerCallbackQuery(query_id, text="Goditi il meglio della musica italiana") #messaggio a comparsa
elif query_data=='due':
bot.sendMessage(chat_id, "Stiamo lavorando per voi..")
bot.answerCallbackQuery(query_id, text="Hai scelto il secondo pulsante") #messaggio a comparsa
elif query_data=='tre':
bot.sendMessage(chat_id, "Anche questo è da finire.. Nel frattempo pijete l'orario in ritardo di due ore")
ts = time.time()
bot.answerCallbackQuery(query_id, text=datetime.fromtimestamp(ts).strftime('%H:%M:%S')) #messaggio a comparsa
bot = telepot.Bot(TOKEN)
MessageLoop(bot, {'chat': on_chat_message, 'callback_query': on_callback_query}).run_as_thread()
x = 0
while True:
try:
A = time.ctime()
A = A.split()
B = datetime.strptime(A[3], "%H:%M:%S")
hour = B.hour + 2 #GMT to CEST time zone conversion
if hour >= 24:
hour = hour - 24
minute = B.minute
second = B.second
day = A[0]
#if x == 0 and minute == 37:
# bot.sendMessage(chat_id,str(hour))
# bot.sendMessage(chat_id,str(minute))
# bot.sendMessage(chat_id,str(second))
# bot.sendMessage(chat_id,day)
# x = x + 1
#bot.sendMessage(chat_id,A)
if day=='Mon' or day=='Tue':
if (hour,minute,second)==(17,30,0):
bot.sendMessage(chat_id_SENSORI,"Manca solo un'ora, resisti campione!")
time.sleep(10)
if (hour,minute,second)==(9,30,0):
bot.sendMessage(chat_id_SENSORI,"Me so rotto li cojoni pure io, figureteve voi atri")
time.sleep(10)
elif day=='Fri':
if (hour,minute,second)==(15,30,0):
bot.sendMessage(chat_id_SENSORI,"Manca solo un'ora, resisti campione!")
time.sleep(10)
except Exception as ex:
print(ex)
time.sleep(15)```