如何在我的 Telegram 机器人上添加自定义键盘?
How can I add custom keyboard on my Telegram bot?
这是我的 Telegram 机器人代码:
import time, datetime
import telepot
from telepot.loop import MessageLoop
now = datetime.datetime.now()
def action(msg):
chat_id = msg['chat']['id']
command = msg['text']
print('Received: %s' % command)
if command == '/start':
telegram_bot.sendMessage (chat_id, str("Hi!"))
telegram_bot.sendMessage(chat_id, str(now.hour)+str(":")+str(now.minute))
elif command == 'Time':
telegram_bot.sendMessage(chat_id, str(now.hour)+str(":")+str(now.minute))
elif command == 'Logo':
telegram_bot.sendPhoto (chat_id, photo = "https://i.pinimg.com/avatars/circuitdigest_1464122100_280.jpg")
elif command == 'File':
telegram_bot.sendDocument(chat_id, document=open('/home/pi/sendbot.py'))
elif command == 'Audio':
telegram_bot.sendAudio(chat_id, audio=open('/home/pi/test.mp3'))
telegram_bot = telepot.Bot('MY-BOT-TOKEN')
print((telegram_bot.getMe()))
MessageLoop(telegram_bot, action).run_as_thread()
print('Up and Running....')
while 1:
time.sleep(10)
我想为这个机器人添加一个自定义键盘,它将有 4 个按钮,分别命名为 Time
、Logo
、File
和 Audio
。
例如,如果我单击 Time
按钮而不是键入 "Time",它会告诉我时间。
如果我单击 Logo
按钮而不是键入它,它将向我发送图片。
Telepot's Github project中有很好的例子。请检查它们。
...
markup = ReplyKeyboardMarkup(keyboard=[['Time', KeyboardButton(text='Logo')],["File", "Audio"]])
if command == '/start':
telegram_bot.sendMessage (chat_id, str("Hi! Which one do you want? choose from the below keyboard buttons."), reply_markup=markup)
telegram_bot.sendMessage(chat_id, str(now.hour)+str(":")+str(now.minute))
...
这是我的 Telegram 机器人代码:
import time, datetime
import telepot
from telepot.loop import MessageLoop
now = datetime.datetime.now()
def action(msg):
chat_id = msg['chat']['id']
command = msg['text']
print('Received: %s' % command)
if command == '/start':
telegram_bot.sendMessage (chat_id, str("Hi!"))
telegram_bot.sendMessage(chat_id, str(now.hour)+str(":")+str(now.minute))
elif command == 'Time':
telegram_bot.sendMessage(chat_id, str(now.hour)+str(":")+str(now.minute))
elif command == 'Logo':
telegram_bot.sendPhoto (chat_id, photo = "https://i.pinimg.com/avatars/circuitdigest_1464122100_280.jpg")
elif command == 'File':
telegram_bot.sendDocument(chat_id, document=open('/home/pi/sendbot.py'))
elif command == 'Audio':
telegram_bot.sendAudio(chat_id, audio=open('/home/pi/test.mp3'))
telegram_bot = telepot.Bot('MY-BOT-TOKEN')
print((telegram_bot.getMe()))
MessageLoop(telegram_bot, action).run_as_thread()
print('Up and Running....')
while 1:
time.sleep(10)
我想为这个机器人添加一个自定义键盘,它将有 4 个按钮,分别命名为 Time
、Logo
、File
和 Audio
。
例如,如果我单击 Time
按钮而不是键入 "Time",它会告诉我时间。
如果我单击 Logo
按钮而不是键入它,它将向我发送图片。
Telepot's Github project中有很好的例子。请检查它们。
...
markup = ReplyKeyboardMarkup(keyboard=[['Time', KeyboardButton(text='Logo')],["File", "Audio"]])
if command == '/start':
telegram_bot.sendMessage (chat_id, str("Hi! Which one do you want? choose from the below keyboard buttons."), reply_markup=markup)
telegram_bot.sendMessage(chat_id, str(now.hour)+str(":")+str(now.minute))
...