aboutsummaryrefslogtreecommitdiff
path: root/app/Main.hs
blob: a427719216435b0eb427e6fc9eebfbdf12b0af6f (plain)
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
{-# LANGUAGE OverloadedStrings #-}
module Main where


import Debug.Trace
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Maybe
import Data.Char (isSpace)


import Telegram.Bot.API
import Telegram.Bot.Simple
import Telegram.Bot.API.InlineMode.InlineQueryResult
import Telegram.Bot.API.InlineMode.InputMessageContent (defaultInputTextMessageContent)

import Shupai (shupai)

type Model = ()

data Action
  = InlineEcho InlineQueryId Text

echoBot :: BotApp Model Action
echoBot = BotApp
  { botInitialModel = ()
  , botAction = updateToAction
  , botHandler = handleAction
  , botJobs = []
  }

updateToAction :: Update -> Model -> Maybe Action
updateToAction update _
  | isJust $ updateInlineQuery update =  do
      query <- updateInlineQuery update
      let queryId = inlineQueryId query
      let msg =  inlineQueryQuery query
      if Text.length msg <= 140 || Text.length msg > 0 then
        seq msg $ Just $ InlineEcho queryId msg
      else Nothing
  | otherwise = Nothing

handleAction :: Action -> Model -> Eff Action Model
handleAction action model = case action of
  InlineEcho queryId msg -> model <# do
    let result = (defInlineQueryResultGeneric (InlineQueryResultId "000"))
          { inlineQueryResultTitle = Just "发送竖排结果"
          , inlineQueryResultInputMessageContent = let
              shupaiText = Text.pack . shupai . Text.unpack $ msg
              in 
               Just (defaultInputTextMessageContent shupaiText)
          }
        thumbnail = defInlineQueryResultGenericThumbnail result
        article = defInlineQueryResultArticle thumbnail
        answerInlineQueryRequest = defAnswerInlineQuery queryId [article]
    _ <- runTG  answerInlineQueryRequest
    return ()

run :: Token -> IO ()
run token = do
  env <- defaultTelegramClientEnv token
  startBot_ echoBot env

main :: IO ()
main = do
  content <- readFile "tgtoken"
  let token = Token $ Text.pack $ strip content
  run token

strip :: String -> String
strip = reverse . dropWhile isSpace . reverse . dropWhile isSpace