2022-04-04 08:14:42 +01:00
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE GADTs #-}
|
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Main where
|
|
|
|
|
|
|
|
import Control.Concurrent.Async
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
import Control.Monad.Reader
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import Simplex.Chat.Bot
|
|
|
|
import Simplex.Chat.Controller
|
2022-04-10 17:13:06 +01:00
|
|
|
import Simplex.Chat.Core
|
2022-04-04 08:14:42 +01:00
|
|
|
import Simplex.Chat.Messages
|
2023-05-24 16:14:41 +04:00
|
|
|
import Simplex.Chat.Messages.ChatItemContent
|
2022-04-04 08:14:42 +01:00
|
|
|
import Simplex.Chat.Options
|
2022-05-11 16:52:08 +01:00
|
|
|
import Simplex.Chat.Terminal (terminalChatConfig)
|
2022-04-04 08:14:42 +01:00
|
|
|
import Simplex.Chat.Types
|
|
|
|
import System.Directory (getAppUserDataDirectory)
|
|
|
|
import Text.Read
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
opts <- welcomeGetOpts
|
2022-05-11 16:52:08 +01:00
|
|
|
simplexChatCore terminalChatConfig opts Nothing mySquaringBot
|
2022-04-04 08:14:42 +01:00
|
|
|
|
|
|
|
welcomeGetOpts :: IO ChatOpts
|
|
|
|
welcomeGetOpts = do
|
|
|
|
appDir <- getAppUserDataDirectory "simplex"
|
2023-02-18 17:39:16 +00:00
|
|
|
opts@ChatOpts {coreOptions = CoreChatOpts {dbFilePrefix}} <- getChatOpts appDir "simplex_bot"
|
2022-04-04 08:14:42 +01:00
|
|
|
putStrLn $ "SimpleX Chat Bot v" ++ versionNumber
|
|
|
|
putStrLn $ "db: " <> dbFilePrefix <> "_chat.db, " <> dbFilePrefix <> "_agent.db"
|
|
|
|
pure opts
|
|
|
|
|
2023-02-14 07:57:27 +00:00
|
|
|
welcomeMessage :: String
|
|
|
|
welcomeMessage = "Hello! I am a simple squaring bot.\nIf you send me a number, I will calculate its square"
|
|
|
|
|
2022-04-04 08:14:42 +01:00
|
|
|
mySquaringBot :: User -> ChatController -> IO ()
|
|
|
|
mySquaringBot _user cc = do
|
|
|
|
initializeBotAddress cc
|
|
|
|
race_ (forever $ void getLine) . forever $ do
|
|
|
|
(_, resp) <- atomically . readTBQueue $ outputQ cc
|
|
|
|
case resp of
|
2023-01-05 20:38:31 +04:00
|
|
|
CRContactConnected _ contact _ -> do
|
2022-04-04 08:14:42 +01:00
|
|
|
contactConnected contact
|
2023-02-14 07:57:27 +00:00
|
|
|
sendMessage cc contact welcomeMessage
|
|
|
|
CRNewChatItem _ (AChatItem _ SMDRcv (DirectChat contact) ChatItem {content = mc@CIRcvMsgContent {}}) -> do
|
|
|
|
let msg = T.unpack $ ciContentToText mc
|
2022-04-04 08:14:42 +01:00
|
|
|
number_ = readMaybe msg :: Maybe Integer
|
2023-02-14 07:57:27 +00:00
|
|
|
sendMessage cc contact $ case number_ of
|
2022-04-04 08:14:42 +01:00
|
|
|
Just n -> msg <> " * " <> msg <> " = " <> show (n * n)
|
2023-02-14 07:57:27 +00:00
|
|
|
_ -> "\"" <> msg <> "\" is not a number"
|
2022-04-04 08:14:42 +01:00
|
|
|
_ -> pure ()
|
|
|
|
where
|
|
|
|
contactConnected Contact {localDisplayName} = putStrLn $ T.unpack localDisplayName <> " connected"
|