Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support slack block kits and view events
- Loading branch information
Showing
3 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import sleep from 'delay'; | ||
import warning from 'warning'; | ||
import { SlackOAuthClient, SlackTypes } from 'messaging-api-slack'; | ||
|
||
import Context from './Context'; | ||
import SlackEvent from './SlackEvent'; | ||
import { PlatformContext } from './PlatformContext'; | ||
|
||
export default class SlackContext extends Context<SlackOAuthClient, SlackEvent> | ||
implements PlatformContext { | ||
/** | ||
* The name of the platform. | ||
* | ||
*/ | ||
get platform(): string { | ||
return 'slack'; | ||
} | ||
|
||
/** | ||
* Delay and show indicators for milliseconds. | ||
* | ||
*/ | ||
async typing(milliseconds: number): Promise<void> { | ||
if (milliseconds > 0) { | ||
await sleep(milliseconds); | ||
} | ||
} | ||
|
||
/** | ||
* Sends a message to the channel of the session. | ||
* | ||
* https://api.slack.com/methods/chat.postMessage | ||
*/ | ||
postMessage( | ||
message: | ||
| { | ||
text?: string; | ||
attachments?: SlackTypes.Attachment[] | string; | ||
blocks?: SlackTypes.Block[] | string; | ||
} | ||
| string, | ||
options?: {} | ||
): Promise<any> { | ||
const channelId = this._getChannelIdFromSession(); | ||
|
||
if (!channelId) { | ||
warning( | ||
false, | ||
'postMessage: should not be called in context without session' | ||
); | ||
return Promise.resolve(); | ||
} | ||
|
||
this._isHandled = true; | ||
|
||
return this._client.postMessage(channelId, message, { | ||
threadTs: this._event.rawEvent.thread_ts, | ||
...options, | ||
}); | ||
} | ||
|
||
/** | ||
* Sends an ephemeral message to the session owner. | ||
* | ||
* https://api.slack.com/methods/chat.postMessage | ||
*/ | ||
postEphemeral( | ||
message: | ||
| { | ||
text?: string; | ||
attachments?: SlackTypes.Attachment[] | string; | ||
blocks?: SlackTypes.Block[] | string; | ||
} | ||
| string, | ||
options?: {} | ||
): Promise<any> { | ||
const channelId = this._getChannelIdFromSession(); | ||
|
||
if (!channelId) { | ||
warning( | ||
false, | ||
'postMessage: should not be called in context without session' | ||
); | ||
return Promise.resolve(); | ||
} | ||
|
||
this._isHandled = true; | ||
|
||
return this._client.postEphemeral( | ||
channelId, | ||
(this._session as any).user.id, | ||
message, | ||
options | ||
); | ||
} | ||
|
||
/** | ||
* Open a view (modal) and use private_metadata to keep channelId for session key | ||
* | ||
* https://api.slack.com/methods/view.open | ||
*/ | ||
openView(view: {}, triggerId: string): Promise<any> { | ||
return this._client.callMethod('views.open', { | ||
view: JSON.stringify({ | ||
...view, | ||
private_metadata: JSON.stringify({ | ||
...(view.private_metadata || {}), | ||
channelId: this.event.rawEvent.channel.id, | ||
}), | ||
}), | ||
trigger_id: triggerId, | ||
}); | ||
} | ||
|
||
/** | ||
* Send text to the owner of the session. | ||
* | ||
*/ | ||
sendText(text: string, options?: {}): Promise<any> { | ||
return this.postMessage(text, options); | ||
} | ||
|
||
// FIXME: this is to fix type checking | ||
_getChannelIdFromSession(): string | null { | ||
if (!this._session) return null; | ||
if ( | ||
typeof this._session.channel === 'object' && | ||
this._session.channel && | ||
this._session.channel.id && | ||
typeof this._session.channel.id === 'string' | ||
) { | ||
return this._session.channel.id; | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters