Skip to content

Commit

Permalink
Merge pull request #63 from mgeorgebrown89/rtm
Browse files Browse the repository at this point in the history
Rtm
  • Loading branch information
mgeorgebrown89 authored Oct 29, 2019
2 parents 5f2f245 + a57ea7b commit 53b6099
Show file tree
Hide file tree
Showing 17 changed files with 482 additions and 2 deletions.
59 changes: 59 additions & 0 deletions Slack/Slack.BlockKit/Classes/Payloads/Home.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace Slack
{
namespace Payloads
{
using Slack.Composition;
using Slack.Layout;
public class Home : View
{
private Block[] _blocks;
private const int maxBlocks = 100;
private string _private_metadata;
private const int private_metadataLength = 3000;
private string _callback_id;
private const int callback_idLength = 255;
public string external_id;

public Home(Block[] blocks) : base("home")
{
this.blocks = blocks;
}

public Block[] blocks
{
get => _blocks; set
{
if (value.Length > maxBlocks)
{
throw new System.Exception($"Modals can only have up to {maxBlocks} blocks.");
}
_blocks = value;
}
}

public string private_metadata
{
get => _private_metadata; set
{
if (value.Length > private_metadataLength)
{
throw new System.Exception($"private_metadata length must be less than {private_metadataLength} characters.");
}
_private_metadata = value;
}
}

public string callback_id
{
get => _callback_id; set
{
if (value.Length > callback_idLength)
{
throw new System.Exception($"callback_id length must be less than {callback_idLength} characters.");
}
_callback_id = value;
}
}
}
}
}
2 changes: 1 addition & 1 deletion Slack/Slack.BlockKit/Classes/Payloads/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Payloads
public abstract class View
{
public string type;
private readonly string[] viewTypes = { "modal" };
private readonly string[] viewTypes = { "modal", "home" };

public View(string type)
{
Expand Down
18 changes: 17 additions & 1 deletion Slack/Slack.WebAPI/Private/Invoke-SlackWebAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function Invoke-SlackWebAPI {
$Token,

[string]
[ValidateSet('GET', 'POST')]
$REST_Method = 'POST',

[string]
Expand All @@ -26,5 +27,20 @@ function Invoke-SlackWebAPI {
Authorization = "Bearer $token"
}

Invoke-RestMethod -Method $REST_Method -Uri $Uri -Headers $Headers -ContentType $ContentType -Body ($Body | ConvertTo-NoNullsJson -Depth 100)
if ($ContentType -eq 'application/json;charset=iso-8859-1' ) {
if ($Body) {
Invoke-RestMethod -Method $REST_Method -Uri $Uri -Headers $Headers -ContentType $ContentType -Body ($Body | ConvertTo-NoNullsJson -Depth 100)
}
else {
Invoke-RestMethod -Method $REST_Method -Uri $Uri -Headers $Headers -ContentType $ContentType
}
}
elseif ($ContentType -eq 'application/x-www-form-urlencoded' ) {
if ($Body) {
Invoke-RestMethod -Method $REST_Method -Uri $Uri -Headers $Headers -ContentType $ContentType -Body $Body
}
else {
Invoke-RestMethod -Method $REST_Method -Uri $Uri -Headers $Headers -ContentType $ContentType
}
}
}
36 changes: 36 additions & 0 deletions Slack/Slack.WebAPI/Public/rtm/Connect-SlackRtmSession.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function Connect-SlackRtmSession {
<#
.SYNOPSIS
Starts a Real Time Messaging Session.
.DESCRIPTION
This function beings a Real Time Messaging Session with Slack
and reserves your application a specific URL with which to connect via websocket.
Unlike rtm.start, this method is focused only on connecting to the RTM API.
.PARAMETER token
A Slack User or Bot token.
.PARAMETER batch_presence_aware
Batch presence deliveries via subscription. Enabling changes the shape of presence_change events.
.PARAMETER presence_sub
Only deliver presence events when requested by subscription.
.LINK
https://api.slack.com/methods/rtm.connect
.EXAMPLE
Connect-SlackRtmSession -token $Token
.EXAMPLE
rtm.connect -token $Token
#>
[CmdletBinding()]
param(
[string]
$token,

[bool]
$batch_presence_aware,

[bool]
$presence_sub
)

Invoke-SlackWebApi -Token $token -REST_Method 'GET' -Method_Family 'rtm.connect'
}
Set-Alias -Name 'rtm.connect' -Value 'Connect-SlackRtmSession'
64 changes: 64 additions & 0 deletions Slack/Slack.WebAPI/Public/rtm/Start-SlackRtmSession.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
function Start-SlackRtmSession {
<#
.SYNOPSIS
Starts a Real Time Messaging session.
.DESCRIPTION
This function begins a Real Time Messaging API session
and reserves your application a specific URL with which to connect via websocket.
It's user-centric and team-centric: your app connects as a specific user or bot user on a specific team.
This method also returns a smorgasbord of data about the team, its channels, and members.
Some times more information than can be provided in a timely or helpful manner.
.PARAMETER token
Authentication token bearing required scopes.
.PARAMETER batch_presence_aware
Batch presence deliveries via subscription. Enabling changes the shape of presence_change events.
.PARAMETER include_locale
Set this to true to receive the locale for users and channels. Defaults to false
.PARAMETER mpim_aware
Returns MPIMs to the client in the API response.
.PARAMETER no_latest
Exclude latest timestamps for channels, groups, mpims, and ims. Automatically sets no_unreads to 1
.PARAMETER no_unreads
Skip unread counts for each channel (improves performance).
.PARAMETER presence_sub
Only deliver presence events when requested by subscription.
.PARAMETER simple_latest
Return timestamp only for latest message object of each channel (improves performance).
.Link
https://api.slack.com/methods/rtm.start
.EXAMPLE
Start-SlackRtmSession -token $token -no_unreads $true
.EXAMPLE
rtm.start -token $token -include_locale $true
#>
[CmdletBinding()]
param(
[string]
[Parameter(Mandatory = $true)]
$token,

[switch]
$batch_presence_aware,

[switch]
$include_locale,

[switch]
$mpim_aware,

[switch]
$no_latest,

[bool]
$no_unreads,

[bool]
$presence_sub,

[bool]
$simple_latest
)

Invoke-SlackWebApi -Token $token -REST_Method 'GET' -Method_Family 'rtm.start'
}
Set-Alias -Name 'rtm.start' -Value 'Start-SlackRtmSession'
45 changes: 45 additions & 0 deletions Slack/Slack.WebAPI/Public/users/Get-SlackUsers.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function Get-SlackUsers {
<#
.SYNOPSIS
Lists all users in a Slack team.
.DESCRIPTION
This function returns a list of all users in the Slack workspace. This includes deleted/deactivated users.
.PARAMETER token
Authentication token bearing required scopes.
.PARAMETER cursor
Paginate through collections of data by setting the cursor parameter to a next_cursor attribute
returned by a previous request's response_metadata.
Default value fetches the first "page" of the collection.
.PARAMETER include_locale
Set this to true to receive the locale for users. Defaults to false
.PARAMETER limit
The maximum number of items to return. Fewer than the requested number of items may be returned,
even if the end of the users list hasn't been reached.
.LINK
https://api.slack.com/methods/users.list
.EXAMPLE
Get-SlackUsers -token $token
.EXAMPLE
users.list -token $token -limit 20
#>
[CmdletBinding()]
param(
[string]
[Parameter(Mandatory = $true)]
$Token,

[string]
$cursor,

[bool]
$include_locale = $false,

[int]
$limit = 0
)

$Body = "cursor=$cursor&include_local=$include_local&limit=$limit"

Invoke-SlackWebAPI -Token $Token -Method_Family "users.list" -REST_Method "POST" -Body $Body -ContentType "application/x-www-form-urlencoded"
}
Set-Alias -Name 'users.list' -Value 'Get-SlackUsers'
39 changes: 39 additions & 0 deletions Slack/Slack.WebAPI/Public/views/Open-SlackView.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function Open-SlackView {
<#
.SYNOPSIS
Open a view for a user.
.DESCRIPTION
This function opens a modal with a user by exchanging a trigger_id received from another interaction.
.PARAMETER Token
Authentication token bearing required scopes.
.PARAMETER trigger_id
Exchange a trigger to post to the user.
.PARAMETER view
The view payload. Must be of type [Slack.Payloads.View]
.LINK
https://api.slack.com/methods/views.open
.EXAMPLE
Open-SlackView -token $token -tiggerId '12345.98765.abcd2358fdea' -view $view
.EXAMPLE
views.open -token $token -tiggerId '12345.98765.abcd2358fdea' -view $view
#>
[CmdletBinding()]
param(
[string]
$Token,

[string]
$trigger_id,

[Slack.Payloads.View]
$view
)

$Body = @{
trigger_id = $trigger_id
view = $view
}

Invoke-SlackWebAPI -Token $Token -Method_Family "views.open" -Body $Body
}
Set-Alias -Name 'views.open' -Value 'Open-SlackView'
45 changes: 45 additions & 0 deletions Slack/Slack.WebAPI/Public/views/Publish-SlackView.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function Publish-SlackView {
<#
.SYNOPSIS
Publish a static view for a User.
.DESCRIPTION
This function creates or updates the view that comprises an app's Home tab for a specific user.
.PARAMETER Token
Authentication token bearing required scopes.
.PARAMETER user_id
id of the user you want publish a view to.
.PARAMETER view
The view payload. Must be of type [Slack.Payloads.View]
.PARAMETER hash
A string that represents view state to protect against possible race conditions.
.LINK
https://api.slack.com/methods/views.publish
.EXAMPLE
Publish-SlackView -token $token -view $view
.EXAMPLE
views.publish -token $token -view $view
#>
[CmdletBinding()]
param(
[string]
$Token,

[string]
$user_id,

[Slack.Payloads.View]
$view,

[string]
$hash
)

$Body = [PSCustomObject]@{
user_id = $user_id
view = $view
hash = $hash
}

Invoke-SlackWebAPI -Token $Token -Method_Family "views.publish" -Body $Body
}
Set-Alias -Name 'views.publish' -Value 'Publish-SlackView'
43 changes: 43 additions & 0 deletions Slack/Slack.WebAPI/Public/views/Push-SlackView.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function Push-SlackView {
<#
.SYNOPSIS
Push a view onto the stack of a root view.
.DESCRIPTION
This function pushes a new view onto the existing view stack
by passing a view payload and a valid trigger_id generated from an interaction within the existing modal.
The pushed view is added to the top of the stack,
so the user will go back to the previous view after they complete or cancel the pushed view.
After a modal is opened, the app is limited to pushing 2 additional views.
.PARAMETER Token
Authentication token bearing required scopes.
.PARAMETER trigger_id
Exchange a trigger to post to the user.
.PARAMETER view
The view payload. Must be of type [Slack.Payloads.View]
.LINK
https://api.slack.com/methods/views.push
.EXAMPLE
Push-SlackView -token $token -tiggerId '12345.98765.abcd2358fdea' -view $view
.EXAMPLE
views.push -token $token -tiggerId '12345.98765.abcd2358fdea' -view $view
#>
[CmdletBinding()]
param(
[string]
$Token,

[string]
$trigger_id,

[Slack.Payloads.View]
$view
)

$Body = @{
trigger_id = $trigger_id
view = $view
}

Invoke-SlackWebAPI -Token $Token -Method_Family "views.push" -Body $Body
}
Set-Alias -Name 'views.push' -Value 'Push-SlackView'
Loading

0 comments on commit 53b6099

Please sign in to comment.