Commit 86940a65 authored by Nicolas Lenz's avatar Nicolas Lenz

Some refactoring

parent 57e5fb6c
......@@ -24,7 +24,7 @@ data Model = Model deriving (Show)
-- | Actions bot can perform.
data Action
= NoAction -- ^ Perform no action.
= NoAction -- ^ Perform no action.
| Reply Text -- ^ Reply some text.
| InlineReply [(Text, Text, Text)]
| SendHelp -- ^Send help text.
......@@ -38,31 +38,29 @@ bot = BotApp {
botHandler = handleAction,
botJobs = []}
mockify :: UpdateParser Action
mockify = UpdateParser f where
f :: Telegram.Update -> Maybe Action
directMock :: UpdateParser Text
directMock = UpdateParser f where
f :: Telegram.Update -> Maybe Text
f update = do
message <- Telegram.updateMessage update
txt <- Telegram.messageText message
let styleNames = T.splitOn "|" . T.toLower . head . T.words $ txt
if length styleNames > 5 then
return $ Reply "Only concatenations of up to 5 styles are allowed."
return "Only concatenations of up to 5 styles are allowed."
else case concatMaybeFunctions . map (\s -> lookup s styles) $ styleNames of
Nothing -> case Telegram.chatType (Telegram.messageChat message) of
Telegram.ChatTypePrivate -> return SendHelp
_ -> return NoAction
Just f -> return $ Reply $ f (T.unwords $ tail $ T.words txt)
Just f -> return $ f (T.unwords $ tail $ T.words txt)
_ -> fail "Invalid mocking"
replyToInline :: UpdateParser Action
replyToInline :: UpdateParser [(Text, Text, Text)]
replyToInline = UpdateParser f where
f :: Telegram.Update -> Maybe Action
f :: Telegram.Update -> Maybe [(Text, Text, Text)]
f update = do
inlineQuery <- Telegram.updateInlineQuery update
let txt = Telegram.inlineQueryQuery inlineQuery
let id = Telegram.inlineQueryId inlineQuery
if T.empty == txt
then return $ InlineReply []
else return $ InlineReply $ map (\(name, f) -> (name, f txt, name <> id)) styles
then return []
else return $ map (\(name, f) -> (name, f txt, name <> id)) styles
-- |Concatenates a list of Maybe functions. Execution goes from left to right.
......@@ -77,7 +75,11 @@ concatMaybeFunctions (mf:mfs) = do
-- | How to process incoming 'Telegram.Update's
-- and turn them into 'Action's.
handleUpdate :: Model -> Telegram.Update -> Maybe Action
handleUpdate _ = parseUpdate $ mockify <|> replyToInline
handleUpdate _ = parseUpdate $
Reply <$> directMock
<|> InlineReply <$> replyToInline
<|> SendHelp <$ command "help"
<|> SendHelp <$ text
-- | How to handle 'Action's.
handleAction :: Action -> Model -> Eff Action Model
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment