Browse Source

Bot preparations

hide
Nicolas Lenz 5 months ago
parent
commit
694b2d7b51
6 changed files with 162 additions and 2 deletions
  1. 2
    2
      .gitignore
  2. 38
    0
      app/Main.hs
  3. 56
    0
      command-telegram-bot.cabal
  4. 36
    0
      package.yaml
  5. 26
    0
      src/Config.hs
  6. 4
    0
      stack.yaml

+ 2
- 2
.gitignore View File

@@ -1,5 +1,5 @@
1
-# Config path
2
-config/
1
+# Config file
2
+config.yaml
3 3
 
4 4
 # ---> Haskell
5 5
 dist

+ 38
- 0
app/Main.hs View File

@@ -0,0 +1,38 @@
1
+{-# LANGUAGE OverloadedStrings #-}
2
+
3
+module Main where
4
+
5
+import Data.Text (Text)
6
+import Data.Maybe
7
+import qualified Data.Text as T
8
+import qualified Data.Text.IO as T
9
+import Data.Char
10
+import qualified Telegram.Bot.API as Telegram
11
+import Telegram.Bot.Simple
12
+import Telegram.Bot.Simple.Debug
13
+import Telegram.Bot.Simple.UpdateParser
14
+import Control.Monad.Reader
15
+import Data.Yaml
16
+import Config
17
+
18
+-- | Bot conversation state model.
19
+data Model = Model deriving (Show)
20
+
21
+-- | Actions bot can perform.
22
+data Action
23
+    = NoAction  -- ^Perform no action.
24
+    | SendHelp  -- ^Show help text.
25
+    | Execute Command  -- ^Execute a shell command.
26
+    deriving (Show)
27
+
28
+-- | Run bot reading token from token file.
29
+main :: IO ()
30
+main = do
31
+    maybeConfig <- decodeFileEither "config.yaml"
32
+    case maybeConfig of
33
+        Right (Config token cmds) -> print cmds  -- TODO
34
+        Left exception -> putStrLn $ prettyPrintParseException exception
35
+
36
+-- |Help string.
37
+help :: T.Text
38
+help = T.unlines ["Command bot"]

+ 56
- 0
command-telegram-bot.cabal View File

@@ -0,0 +1,56 @@
1
+cabal-version: 1.12
2
+
3
+-- This file has been generated from package.yaml by hpack version 0.31.1.
4
+--
5
+-- see: https://github.com/sol/hpack
6
+--
7
+-- hash: 674a7d18465e3ac7fdd02d980d445adcdae5d2c71fde7b85b80909f8ee737e1e
8
+
9
+name:           command-telegram-bot
10
+version:        0.1.0
11
+synopsis:       Telegram bot for executing shell commands on the host
12
+description:    Please see the README at <https://git.eisfunke.com/software/command-telegram-bot#readme>
13
+category:       Web
14
+author:         Nicolas Lenz
15
+maintainer:     nicolas@eisfunke.com
16
+copyright:      2018 Nicolas Lenz
17
+license:        Apache-2.0
18
+license-file:   LICENSE
19
+build-type:     Simple
20
+extra-source-files:
21
+    README.md
22
+
23
+source-repository head
24
+  type: git
25
+  location: https://git.eisfunke.com/software/command-telegram-bot
26
+
27
+library
28
+  exposed-modules:
29
+      Config
30
+  other-modules:
31
+      Paths_command_telegram_bot
32
+  hs-source-dirs:
33
+      src
34
+  build-depends:
35
+      base >=4.7 && <5
36
+    , mtl
37
+    , telegram-bot-simple
38
+    , text
39
+    , yaml
40
+  default-language: Haskell2010
41
+
42
+executable command-telegram-bot
43
+  main-is: Main.hs
44
+  other-modules:
45
+      Paths_command_telegram_bot
46
+  hs-source-dirs:
47
+      app
48
+  ghc-options: -threaded -rtsopts -with-rtsopts=-N
49
+  build-depends:
50
+      base >=4.7 && <5
51
+    , command-telegram-bot
52
+    , mtl
53
+    , telegram-bot-simple
54
+    , text
55
+    , yaml
56
+  default-language: Haskell2010

+ 36
- 0
package.yaml View File

@@ -0,0 +1,36 @@
1
+name:                command-telegram-bot
2
+version:             0.1.0
3
+license:             Apache-2.0
4
+git:                 "https://git.eisfunke.com/software/command-telegram-bot"
5
+author:              "Nicolas Lenz"
6
+maintainer:          "nicolas@eisfunke.com"
7
+copyright:           "2018 Nicolas Lenz"
8
+
9
+extra-source-files:
10
+- README.md
11
+
12
+# Metadata used when publishing your package
13
+synopsis:            Telegram bot for executing shell commands on the host
14
+category:            Web
15
+description:         Please see the README at <https://git.eisfunke.com/software/command-telegram-bot#readme>
16
+
17
+dependencies:
18
+- base >= 4.7 && < 5
19
+- telegram-bot-simple
20
+- mtl
21
+- text
22
+- yaml
23
+
24
+library:
25
+  source-dirs: src
26
+
27
+executables:
28
+  command-telegram-bot:
29
+    main:                Main.hs
30
+    source-dirs:         app
31
+    ghc-options:
32
+    - -threaded
33
+    - -rtsopts
34
+    - -with-rtsopts=-N
35
+    dependencies:
36
+    - command-telegram-bot

+ 26
- 0
src/Config.hs View File

@@ -0,0 +1,26 @@
1
+{-# LANGUAGE OverloadedStrings #-}
2
+{-# LANGUAGE DeriveGeneric #-}
3
+
4
+module Config where
5
+
6
+import Data.Text (Text)
7
+import Data.Yaml
8
+import GHC.Generics
9
+
10
+-- |Type for a config containing a list of command specifications.
11
+data Config = Config {
12
+    token :: Text,
13
+    commands :: [Command]
14
+} deriving (Show, Generic)
15
+
16
+-- |Type for a command specification.
17
+data Command = Command {
18
+    name :: Text,  -- ^The command name used to call it in Telegram.
19
+    command :: Text,  -- ^The shell command to be executed.
20
+    authorized :: [Int]  -- ^A list of Telegram user ids specifying the users allowed to call the command.
21
+} deriving (Show, Generic)
22
+
23
+instance FromJSON Command
24
+instance ToJSON Command
25
+instance FromJSON Config
26
+instance ToJSON Config

+ 4
- 0
stack.yaml View File

@@ -0,0 +1,4 @@
1
+resolver: lts-12.19
2
+
3
+packages:
4
+- .

Loading…
Cancel
Save