我开发了一个简单的 Telegram 机器人并将其添加到一个组中。每当发送命令 \hey 时,我都尝试实现内联键盘。一切正常,除了当用户按下群组中的按钮时,机器人回复他的私人聊天而不是群组聊天。问题是telepot.glance(msg, flavor='callback_query')
返回用户的chat_id 而不是组的chat_id(就像telepot.glance(msg)
会做的那样)。我希望能够在私人聊天和群聊中使用它,所以我尝试将 chat_id 声明为全局变量,以便将其保存并重on_chat_message(msg)
用on_callback_query(msg)
. 它可以工作,但是如果我按下不是最后创建的键盘的按钮,它会变得混乱(例如,如果最后创建的键盘在私人聊天中,如果我按下组中先前创建的键盘的按钮机器人在私人聊天中回复)。有没有更清洁的方法来做到这一点?先感谢您
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)```