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"
|
2025-01-10 15:27:29 +04:00
|
|
|
opts@ChatOpts {coreOptions} <- getChatOpts appDir "simplex_bot"
|
2022-04-04 08:14:42 +01:00
|
|
|
putStrLn $ "SimpleX Chat Bot v" ++ versionNumber
|
2025-01-10 15:27:29 +04:00
|
|
|
printDbOpts coreOptions
|
2022-04-04 08:14:42 +01:00
|
|
|
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
|
2025-05-04 22:14:36 +01:00
|
|
|
(_, evt) <- atomically . readTBQueue $ outputQ cc
|
|
|
|
case evt of
|
|
|
|
CEvtContactConnected _ contact _ -> do
|
2022-04-04 08:14:42 +01:00
|
|
|
contactConnected contact
|
2023-02-14 07:57:27 +00:00
|
|
|
sendMessage cc contact welcomeMessage
|
2025-05-04 22:14:36 +01:00
|
|
|
CEvtNewChatItems {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"
|