-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from cofree-coffee/feature/parsing
Bot I/O Serialization
- Loading branch information
Showing
15 changed files
with
348 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,34 @@ | ||
module Data.Chat.Bot.CoinFlip where | ||
module Data.Chat.Bot.CoinFlip | ||
( -- * Bot | ||
coinFlipBot, | ||
|
||
-- * Serializer | ||
coinFlipSerializer, | ||
) | ||
where | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
import Control.Monad.ListT (emptyListT) | ||
import Control.Monad.Reader | ||
import Data.Attoparsec.Text | ||
import Data.Bifunctor (bimap) | ||
import Data.Chat.Bot | ||
import Data.Profunctor | ||
import Data.Chat.Bot (Bot) | ||
import Data.Chat.Bot.Serialization (TextSerializer) | ||
import Data.Chat.Bot.Serialization qualified as S | ||
import Data.Text (Text) | ||
import Data.Text qualified as Text | ||
import System.Random (randomIO) | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
coinFlipBot :: Bot IO () () Bool | ||
coinFlipBot = do | ||
randomIO | ||
|
||
simplifyCoinFlipBot :: forall s. Bot IO s () Bool -> Bot IO s Text Text | ||
simplifyCoinFlipBot b = do | ||
t <- ask | ||
case to t of | ||
Left _err -> Bot $ pure $ const emptyListT | ||
Right _ -> dimap (const ()) from $ b | ||
where | ||
to :: Text -> Either Text () | ||
to = fmap (bimap Text.pack id) $ parseOnly parseCoinFlipCommand | ||
|
||
from :: Bool -> Text | ||
from = \case | ||
True -> "Coin Flip Result: True" | ||
False -> "Coin Flip Result: False" | ||
|
||
parseCoinFlipCommand :: Parser () | ||
parseCoinFlipCommand = "flip a coin" *> pure () | ||
coinFlipBot = randomIO | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
coinFlipSerializer :: TextSerializer Bool () | ||
coinFlipSerializer = S.Serializer {parser, printer} | ||
|
||
parser :: Text -> Maybe () | ||
parser = either (const Nothing) Just . parseOnly ("flip a coin" *> pure ()) | ||
|
||
printer :: Bool -> Text | ||
printer x = "Coin Flip Result: " <> Text.pack (show x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,36 @@ | ||
-- | The Simplest Bot. This module serves as an introductory example | ||
-- for bot construction. | ||
module Data.Chat.Bot.Hello | ||
( helloSimpleBot, | ||
helloMatrixBot, | ||
( -- * Bot | ||
helloBot, | ||
|
||
-- * Serializer | ||
helloBotSerializer, | ||
helloBotParser, | ||
) | ||
where | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
import Control.Monad.ListT (emptyListT) | ||
import Data.Chat.Bot | ||
import Data.Chat.Server.Matrix | ||
import Data.Chat.Bot.Serialization | ||
import Data.Text (Text) | ||
import Data.Text qualified as Text | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
-- | A pure, stateless bot which simply takes a 'Text' input and | ||
-- produces a 'Text' output from it. | ||
helloSimpleBot :: Monad m => Bot m s Text Text | ||
helloSimpleBot = Bot $ \s msg -> | ||
-- | A pure, stateless bot which produces a 'Text' output. | ||
helloBot :: Monad m => Bot m s () Text | ||
helloBot = Bot $ \s () -> pure ("Are you talking to me, punk?", s) | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
helloBotParser :: Text -> Maybe () | ||
helloBotParser msg = | ||
let name = "cofree-bot" | ||
in if name `Text.isInfixOf` msg | ||
then pure ("Are you talking to me, punk?", s) | ||
else emptyListT | ||
then Just () | ||
else Nothing | ||
|
||
-- | We can then embed our bot in the Matrix API using | ||
-- 'liftSimpleBot'. | ||
helloMatrixBot :: Monad m => Bot m () (RoomID, Event) (RoomID, Event) | ||
helloMatrixBot = embedTextBot helloSimpleBot | ||
helloBotSerializer :: TextSerializer Text () | ||
helloBotSerializer = Serializer helloBotParser id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.