通过在电报电报Bot上发送命令来更改自定义键盘

时间:2018-09-04 20:07:28

标签: python python-3.x telegram-bot telepot

我将此行用于Telegram机器人上的自定义键盘:

markup = ReplyKeyboardMarkup(keyboard=[['Time', KeyboardButton(text='NewKey')],["File", "Audio"]])

我希望我的机器人在用户发送NewKey时将自定义键盘更改为另一个键盘。它将发送显示新的自定义键盘,但其布局将与默认键盘相同。 我已经尝试过了,但是没有用: elif command == 'NewKey': markup = ReplyKeyboardMarkup(keyboard=[['More', KeyboardButton(text='More2')],["More3", "More5"]])

这是我的机器人的完整代码:

import time, datetime
import telepot
from telepot.loop import MessageLoop
from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton

now = datetime.datetime.now()

def action(msg):
    chat_id = msg['`chat']['id']
    command = msg['text']

    print('Received: %s' % command)

    markup = ReplyKeyboardMarkup(keyboard=[['Time', KeyboardButton(text='NewKey')],["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))
    elif command == 'NewKey':
   markup = ReplyKeyboardMarkup(keyboard=[['More', KeyboardButton(text='More2')],["More3", "More5"]]))
    elif command == 'Time':
        telegram_bot.sendMessage(chat_id, str(now.hour)+str(":")+str(now.minute))
    elif command == '/file':
        telegram_bot.sendDocument(chat_id, document=open('/home/pi/Aisha.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)

`

0 个答案:

没有答案