mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2024-12-17 17:20:21 +01:00
fa9e0086f6
* api
* UCR
* Revert "UCR"
This reverts commit 1f98d25192.
* comment
* events User
* events in api User
* CRActiveUser in APISetActiveUser
* process message with/without connection
* refactor
* mute error
* user in api responses
* name
* lost response
* user in CRChatCmdError
* compiles
* user in CRChatError
* -- UserId
* mute unused warning
* catch in withUser
* remove comment
Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
52 lines
2.0 KiB
Haskell
52 lines
2.0 KiB
Haskell
{-# LANGUAGE DuplicateRecordFields #-}
|
|
{-# LANGUAGE GADTs #-}
|
|
{-# LANGUAGE LambdaCase #-}
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
module Simplex.Chat.Bot where
|
|
|
|
import Control.Concurrent.Async
|
|
import Control.Concurrent.STM
|
|
import Control.Monad.Reader
|
|
import qualified Data.ByteString.Char8 as B
|
|
import qualified Data.Text as T
|
|
import Simplex.Chat.Controller
|
|
import Simplex.Chat.Core
|
|
import Simplex.Chat.Messages
|
|
import Simplex.Chat.Store
|
|
import Simplex.Chat.Types (Contact (..), User (..))
|
|
import Simplex.Messaging.Encoding.String (strEncode)
|
|
import System.Exit (exitFailure)
|
|
|
|
chatBotRepl :: String -> (String -> String) -> User -> ChatController -> IO ()
|
|
chatBotRepl welcome answer _user cc = do
|
|
initializeBotAddress cc
|
|
race_ (forever $ void getLine) . forever $ do
|
|
(_, resp) <- atomically . readTBQueue $ outputQ cc
|
|
case resp of
|
|
CRContactConnected _ contact _ -> do
|
|
contactConnected contact
|
|
void $ sendMsg contact welcome
|
|
CRNewChatItem _ (AChatItem _ SMDRcv (DirectChat contact) ChatItem {content}) -> do
|
|
let msg = T.unpack $ ciContentToText content
|
|
void . sendMsg contact $ answer msg
|
|
_ -> pure ()
|
|
where
|
|
sendMsg Contact {contactId} msg = sendChatCmd cc $ "/_send @" <> show contactId <> " text " <> msg
|
|
contactConnected Contact {localDisplayName} = putStrLn $ T.unpack localDisplayName <> " connected"
|
|
|
|
initializeBotAddress :: ChatController -> IO ()
|
|
initializeBotAddress cc = do
|
|
sendChatCmd cc "/show_address" >>= \case
|
|
CRUserContactLink _ UserContactLink {connReqContact} -> showBotAddress connReqContact
|
|
CRChatCmdError _ (ChatErrorStore SEUserContactLinkNotFound) -> do
|
|
putStrLn "No bot address, creating..."
|
|
sendChatCmd cc "/address" >>= \case
|
|
CRUserContactLinkCreated _ uri -> showBotAddress uri
|
|
_ -> putStrLn "can't create bot address" >> exitFailure
|
|
_ -> putStrLn "unexpected response" >> exitFailure
|
|
where
|
|
showBotAddress uri = do
|
|
putStrLn $ "Bot's contact address is: " <> B.unpack (strEncode uri)
|
|
void $ sendChatCmd cc "/auto_accept on"
|