Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/Bot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import Web.Telegram.API.Bot (Chat (..), GetUpdatesRequest (..), Message (..),
Response (..), TelegramClient, Update (..),
User (..), getUpdatesM, getUpdatesRequest)

import BotCommands (BotCmd (..), addNote, readCommand, showNew, showOld)
import BotCommands (BotCmd (..), addNote, countNotes, readCommand, sendMessage,
showNew, showOld)
import Const (updateIdFile)
import Tools (putLog, saveOffset, tshow)

Expand Down Expand Up @@ -44,7 +45,13 @@ handleMessage update =
showOld (fromIntegral chat_id) user_id
WrongCommand wrongCmd ->
putLog $ cmdErr wrongCmd
Nothing -> addNote user_id text
Nothing -> do
addNote user_id text
notes <- countNotes user_id
sendMessage
(fromIntegral chat_id)
("Добавлено. Всего " <> (tshow notes) <>
" заметок.")
saveOffset updateIdFile update_id
Just msg ->
putLog $ "unhandled " <> tshow msg
Expand Down
12 changes: 12 additions & 0 deletions lib/BotCommands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
module BotCommands
( BotCmd (..)
, addNote
, countNotes
, readCommand
, sendMessage
, showNew
, showOld
) where
Expand All @@ -24,6 +26,16 @@ import DB (EntityField (NoteId, NoteOwner), Note (..), User (..), runDB)

data BotCmd = ShowNew | ShowOld | WrongCommand Text

countNotes :: Int
-> TelegramClient (Int)
countNotes userId = do
mUid <- runDB $ getKeyByValue DB.User{userTelegramId = fromIntegral userId}
case mUid of
Just uid -> do
notes <- runDB $ selectValList [NoteOwner ==. uid] []
pure $ length notes
Nothing -> pure 0

sendMessage :: Integer
-> Text
-> TelegramClient ()
Expand Down