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
|
2023-08-25 04:56:37 +08:00
|
|
|
import Control.Monad
|
2024-11-10 15:21:33 +00:00
|
|
|
import Data.Text (Text)
|
2022-04-04 08:14:42 +01:00
|
|
|
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-06-17 11:03:22 +01:00
|
|
|
import Simplex.Chat.Messages.CIContent
|
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
|
2024-11-10 15:21:33 +00:00
|
|
|
import Simplex.Messaging.Util (tshow)
|
2022-04-04 08:14:42 +01:00
|
|
|
import System.Directory (getAppUserDataDirectory)
|
|
|
|
import Text.Read
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
opts <- welcomeGetOpts
|
2023-10-11 09:50:11 +01:00
|
|
|
simplexChatCore terminalChatConfig opts 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
|
|
|
|
|
2024-11-10 15:21:33 +00:00
|
|
|
welcomeMessage :: Text
|
2023-02-14 07:57:27 +00:00
|
|
|
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
|
2023-09-27 11:41:02 +03:00
|
|
|
(_, _, resp) <- atomically . readTBQueue $ outputQ cc
|
2022-04-04 08:14:42 +01:00
|
|
|
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
|
2024-08-22 21:36:35 +04:00
|
|
|
CRNewChatItems {chatItems = (AChatItem _ SMDRcv (DirectChat contact) ChatItem {content = mc@CIRcvMsgContent {}}) : _} -> do
|
2024-11-10 15:21:33 +00:00
|
|
|
let msg = ciContentToText mc
|
|
|
|
number_ = readMaybe (T.unpack msg) :: Maybe Integer
|
2023-02-14 07:57:27 +00:00
|
|
|
sendMessage cc contact $ case number_ of
|
2024-11-10 15:21:33 +00:00
|
|
|
Just n -> msg <> " * " <> msg <> " = " <> tshow (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"
|