Commit ef186f95 authored by Nicolas Lenz's avatar Nicolas Lenz

Merge branch 'inline' into 'master'

Inline

See merge request !1
parents 9c73e9bd 30d6e53b
{-# LANGUAGE OverloadedStrings #-}
module Main where
module Main where -- TODO: Refactor this horrible module
import Data.Text (Text)
import Data.Maybe
import Control.Applicative
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Encoding as T
import Data.Char
import qualified Telegram.Bot.API as Telegram
import Telegram.Bot.Simple
import Telegram.Bot.Simple.Debug
import Telegram.Bot.Simple.UpdateParser
import Telegram.Bot.API.InlineMode
import Control.Monad.Reader
import Mock (styles)
import Mock.Help (styleHelp)
import Text.Mock
import Text.Mock.Help (styleHelp)
-- | Bot conversation state model.
......@@ -21,8 +24,9 @@ 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.
deriving (Show)
......@@ -34,18 +38,36 @@ bot = BotApp {
botHandler = handleAction,
botJobs = []}
mockify :: UpdateParser Action
mockify = UpdateParser f where
f :: Telegram.Update -> Maybe Action
-- | Whether the message was sent in a private chat.
isPrivate :: Telegram.Message -> Bool
isPrivate msg = case Telegram.chatType $ Telegram.messageChat msg of
Telegram.ChatTypePrivate -> True
_ -> False
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 -> return $ SendHelp
Just f -> return $ Reply $ f (T.unwords $ tail $ T.words txt)
Just f -> return $ f (T.unwords $ tail $ T.words txt)
_ -> if isPrivate message then return "Invalid mocking. See /help." else fail "Invalid mocking."
replyToInline :: UpdateParser [(Text, Text, Text)]
replyToInline = UpdateParser f where
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 []
else return $ map (\(name, f) -> (name, f txt, name <> id)) styles
-- |Concatenates a list of Maybe functions. Execution goes from left to right.
-- Returns Nothing if any of the elements of the list is Nothing.
......@@ -59,7 +81,13 @@ 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
handleUpdate _ = parseUpdate $
SendHelp <$ command "help"
<|> SendHelp <$ command "start"
<|> Reply <$> directMock
<|> InlineReply <$> replyToInline
-- <|> SendHelp <$ text
-- | How to handle 'Action's.
handleAction :: Action -> Model -> Eff Action Model
......@@ -67,6 +95,10 @@ handleAction NoAction model = pure model
handleAction (Reply message) model = model <# do
replyText message
pure NoAction
handleAction (InlineReply msgs) model = model <# do
let results = map (\(title, message, id) -> Telegram.InlineQueryResultArticle "article" id title (Telegram.InputTextMessageContent message) message) msgs
answerInlineQuery results
pure NoAction
handleAction SendHelp model = model <# do
reply $ (toReplyMessage help) {replyMessageParseMode = Just Telegram.Markdown, replyMessageDisableWebPagePreview = Just True}
pure NoAction
......@@ -84,10 +116,12 @@ main = T.readFile "config/token" >>= run . T.dropWhileEnd isSpace
-- |Help string.
help :: T.Text
help = T.unlines [
"*Mock*",
"*Mock " <> version <> "*",
"A Great BoT tO TRANsFoRM TEXt, wRiTten iN HaSKeLL.",
"By Nicolas Lenz. [Free and open source under the WTFPL.](https://git.eisfunke.com/software/mock-telegram-bot)",
"",
"*Inline usage:* Just type `@truemockbot` and the text you want to stylize in any chat. Telegram will show you a selection of the styles available.",
"",
"*Usage:* \\[STYLE] \\[TEXT]",
"*Example:* `random Cool Text`",
"",
......
cabal-version: 2.2
-- This file has been generated from package.yaml by hpack version 0.31.1.
-- This file has been generated from package.yaml by hpack version 0.31.2.
--
-- see: https://github.com/sol/hpack
--
-- hash: 9c41ed5721e932d7a05a04c7d08230e9f21412f1c8ce62ba3ba6f8c98ec9d066
-- hash: 7a3dff0fdd818b0e6a8697eb3807dd245287be140a833da3f3031246b85f903b
name: mock-telegram-bot
version: 0.2.0
......@@ -32,7 +32,9 @@ executable mock-telegram-bot
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
base
, cryptonite
, memory
, mock
, mtl
, telegram-bot-simple
......
......@@ -9,17 +9,18 @@ copyright: "2019 Nicolas Lenz"
extra-source-files:
- README.md
# Metadata used when publishing your package
synopsis: tELegrAm bOT iNteGratiOn fOR mOCK
category: Web
description: Please see the README at <https://git.eisfunke.com/software/mock-telegram-bot#readme>
dependencies:
- base >= 4.7 && < 5
- base
- telegram-bot-simple
- mock
- text
- mtl
- cryptonite
- memory
executables:
mock-telegram-bot:
......
......@@ -5,4 +5,6 @@ packages:
extra-deps:
- git: https://git.eisfunke.com/software/mock
commit: 1a07d08f679de0ff3420d19d7d1cf3e9499db182
commit: 1c7d88ae23a8ffa93a352329bd79d8214cc868fc
- git: https://github.com/Eisfunke/telegram-bot-simple
commit: 59ea013c2edf359d317517b3214085bac535f180
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