-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
102 lines (89 loc) · 2.76 KB
/
Main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ExtendedDefaultRules #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE PatternGuards #-}
{-# OPTIONS_GHC -fno-warn-deprecations #-}
module Main where
import System.IO
import Data.ByteString.Lazy.Char8
import Data.ByteString.Lazy.Char8()
import GHC.Types (IO (..))
import Control.Monad
import Data.Char(toUpper)
import Data.Maybe
import Data.Monoid ((<>))
import Data.Bool
import Prelude
import Network.HTTP.Client.TLS (getGlobalManager)
import Network.HTTP.Client
import Network.Linklater
import Control.Monad.Trans.Maybe
import Data.Aeson
import Servant.Common.Req
import qualified Data.Text as T
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Maybe (MaybeT, runMaybeT)
import Data.Aeson (encode)
import Data.Text (Text)
import Network.HTTP.Types
import Network.TLS
import Network.Wai.Handler.Warp (run)
import Network.Wreq
import Network.HTTP.Types.Status (statusCode)
import Network.HTTP.Base
import Servant.Client (ClientEnv(ClientEnv), runClientM)
import Web.Yahoo.Finance.YQL
import Reddit
import Reddit.Types.Post
import Control.Monad
import Control.Monad.IO.Class
import qualified Data.Text as Text
import qualified Data.Text.IO as Text
readSlackFile :: FilePath -> IO Text
readSlackFile filename =
T.filter (/= '\n') . T.pack <$> Prelude.readFile filename
configIO :: IO Config
configIO =
Config <$> (readSlackFile "hook")
parseText :: Text -> Maybe Text
parseText text = case T.strip text of
"" -> Nothing
x -> Just x
liftMaybe :: Maybe a -> IO a
liftMaybe = maybe mzero return
messageOfCommandStock :: Command -> IO Network.Linklater.Message
messageOfCommandStock (Command "stock" user channel (Just text)) = do
manager <- getGlobalManager
query <- liftMaybe (parseText text)
res <- runClientM (getQuotes (YQLQuery [StockSymbol query])) (ClientEnv manager yahooFinanceJsonBaseUrl)
case res of
Right responseQuotes ->
return (messageOf [FormatAt user, FormatString (T.pack(show responseQuotes))])
where
messageOf =
FormattedMessage(EmojiIcon "gift") "stockbot" channel
stockify :: Maybe Command -> IO Text
stockify Nothing =
return "Unrecognized Slack request!"
stockify (Just command) = do
Prelude.putStrLn ("+ Incoming command: " <> show command)
message <- (messageOfCommandStock) command
config <- configIO
--putStrLn ("+ Outgoing message: " <> show (message))
case (debug, message) of
(False, m) -> do
_ <- say m config
return ""
_ ->
return ""
where
debug = False
main :: IO ()
main = do
Prelude.putStrLn ("+ Listening on port " <> show port)
run port (slashSimple stockify)
where
port = 3000