Skip to content

Commit

Permalink
feat(slack): generated internal api
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX committed Jul 13, 2023
1 parent 0935b98 commit 2077591
Show file tree
Hide file tree
Showing 31 changed files with 4,083 additions and 31 deletions.
49 changes: 26 additions & 23 deletions adapters/slack/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import { SlackMessageEncoder } from './message'
import { GenericMessageEvent, SlackChannel, SlackTeam, SlackUser } from './types'
import FormData from 'form-data'
import * as WebApi from 'seratch-slack-types/web-api'

Check failure on line 8 in adapters/slack/src/bot.ts

View workflow job for this annotation

GitHub Actions / lint

'WebApi' is defined but never used
import { Internal, Token } from './types/internal'

export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T> {
static MessageEncoder = SlackMessageEncoder
public http: Quester
public internal: Internal

constructor(ctx: Context, config: T) {
super(ctx, config)
this.http = ctx.http.extend({
headers: {
// 'Authorization': `Bearer ${config.token}`,
},
}).extend(config)
this.http = ctx.http.extend(config)

this.internal = new Internal(this, this.http)

if (config.protocol === 'ws') {
ctx.plugin(WsClient, this)
Expand All @@ -41,7 +41,7 @@ export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T
}

async getSelf() {
const data = await this.request<AuthTestResponse>('POST', '/auth.test')
const data = await this.internal.authTest(Token.BOT)
return {
userId: data.user_id,
avatar: null,
Expand All @@ -51,18 +51,20 @@ export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T
}

async deleteMessage(channelId: string, messageId: string): Promise<void> {
return this.request('POST', '/chat.delete', { channel: channelId, ts: messageId })
await this.internal.chatDelete(Token.BOT, {
channel: channelId,
ts: Number(messageId),
})
}

async getMessage(channelId: string, messageId: string): Promise<Universal.Message> {
const msg = await this.request<{
messages: GenericMessageEvent[]
}>('POST', '/conversations.history', {
const msg = await this.internal.conversationsHistory(Token.BOT, {
channel: channelId,
oldest: messageId,
oldest: Number(messageId),
limit: 1,
inclusive: true,
})
// @ts-ignore
return adaptMessage(this, msg.messages[0])
}

Expand All @@ -73,7 +75,7 @@ export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T
channel: channelId,
latest: before,
})
return msg.messages.map(v => adaptMessage(this, v))
return Promise.all(msg.messages.map(v => adaptMessage(this, v)))
}

async getUser(userId: string, guildId?: string): Promise<Universal.User> {
Expand Down Expand Up @@ -128,19 +130,18 @@ export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T

async sendPrivateMessage(channelId: string, content: Fragment, options?: SendOptions): Promise<string[]> {
// "channels:write,groups:write,mpim:write,im:write",
const { channel } = await this.request<{
channel: {
id: string
}
}>('POST', '/conversations.open', {
const { channel } = await this.internal.conversationsOpen(Token.BOT, {
users: channelId,
})
// @ts-ignore
return this.sendMessage(channel.id, content, undefined, options)
}

async getReactions(channelId: string, messageId: string, emoji: string): Promise<Universal.User[]> {
const { message } = await this.request<WebApi.ReactionsGetResponse>('POST', '/reactions.get', `channel=${channelId}&timestamp=${messageId}&emoji=${emoji}`, {
'content-type': 'application/x-www-form-urlencoded',
const { message } = await this.internal.reactionsGet(Token.BOT, {
channel: channelId,
timestamp: messageId,
full: true,
})
return message.reactions.find(v => v.name === emoji)?.users.map(v => ({
userId: v,
Expand All @@ -149,20 +150,22 @@ export class SlackBot<T extends SlackBot.Config = SlackBot.Config> extends Bot<T

async createReaction(channelId: string, messageId: string, emoji: string): Promise<void> {
// reactions.write
return this.request('POST', '/reactions.add', {
await this.internal.reactionsAdd(Token.BOT, {
channel: channelId,
timestamp: messageId,
name: emoji,
})
}

async clearReaction(channelId: string, messageId: string, emoji?: string): Promise<void> {
const { message } = await this.request<WebApi.ReactionsGetResponse>('POST', '/reactions.get', `channel=${channelId}&timestamp=${messageId}&full=true`, {
'content-type': 'application/x-www-form-urlencoded',
const { message } = await this.internal.reactionsGet(Token.BOT, {
channel: channelId,
timestamp: messageId,
full: true,
})
for (const reaction of message.reactions) {
if (!emoji || reaction.name === emoji) {
await this.request('POST', '/reactions.remove', {
await this.internal.reactionsRemove(Token.BOT, {
channel: channelId,
timestamp: messageId,
name: reaction.name,
Expand Down
3 changes: 2 additions & 1 deletion adapters/slack/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ export class SlackMessageEncoder extends MessageEncoder<SlackBot> {
results: Session[] = []
async flush() {
if (!this.buffer.length) return
const r = await this.bot.request('POST', '/chat.postMessage', {
const r = await this.bot.internal.chatPostMessage(this.bot.config.botToken, {
channel: this.channelId,
...this.addition,
thread_ts: this.thread_ts,
text: this.buffer,
})
const session = this.bot.session()
await adaptMessage(this.bot, r.message, session)

Check failure on line 38 in adapters/slack/src/message.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type 'Message' is not assignable to parameter of type 'Partial<GenericMessageEvent>'.
session.channelId = this.channelId
session.app.emit(session, 'send', session)
this.results.push(session)
this.buffer = ''
Expand Down
Loading

0 comments on commit 2077591

Please sign in to comment.