Skip to content

avatar29A/hs-aitubots-api

Repository files navigation

hs-aitubots-api

Build Status Haskell Programming Language BSD3 License

High-level bindings to the Aitu Bot API based on mtl style.

Usage

We are support mtl-style way to using Aitu Bot API. Is a based on AituBotClient which is just ReaderT monad.

Documentation: https://btsdigital.github.io/bot-api-contract/

getMe

{-# LANGUAGE OverloadedStrings #-}

module Main where

import Network.HTTP.Client
import Network.HTTP.Client.TLS (tlsManagerSettings)

import qualified Data.ByteString.Lazy.Char8 as BC

import Aitu.Bot

main :: IO ()
main = do
    let token = "<YOUR_BOT_TOKEN>"

    manager <- newManager tlsManagerSettings
    response <- runAituBotClient token manager $ do 
        getMe

    case response of
        Left (code, errMsg) -> BC.putStrLn errMsg
        Right me -> print me

Upload File

{-# LANGUAGE OverloadedStrings #-}

module Main where

import Network.HTTP.Client
import Network.HTTP.Client.TLS (tlsManagerSettings)

import qualified Data.ByteString.Lazy.Char8 as BC

import Aitu.Bot

main :: IO ()
main = do
    let token = "<YOUR_BOT_TOKEN>"

    manager <- newManager tlsManagerSettings
    response <- runAituBotClient token manager $ do 
        uploadFile "cat.png" "images/cat.png"

    case response of
        Left (code, errMsg) -> BC.putStrLn errMsg
        Right me -> print me

UI

Simple Form with Button, that open url by click

{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}

module FormButton where

import           Aitu.Bot
import           Aitu.Bot.Types
import           Aitu.Bot.Forms
import           Aitu.Bot.Commands
import           Aitu.Bot.Widgets

import qualified Aitu.Bot.Forms.Content.Content
                                               as C

open :: Peer -> AituBotClient ()
open peer = do
    let
        form = mkBackdropForm Backdrop
            { formId    = "form1"
            , header    = Header { headerType = TOOLBAR
                                 , title      = "FormButton Example"
                                 , options    = defaultHeaderOptions
                                 , formAction = Nothing
                                 }
            , content   = [ C.Content $ Button
                                "Open ya.ru"
                                "button_1"
                                DefaultButton
                                (FormAction OpenUrlAction
                                            (DataTemplate "http://ya.ru")
                                )
                                Nothing
                          ]
            , options   = Just defaultOptions { fullscreen = Just True }
            , bottomBar = Nothing
            }

    sendForm peer form

see more examples here

Contribution

Contributions are welcome!

  1. Fork repository
  2. Do your changes
  3. Create pull request
  4. Wait for CI build and review

You can use stack to build library

$ stack build

also run tests

Unit Tests

$ stack test

Integration Tests

stack test --test-arguments "--integration -t TOKEN -c CHAT_ID -n BOT_USER_NAME -- HSPEC_ARGS"

or alternative

$ AITU_BOT_TOKEN=TOKEN AITU_CHAT_ID=CHAT_ID make test-integration

where

* TOKEN is a your bot's token
* CHAT_ID your user id for emulate communication between you and bot.

Roadmap

Methods

Updates

Forms