Commit 86940a65 authored by Nicolas Lenz's avatar Nicolas Lenz ❄️
Browse files

Some refactoring

parent 57e5fb6c
Loading
Loading
Loading
Loading
+16 −14
Original line number Diff line number Diff line
@@ -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