telegrambot 버튼 이어서 진행하기
ConversationHandler 라고 한다.
from telegram.ext import Updater, Filters, ConversationHandler, CallbackQueryHandler
HEY = 200
ACTION = 201
EAT = 202
PLAY = 203
LOVE = 204
kairos_token = "[각자 발급받은 토큰]"
updater = Updater(kairos_token)
def hey(update, context, args):
hey_message = "이봐.... 나 " + str(args[0]) + "가 원하는 것은"
reply_markup = InlineKeyboardMarkup([
[InlineKeyboardButton("밥줘", callback_data="eat")]
,[InlineKeyboardButton("놀아줘", callback_data="play")]
,[InlineKeyboardButton("쓰다듬어줘", callback_data="love")]
])
context.message.reply_text(text=hey_message, reply_markup=reply_markup)
return ACTION
def action(update, context):
query = context.callback_query
chat_id = query.message.chat_id
if query.data == "eat":
update.edit_message_text(
chat_id=chat_id
,message_id=query.message.message_id
,text="뭐먹을래"
)
reply_markup = InlineKeyboardMarkup([
[InlineKeyboardButton("사료", callback_data="food")]
,[InlineKeyboardButton("츄르", callback_data="choo")]
])
update.edit_message_reply_markup(
chat_id=chat_id
,message_id=query.message.message_id
,reply_markup=reply_markup
)
return EAT
elif query.data == "play":
update.edit_message_text(
chat_id=chat_id
,message_id=query.message.message_id
,text="뭐하고놀까"
)
reply_markup = InlineKeyboardMarkup([
[InlineKeyboardButton("사냥", callback_data="hunt")]
,[InlineKeyboardButton("숨바꼭질", callback_data="hide")]
])
update.edit_message_reply_markup(
chat_id=chat_id
,message_id=query.message.message_id
,reply_markup=reply_markup
)
return PLAY
elif query.data == "love":
update.edit_message_text(
chat_id=chat_id
,message_id=query.message.message_id
,text="어딜"
)
reply_markup = InlineKeyboardMarkup([
[InlineKeyboardButton("머리", callback_data="head")]
,[InlineKeyboardButton("턱", callback_data="chin")]
,[InlineKeyboardButton("몸", callback_data="body")]
])
update.edit_message_reply_markup(
chat_id=chat_id
,message_id=query.message.message_id
,reply_markup=reply_markup
)
return LOVE
def eat(update, context):
query = context.callback_query
chat_id = query.message.chat_id
if query.data == "food":
update.edit_message_text(
chat_id=chat_id
,message_id=query.message.message_id
,text="밥먹자"
)
elif query.data == "choo":
update.edit_message_text(
chat_id=chat_id
,message_id=query.message.message_id
,text="간식은 조금만"
)
return ConversationHandler.END
def play(update, context):
query = context.callback_query
chat_id = query.message.chat_id
if query.data == "hunt":
update.edit_message_text(
chat_id=chat_id
,message_id=query.message.message_id
,text="딸랑딸랑"
)
elif query.data == "hide":
update.edit_message_text(
chat_id=chat_id
,message_id=query.message.message_id
,text="10 9 8 7 ..."
)
return ConversationHandler.END
def love(update, context):
query = context.callback_query
chat_id = query.message.chat_id
update.edit_message_text(
chat_id=chat_id
,message_id=query.message.message_id
,text="쓰담쓰담"
)
return ConversationHandler.END
test_conv_handler = ConversationHandler(
entry_points=[CommandHandler('hey', hey, pass_args=True)]
,states={
ACTION: [CallbackQueryHandler(action)]
,EAT: [CallbackQueryHandler(eat)]
,PLAY: [CallbackQueryHandler(play)]
,LOVE: [CallbackQueryHandler(love)]
}
,fallbacks=[CommandHandler('hey', hey, pass_args=True)]
)
updater.dispatcher.add_handler(test_conv_handler)
updater.start_polling()
updater.idle()
import 하고 (ConversationHandler, CallbackQueryHandler ...)
상수 지정 HEY, ACTION, EAT, PLAY, LOVE = range(5)
HEY, ACTION, EAT, PLAY, LOVE = range(5)
라고 하면 깔끔
토큰과 updater 변수 저장해주고,
함수들 선언
ConversationHandler 선언 후 add_handler에 넣어주기
return [상수명] 을 써준다. 끝내기 위해서는 return Conversation.END
handler 선언 시 다른 변수 등을 넘기고 싶은 경우, 함수명 이후에 콤마로 구분하여 써주면 된다.
CREATE: [CallbackQueryHandler(create, pass_job_queue=True)]
entry_points 는 시작점, fallbacks 는 오류 시..
아래는 실행 영상~.~
728x90
반응형
'개발' 카테고리의 다른 글
github blog theme 변경 (0) | 2020.06.06 |
---|---|
ubuntu file encoding 확인 (1) | 2020.05.15 |
telegrambot /명령어 입력받기 (0) | 2020.05.09 |
telegrambot 생성 (0) | 2020.05.09 |
flutter dependency (0) | 2020.05.03 |