-
Notifications
You must be signed in to change notification settings - Fork 0
/
Post.hs
executable file
·75 lines (58 loc) · 1.92 KB
/
Post.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings,ScopedTypeVariables#-}
module Post (
Message(Message), msType, msId, msText, LINEEvent(LINEEvent), evType, evReplyToken, evTimeStamp, evMessage, LINEReq(..),
evSource, srcUserId, srcGroupId,srcType) where
import Control.Lens
import Control.Monad (mzero)
import Data.Aeson as Aeson
import Get (UserId(..), ReplyToken(..), GroupId(..))
data Source = Source {
_srcUserId :: Maybe UserId
,_srcType :: String
,_srcGroupId :: Maybe GroupId
} deriving (Show)
makeLenses ''Source
data Message = Message {
_msType :: String
, _msId :: Int
, _msText :: String
} deriving Show
makeLenses ''Message
data LINEEvent = LINEEvent {
_evType :: String
, _evReplyToken :: ReplyToken
, _evSource :: Source --"source":{"userId":"Ub0292059288c2655f61486607cf0c7b9","type":"user"},
, _evTimeStamp:: Int
, _evMessage :: Maybe Message
} deriving Show
makeLenses ''LINEEvent
newtype LINEReq = Events { fromLINEReq :: [LINEEvent] } deriving Show
instance FromJSON LINEReq where
parseJSON (Object v) = do
(arr::[LINEEvent]) <- v .: "events"
return $ Events arr
parseJSON _ = mzero
instance FromJSON Message where
parseJSON (Object v) = do
t <- v .: "type"
i <- v .: "id"
text <- v .: "text"
return $ Message t (read i::Int) text
parseJSON _ = mzero
instance FromJSON LINEEvent where
parseJSON (Object v) = do
t <- v .: "type"
rt <- v .: "replyToken"
src <- v .: "source"
ts <- v .: "timestamp"
(ms::Maybe Message) <- v .:? "message"
return $ LINEEvent { _evType =t, _evReplyToken=(ReplyToken rt), _evSource=src, _evTimeStamp=ts, _evMessage=ms}
parseJSON _ = mzero
instance FromJSON Source where
parseJSON (Object v) = do
typ <- v .: "type"
uid <- v .:? "userId"
gid <- v .:? "groupId"
return $ Source { _srcUserId = fmap UserId uid, _srcType =typ, _srcGroupId = fmap GroupId gid}
parseJSON _ = mzero