refactor major
This commit is contained in:
82
_archiv/bot.py
Normal file
82
_archiv/bot.py
Normal file
@@ -0,0 +1,82 @@
|
||||
import logging
|
||||
|
||||
from telegram.ext import CommandHandler, CallbackQueryHandler
|
||||
from telegram.ext import Updater
|
||||
from telegram import InlineKeyboardButton,InlineKeyboardMarkup
|
||||
from telegram.ext import MessageHandler, Filters
|
||||
from key import API_KEY
|
||||
from bot.states import CreatePostWorkflow
|
||||
from flask import Flask
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route('/')
|
||||
def hello_world():
|
||||
return 'Hello, World!'
|
||||
|
||||
# Bot Updater that will poll
|
||||
updater = Updater(token=API_KEY, use_context=True)
|
||||
# The Dispatcher that get all the Handlers
|
||||
dispatcher = updater.dispatcher
|
||||
logger = logging.getLogger()
|
||||
logger.setLevel(logging.INFO)
|
||||
ch = logging.StreamHandler()
|
||||
ch.setLevel(logging.DEBUG)
|
||||
logger.addHandler(ch)
|
||||
|
||||
workflows = {}
|
||||
|
||||
logger.info('initializing')
|
||||
def start(update, context):
|
||||
workflows[update.effective_chat.id]=CreatePostWorkflow(chat_id=update.effective_chat.id,context=context)
|
||||
logger.info("Start")
|
||||
context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text)
|
||||
|
||||
start_handler = CommandHandler('start', start)
|
||||
dispatcher.add_handler(start_handler)
|
||||
|
||||
def test(update, context):
|
||||
# workflows[update.effective_chat.id]=CreatePostWorkflow(chat_id=update.effective_chat.id,context=context)
|
||||
logger.info("Test")
|
||||
keyboard = [[InlineKeyboardButton("Option 1", callback_data='1'),
|
||||
InlineKeyboardButton("Option 2", callback_data='2')],
|
||||
[InlineKeyboardButton("Option 3", callback_data='3')]]
|
||||
context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text,reply_markup=InlineKeyboardMarkup(keyboard))
|
||||
|
||||
|
||||
dispatcher.add_handler( CommandHandler('test', test))
|
||||
|
||||
def echo(update,context):
|
||||
logger = logging.getLogger()
|
||||
# logger.info("echo")
|
||||
if update.effective_chat.id in workflows:
|
||||
# text= "Current State is: %s" % workflows[update.effective_chat.id].current_state
|
||||
# context.bot.send_message(chat_id=update.effective_chat.id, text=text)
|
||||
workflows[update.effective_chat.id].message_handler(update,context)
|
||||
|
||||
def command(update,context):
|
||||
if update.effective_chat.id in workflows:
|
||||
workflows[update.effective_chat.id].command_handler(update,context)
|
||||
|
||||
|
||||
def button(update, context):
|
||||
if update.effective_chat.id in workflows:
|
||||
workflows[update.effective_chat.id].button_handler(update,context)
|
||||
|
||||
#query = update.callback_query
|
||||
#query.edit_message_text(text="Selected option: {}".format(que#ry.data))
|
||||
|
||||
|
||||
dispatcher.add_handler(CallbackQueryHandler(button))
|
||||
|
||||
|
||||
echo_handler = MessageHandler(Filters.text & (~Filters.command), echo)
|
||||
dispatcher.add_handler(echo_handler)
|
||||
|
||||
command_handler = MessageHandler(Filters.command, command)
|
||||
dispatcher.add_handler(command_handler)
|
||||
|
||||
|
||||
print("Added Echo")
|
||||
|
||||
updater.start_polling()
|
||||
#app.run()
|
||||
0
_archiv/bot/__init__.py
Normal file
0
_archiv/bot/__init__.py
Normal file
113
_archiv/bot/states.py
Normal file
113
_archiv/bot/states.py
Normal file
@@ -0,0 +1,113 @@
|
||||
from statemachine import StateMachine, State
|
||||
from telegram import InlineKeyboardButton,InlineKeyboardMarkup
|
||||
|
||||
import logging
|
||||
logger=logging.getLogger()
|
||||
|
||||
class TelegramStateMachine(StateMachine):
|
||||
def __init__(self,chat_id=None, context=None*args,**kwargs):
|
||||
self.chat_id = chat_id
|
||||
self.user_id = user_id
|
||||
self.context=context
|
||||
|
||||
super().__init__(*args,**kwargs)
|
||||
|
||||
def send(self,text:str):
|
||||
self.context.bot.send_message(chat_id=self.chat_id, text=text)
|
||||
|
||||
def send_confirm(self,text:str):
|
||||
keyboard = [[InlineKeyboardButton("OK", callback_data='ok'),
|
||||
InlineKeyboardButton("Repeat", callback_data='retry')],
|
||||
[ InlineKeyboardButton("Cancel", callback_data='cancel')] ]
|
||||
#,
|
||||
# InlineKeyboardButton("Cancel", callback_data='3')]
|
||||
#keyboard = [[InlineKeyboardButton("OK", callback_data='1'),
|
||||
#]]
|
||||
|
||||
self.context.bot.send_message(chat_id=self.chat_id,
|
||||
text=text,
|
||||
reply_markup=InlineKeyboardMarkup(keyboard))
|
||||
|
||||
|
||||
|
||||
class CreatePostWorkflow(TelegramStateMachine):
|
||||
init =State('init', initial=True)
|
||||
|
||||
wait = State('wait',value=0)
|
||||
confirm =State('confirm')
|
||||
finished = State('finished')
|
||||
|
||||
initialize = init.to(wait)
|
||||
entered = wait.to(confirm)
|
||||
next=confirm.to(wait)
|
||||
retry=confirm.to(wait)
|
||||
|
||||
steps={
|
||||
0:'title',
|
||||
1:'text'
|
||||
}
|
||||
|
||||
finish =confirm.to(finished)
|
||||
cancel = finished.from_(wait,confirm)
|
||||
@property
|
||||
def value(self):
|
||||
return self.p[self.steps[self.step]]
|
||||
|
||||
def __init__(self,chat_id=None, context=None,*args, **kwargs):
|
||||
super().__init__(chat_id, context, *args, **kwargs)
|
||||
self.p=dict()
|
||||
self.step=0
|
||||
self.initialize()
|
||||
|
||||
|
||||
def confirmed(self):
|
||||
if self.step >= len(self.steps)-1:
|
||||
self.finish()
|
||||
else:
|
||||
self.next()
|
||||
|
||||
def message_handler(self,update,context):
|
||||
if self.current_state==self.wait:
|
||||
self.p[self.steps[self.step]]=update.message.text
|
||||
self.send(str(self.p))
|
||||
self.entered()
|
||||
elif self.current_state==self.confirm:
|
||||
if update.message.text=="ok":
|
||||
self.confirmed()
|
||||
else:
|
||||
self.retry()
|
||||
#file = bot.getFile(update.message.photo[-1].file_id
|
||||
|
||||
def button_handler(self, update, context):
|
||||
query = update.callback_query
|
||||
if self.current_state ==self.confirm:
|
||||
if query.data=='ok':
|
||||
self.confirmed()
|
||||
elif query.data=='cancel':
|
||||
self.cancel()
|
||||
else:
|
||||
self.retry()
|
||||
query.edit_message_text(text="Selected option: {}".format(query.data))
|
||||
|
||||
def command_handler(self,update,context):
|
||||
logger.info("Processing Command: %s" % update.message.text)
|
||||
|
||||
def on_next(self):
|
||||
self.step+=1
|
||||
|
||||
def on_cancel(self):
|
||||
self.p={}
|
||||
self.step=0
|
||||
self.send("Canceled")
|
||||
|
||||
def on_finish(self):
|
||||
self.step=0
|
||||
self.send("Eingabe fertig:%s" % str(self.p))
|
||||
|
||||
def on_enter_confirm(self):
|
||||
self.send_confirm("Bitte die Eingabe von %s bestaetigen" % self.value)
|
||||
|
||||
def on_enter_wait(self):
|
||||
self.send("Bitte folgendes das Attribut %s eingeben " % self.steps[self.step])
|
||||
|
||||
|
||||
74
_archiv/test.py
Normal file
74
_archiv/test.py
Normal file
@@ -0,0 +1,74 @@
|
||||
import logging
|
||||
|
||||
from telegram.ext import CommandHandler, CallbackQueryHandler
|
||||
from telegram.ext import Updater
|
||||
from telegram import InlineKeyboardButton,InlineKeyboardMarkup
|
||||
|
||||
from telegram.ext import MessageHandler, Filters
|
||||
|
||||
updater = Updater(token='317750953:AAFzRryrTGUhnaaHgXfvVUkvk5WKgyTQ7CU', use_context=True)
|
||||
dispatcher = updater.dispatcher
|
||||
logger = logging.getLogger()
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
workflows = {}
|
||||
|
||||
|
||||
def start(update, context):
|
||||
logger.info("start")
|
||||
keyboard = [[InlineKeyboardButton("Option 1", callback_data='1'),
|
||||
InlineKeyboardButton("Option 2", callback_data='2')],
|
||||
[InlineKeyboardButton("Option 3", callback_data='3')]]
|
||||
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
update.message.reply_text('Please choose:', reply_markup=reply_markup)
|
||||
|
||||
|
||||
#context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")
|
||||
|
||||
start_handler = CommandHandler('start', start)
|
||||
dispatcher.add_handler(start_handler)
|
||||
print("StartCommand")
|
||||
|
||||
def echo(update, context):
|
||||
logger.info("echo")
|
||||
context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text)
|
||||
|
||||
echo_handler = MessageHandler(Filters.text & (~Filters.command), echo)
|
||||
dispatcher.add_handler(echo_handler)
|
||||
print("Added Echo")
|
||||
|
||||
def caps(update, context):
|
||||
text_caps = ' '.join(context.args).upper()
|
||||
context.bot.send_message(chat_id=update.effective_chat.id, text=text_caps)
|
||||
|
||||
caps_handler = CommandHandler('caps', caps)
|
||||
dispatcher.add_handler(caps_handler)
|
||||
|
||||
from telegram import InlineQueryResultArticle, InputTextMessageContent
|
||||
def inline_caps(update, context):
|
||||
query = update.inline_query.query
|
||||
if not query:
|
||||
return
|
||||
results = list()
|
||||
results.append(
|
||||
InlineQueryResultArticle(
|
||||
id=query.upper(),
|
||||
title='Caps',
|
||||
input_message_content=InputTextMessageContent(query.upper())
|
||||
)
|
||||
)
|
||||
context.bot.answer_inline_query(update.inline_query.id, results)
|
||||
|
||||
from telegram.ext import InlineQueryHandler
|
||||
inline_caps_handler = InlineQueryHandler(inline_caps)
|
||||
dispatcher.add_handler(inline_caps_handler)
|
||||
|
||||
|
||||
def button(update, context):
|
||||
query = update.callback_query
|
||||
query.edit_message_text(text="Selected option: {}".format(query.data))
|
||||
updater.dispatcher.add_handler(CallbackQueryHandler(button))
|
||||
|
||||
|
||||
updater.start_polling()
|
||||
Reference in New Issue
Block a user