Commit 679b30ac authored by Nicolas Lenz's avatar Nicolas Lenz

Very hacky first try on inline mode

parent f56cc68a
{-# LANGUAGE OverloadedStrings #-}
module Main where
module Main where -- TODO: Refactor this horrible module
import Data.Text (Text)
import Data.Maybe
import Control.Applicative
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Data.Char
......@@ -11,8 +12,9 @@ import qualified Telegram.Bot.API as Telegram
import Telegram.Bot.Simple
import Telegram.Bot.Simple.Debug
import Telegram.Bot.Simple.UpdateParser
import Telegram.Bot.API.InlineMode
import Control.Monad.Reader
import Mock (styles)
import Mock (styles, mockRandom)
import Mock.Help (styleHelp)
......@@ -23,6 +25,7 @@ data Model = Model deriving (Show)
data Action
= NoAction -- ^ Perform no action.
| Reply Text -- ^ Reply some text.
| InlineReply (Text, Text)
| SendHelp -- ^Send help text.
deriving (Show)
......@@ -47,6 +50,15 @@ mockify = UpdateParser f where
Nothing -> return $ SendHelp
Just f -> return $ Reply $ f (T.unwords $ tail $ T.words txt)
replyToInline :: UpdateParser Action
replyToInline = UpdateParser f where
f :: Telegram.Update -> Maybe Action
f update = do
inlineQuery <- Telegram.updateInlineQuery update
let txt = Telegram.inlineQueryQuery inlineQuery
return $ InlineReply ("Random", mockRandom txt)
-- |Concatenates a list of Maybe functions. Execution goes from left to right.
-- Returns Nothing if any of the elements of the list is Nothing.
concatMaybeFunctions :: [Maybe (a -> a)] -> Maybe (a -> a)
......@@ -59,7 +71,7 @@ concatMaybeFunctions (mf:mfs) = do
-- | How to process incoming 'Telegram.Update's
-- and turn them into 'Action's.
handleUpdate :: Model -> Telegram.Update -> Maybe Action
handleUpdate _ = parseUpdate mockify
handleUpdate _ = parseUpdate $ mockify <|> replyToInline
-- | How to handle 'Action's.
handleAction :: Action -> Model -> Eff Action Model
......@@ -67,6 +79,9 @@ handleAction NoAction model = pure model
handleAction (Reply message) model = model <# do
replyText message
pure NoAction
handleAction (InlineReply (title,message)) model = model <# do
answerInlineQuery [Telegram.InlineQueryResultArticle "article" "unique id mkay" title $ Telegram.InputTextMessageContent message]
pure NoAction
handleAction SendHelp model = model <# do
reply $ (toReplyMessage help) {replyMessageParseMode = Just Telegram.Markdown, replyMessageDisableWebPagePreview = Just True}
pure NoAction
......
cabal-version: 2.2
-- This file has been generated from package.yaml by hpack version 0.31.1.
-- This file has been generated from package.yaml by hpack version 0.31.2.
--
-- see: https://github.com/sol/hpack
--
-- hash: 9c41ed5721e932d7a05a04c7d08230e9f21412f1c8ce62ba3ba6f8c98ec9d066
-- hash: 3d6ddc0a63909e16d9138d7792b39eb08b1b03951aa55c0e5af71a058d7549e4
name: mock-telegram-bot
version: 0.2.0
......@@ -32,7 +32,7 @@ executable mock-telegram-bot
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
base
, mock
, mtl
, telegram-bot-simple
......
......@@ -9,13 +9,12 @@ copyright: "2018 Nicolas Lenz"
extra-source-files:
- README.md
# Metadata used when publishing your package
synopsis: tELegrAm bOT iNteGratiOn fOR mOCK
category: Web
description: Please see the README at <https://git.eisfunke.com/software/mock-telegram-bot#readme>
dependencies:
- base >= 4.7 && < 5
- base
- telegram-bot-simple
- mock
- text
......
......@@ -7,4 +7,4 @@ extra-deps:
- git: https://git.eisfunke.com/software/mock
commit: 0ee2b62b984d44fa5199d4d94c81fb6cf271ba3d
- git: https://github.com/Eisfunke/telegram-bot-simple
commit: 38a73f94b78097a97383d1064bc1962d585b4433
commit: 8125540eeba404ef49f62df54ce838f2823516b1
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment