Skip to content

Commit

Permalink
feat(zulip): add bot api
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX committed Aug 20, 2023
1 parent a41603f commit e76f85f
Show file tree
Hide file tree
Showing 4 changed files with 6,130 additions and 1,634 deletions.
56 changes: 43 additions & 13 deletions adapters/zulip/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Internal } from './types'
import { ZulipMessageEncoder } from './message'
// @ts-ignore
import { version } from '../package.json'
import { decodeGuild } from './utils'
import { decodeGuild, decodeMember, decodeMessage } from './utils'

export class ZulipBot extends Bot<ZulipBot.Config> {
static MessageEncoder = ZulipMessageEncoder
Expand All @@ -29,18 +29,10 @@ export class ZulipBot extends Bot<ZulipBot.Config> {
}

async initliaze() {
const { avatar_url, user_id, full_name } = await this.internal.getOwnUser()
this.selfId = user_id.toString()
this.username = full_name
this.avatar = avatar_url
}

async getUser(userId: string, guildId?: string) {
const { user } = await this.internal.getUser(userId)
return {
userId,
username: user?.full_name,
}
const { avatar, userId, username } = await this.getSelf()
this.selfId = userId
this.username = username
this.avatar = avatar
}

async getGuildList() {
Expand All @@ -57,6 +49,44 @@ export class ZulipBot extends Bot<ZulipBot.Config> {
const { topics } = await this.internal.getStreamTopics(guildId)
return topics.map(({ name }) => ({ channelId: name }))
}

async getGuildMember(guildId: string, userId: string) {
const { user } = await this.internal.getUser(userId)
return decodeMember(user)
}

getUser(userId: string, guildId?: string) {
return this.getGuildMember(guildId, userId)
}

async getGuildMemberList(guildId: string) {
const { members } = await this.internal.getUsers()
return members.map(decodeMember)
}

async getMessage(channelId: string, messageId: string) {
const { message } = await this.internal.getMessage(messageId)
const msg = await decodeMessage(this, message)
return msg
}

async getSelf() {
const self = await this.internal.getOwnUser()
return decodeMember(self)
}

async getMessageList(channelId: string, before?: string) {
const { messages } = await this.internal.getMessages({
num_before: 50,
num_after: 0,
narrow: JSON.stringify([
{ operator: 'topic', operand: channelId },
]),
anchor: before ?? 'newest',
apply_markdown: false,
})
return await Promise.all(messages.map(data => decodeMessage(this, data)))
}
}

export namespace ZulipBot {
Expand Down
15 changes: 11 additions & 4 deletions adapters/zulip/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class ZulipMessageEncoder extends MessageEncoder<ZulipBot> {
form.append('file', value, attrs.file || filename)
const response = await this.bot.http.post<{
uri: string
}>('/api/v1/user_uploads', form, {
}>('/user_uploads', form, {
headers: form.getHeaders(),
})
return [response.uri, filename]
Expand Down Expand Up @@ -84,9 +84,16 @@ export class ZulipMessageEncoder extends MessageEncoder<ZulipBot> {
} else if (type === 'sharp') {
// @TODO
// this.buffer += `#**${attrs.name}** `
} else if (type === 'at') {
const u = await this.getUser(attrs.id)
if (u) this.buffer += `@**${u}|${attrs.id}** `
} else if (type === 'at' && attrs.id) {
try {
const u = await this.getUser(attrs.id)
if (u) this.buffer += ` @**${u}|${attrs.id}** `
} catch (e) {
this.bot.logger.error(e)
this.buffer += ` @**${attrs.id}** `
}
} else if (type === 'at' && ['all', 'here'].includes(attrs.type)) {
this.buffer += ` @**all** `
} else if (type === 'message') {
await this.render(children)
}
Expand Down
Loading

0 comments on commit e76f85f

Please sign in to comment.