package slack

  1. Overview
  2. Docs

Source file api_local.ml

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
103
104
105
106
107
108
109
110
111
112
(** Module for mocking test requests to slack--will output on Stdio *)

open Common
open Printf

let cwd = Sys.getcwd ()
let cache_dir = Filename.concat cwd "slack-api-cache"

(** return the file with a function f applied unless the file is empty;
 empty file:this is needed to simulate 404 returns from github *)
let with_cache_file url f =
  match get_local_file url with
  | "" -> Lwt.return_error (`Other "empty file")
  | file -> Lwt.return_ok (f file)
  | exception Slack_lib_error e -> Lwt.return_error (`Other e)

let default_post_message_res : Slack_t.post_message_res = { channel = "SOME_RETURN_POST_CHANNEL_ID"; ts = "SOME_TS" }

let default_update_message_res : Slack_t.update_message_res =
  { channel = "SOME_RETURN_UPDATE_CHANNEL_ID"; ts = "SOME_TS" }

let default_files_res : Slack_t.file =
  {
    id = "SOME_FILE_ID";
    name = None;
    title = None;
    mimetype = None;
    filetype = None;
    pretty_type = None;
    user = None;
    size = None;
    channels = [];
    ims = [];
    groups = [];
    permalink = None;
    permalink_public = None;
  }

let default_files_upload_res : Slack_t.files_upload_res = { ok = true; file = default_files_res }

let send_message ~ctx:_ ~msg =
  let json = msg |> Slack_j.string_of_post_message_req |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
  printf "will notify #%s\n" msg.channel;
  printf "%s\n" json;
  Lwt.return_ok { default_post_message_res with channel = msg.channel }

let send_message_webhook ~ctx:_ ~url ~msg =
  let json = msg |> Slack_j.string_of_post_message_req |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
  printf "will notify #%s\n" url;
  printf "%s\n" json;
  Lwt.return_ok ()

let update_message ~ctx:_ ~msg =
  let json = msg |> Slack_j.string_of_update_message_req |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
  printf "will update #%s at %s \n" msg.channel msg.ts;
  printf "%s\n" json;
  Lwt.return_ok { default_update_message_res with channel = msg.channel }

let upload_file ~ctx:_ ~file =
  let json = file |> Slack_j.string_of_files_upload_req |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
  match file.channels with
  | Some channels ->
    printf "will update #%s\n" channels;
    printf "%s\n" json;
    Lwt.return_ok { default_files_upload_res with file = { default_files_res with channels = [ channels ] } }
  | None -> Lwt.return_error (`Other "invalid file upload")

let join_conversation ~ctx:_ ~(channel : Slack_t.conversations_join_req) =
  printf "joining #%s...\n" channel.channel;
  let url = Filename.concat cache_dir (sprintf "%s_join" channel.channel) in
  with_cache_file url Slack_j.conversations_join_res_of_string

let send_chat_unfurl ~ctx:_ ~(req : Slack_t.chat_unfurl_req) =
  let data = req |> Slack_j.string_of_chat_unfurl_req |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
  printf "will unfurl in #%s\n" req.channel;
  printf "%s\n" data;
  Lwt.return_ok ()

let update_usergroup_users ~ctx:_ ~(usergroup : Slack_t.update_usergroups_users_req) =
  printf "updating #%s...\n" usergroup.usergroup;
  let url = Filename.concat cache_dir (sprintf "%s_usergroup_users" usergroup.usergroup) in
  with_cache_file url Slack_j.update_usergroups_users_res_of_string

let get_replies ~ctx:_ ~(conversation : Slack_t.conversations_replies_req) =
  let url = Filename.concat cache_dir (sprintf "%s_%s_replies" conversation.channel conversation.ts) in
  with_cache_file url Slack_j.conversations_replies_res_of_string

let get_conversations_info ~ctx:_ ~(conversation : Slack_t.conversations_info_req) =
  let url = Filename.concat cache_dir conversation.channel in
  with_cache_file url Slack_j.conversations_info_res_of_string

let get_user ~ctx:_ ~(user : Slack_t.user_info_req) =
  let url = Filename.concat cache_dir user.user in
  with_cache_file url Slack_j.user_info_res_of_string

let list_usergroups ~ctx:_ ~(req : Slack_t.list_usergroups_req) =
  let url = Filename.concat cache_dir (sprintf "%s_usergroups_list" @@ Option.get req.team_id) in
  with_cache_file url Slack_j.list_usergroups_res_of_string

let list_usergroup_users ~ctx:_ ~(usergroup : Slack_t.list_usergroup_users_req) =
  printf "listing #%s...\n" usergroup.usergroup;
  let url = Filename.concat cache_dir (sprintf "%s_list_usergroup_users" usergroup.usergroup) in
  with_cache_file url Slack_j.list_usergroup_users_res_of_string

let list_users ~ctx:_ ~(req : Slack_t.list_users_req) =
  printf "listing at cursor #%s...\n" @@ Option.get req.cursor;
  let url = Filename.concat cache_dir (sprintf "%s_list_users" @@ Option.get req.cursor) in
  with_cache_file url Slack_j.list_users_res_of_string

let send_auth_test ~ctx:_ () =
  Lwt.return
  @@ Ok ({ url = ""; team = ""; user = ""; team_id = ""; user_id = "test_slack_user" } : Slack_t.auth_test_res)
OCaml

Innovation. Community. Security.