Skip to content

Commit

Permalink
feat: support feishu change avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
Leizhenpeng committed Jan 15, 2024
1 parent 16f4a70 commit 8dd7e06
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 130 deletions.
38 changes: 38 additions & 0 deletions botops.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "dsadsad",
"desc": "onebot test demo",
"avatar": "https://avatars.githubusercontent.com/u/145313435?s=200&v=4",
"platform": "feishu",
"feishuConfig": {
"appId": "cli_a52fa4f61af4100d",
"events": [
"im.message.message_read_v1",
"im.message.receive_v1"
],
"encryptKey": "e-fJKrqNbSz9NqSWL5",
"verificationToken": "v-Ohw8k6KwVynNmzXX",
"scopeIds": [
"21001",
"7",
"21003",
"21002",
"20001",
"20011",
"3001",
"20012",
"6005",
"20010",
"3000",
"20013",
"20014",
"20015",
"20008",
"1000",
"1006",
"1005",
"20009"
],
"cardRequestUrl": "https://connect-ai-e.com/feishu/64af64fab84e8e000162ef66/card",
"verificationUrl": "https://connect-ai-e.com/feishu/64af64fab84e8e000162ef66/event"
}
}
2 changes: 2 additions & 0 deletions packages/feishuapi/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ export class OpenApp {
}

async changeAppInfo(appId: string, appInfo: AppInfo) {
if (appInfo.avatar)
appInfo.avatar = await this.uploadAppAvatar(appInfo.avatar)
const formatBody = await this.formatNewAppBody(appInfo)
const result = await this.cfg.aPostRequest(`developers/v1/base_info/${appId}`, formatBody)
return result.data
Expand Down
8 changes: 5 additions & 3 deletions packages/feishuapi/test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type { AppInfo, EventUrlInfo } from '../src/app'
import { FeishuAppPlus, OpenApp } from '../src/app'

const testConfig: FeishuLoginCookies = {
lark_oapi_csrf_token: '6YRvL8A3JuqniYsZyAf6c//eMBfh3jaDMF6Vc1Sr870=',
session: 'XN0YXJ0-39ej5d23-864f-4461-aad1-b0eacaf2cc99-WVuZA',
lark_oapi_csrf_token: 'mMH47ijv/C0IKvUblsY3cA9QVjskDfEvCmYyXoaqTIU=',
session: 'XN0YXJ0-e29q5c84-aef0-44db-9b99-24282033dd87-WVuZA',
}

const config = new Configuration(testConfig)
Expand Down Expand Up @@ -50,12 +50,14 @@ describe('app', async () => {
expect(result).not.toBeUndefined()
})
it('should change app info', async () => {
const result = await app.changeAppInfo('cli_a42ef85d81bad00e', {
const result = await app.changeAppInfo('cli_a52fa4f61af4100d', {
name: 'a botName',
avatar: 'https://s1-imfile.feishucdn.com/static-resource/v1/v2_2514eb9a-de2f-41cd-89b4-274940456f3g',
})
console.log(JSON.stringify(result))
expect(result).not.toBeUndefined()
})

it('should delete app', async () => {
const result = await app.deleteApp('cli_a5281a3b6838500b')
console.log(JSON.stringify(result))
Expand Down
43 changes: 0 additions & 43 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

168 changes: 87 additions & 81 deletions src/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// commands/auth.ts
import type { Argv } from 'yargs'
import clipboard from 'clipboardy'
import confirm from '@inquirer/confirm'
import Listr from 'listr'
import { greenIt, redIt } from './utils'
import ora from 'ora'
import { changeArgvToString, greenIt } from './utils'
import { DeployConfig } from './manifest'
import { FeishuConfigManager } from './config'

Expand All @@ -24,92 +24,98 @@ export function builder(yargs: Argv) {
})
}

const DEFAULT_MANIFEST_FILE = 'botops.json'

export async function handler(argv: any) {
// 如果没有接收到参数,默认读取同级目录下的 botops.json 文件
let pathFile = ''
const spin = ora('Loading manifest file...').start()
if (argv._.length === 1) {
const answer = await confirm({
message: 'read the deployment configuration file from the clipboard ?',
default: false,
})
if (answer) {
const text = readClipboard()
console.log('manifest from clipbord :', text)
return
}
spin.info('Not specify manifest file, will load botops.json in current directory')
spin.info(`Loading default manifest from botops.json in ${process.cwd()}`)
pathFile = DEFAULT_MANIFEST_FILE
}
// 如果接收到参数,就读取参数
if (argv._.length > 1) {
const pathFile = argv._[1]
const aDeployConfig = new DeployConfig()
if (!await aDeployConfig.validateConfigByPath(pathFile))
redIt('the config file is not valid')
await aDeployConfig.loadConfig(pathFile)
const aLocalConfig = FeishuConfigManager.getInstance()
const appBuilder = aLocalConfig.appBuilder
await appBuilder.init()

if (aDeployConfig.ifFirstDeploy) {
const appId = await appBuilder.newApp(aDeployConfig.botBaseInfo)

const tasks = new Listr([
{
title: '初始化机器人',
task: async (ctx, task) => {
const appId = await appBuilder.newApp(aDeployConfig.botBaseInfo)
await appBuilder.versionManager.clearUnPublishedVersion(appId)

ctx.appId = appId
},
},
pathFile = changeArgvToString(argv._[1])
spin.info(`Loading manifest from ${pathFile}`)
}

{
title: '添加事件权限',
task: async (ctx, task) => {
const appId = ctx.appId
await appBuilder.eventManager.addEvent(appId, aDeployConfig.events)
},
},
{
title: '添加事件回调',
task: async (ctx, task) => {
const appId = ctx.appId
await appBuilder.eventManager.addEventCallBack(appId, aDeployConfig.eventCallbackUrl)
},
},
{
title: '启用机器人',
task: async (ctx) => {
const appId = ctx.appId
await appBuilder.enableBot(appId)
},
},
{
title: '添加权限范围',
task: async (ctx) => {
const appId = ctx.appId
await appBuilder.addScope(appId, aDeployConfig.scopeIds)
},
},
{
title: '添加机器人回调',
task: async (ctx) => {
const appId = ctx.appId
await appBuilder.botManager.addBotCallBack(appId, aDeployConfig.cardRequestUrl)
},
},
{
title: '创建并发布下一个版本',
task: async (ctx) => {
const appId = ctx.appId
await appBuilder.versionManager.createAndPublishNextVersion(appId)
},
},
])
const aDeployConfig = new DeployConfig()
if (!await aDeployConfig.validateConfigByPath(pathFile)) {
spin.fail('Manifest file of deploy is not valid.')
spin.clear()
return
}
await aDeployConfig.loadConfig(pathFile)
spin.succeed('Manifest file loaded successfully.')
const aLocalConfig = FeishuConfigManager.getInstance()
const appBuilder = aLocalConfig.appBuilder
await appBuilder.init()

await tasks.run()
greenIt((`机器人创建成功,appId: ${appId}`))
}
let appId = ''
if (aDeployConfig.ifFirstDeploy) {
appId = await appBuilder.newApp(aDeployConfig.botBaseInfo)
greenIt(`飞书机器人 ${aDeployConfig.botName}(${appId}) 初始化成功`)
}
else {
appId = aDeployConfig.appId as string
await appBuilder.versionManager.clearUnPublishedVersion(appId)
await appBuilder.changeAppInfo(appId, aDeployConfig.botBaseInfo)
greenIt(`即将为飞书机器人 ${aDeployConfig.botName}(${appId}) 部署新版本`)
}
const tasks = new Listr([
{
title: '操作前检查',
task: async (ctx, task) => {
await appBuilder.versionManager.clearUnPublishedVersion(appId)
ctx.appId = appId
},
},

{
title: '添加事件权限',
task: async (ctx, task) => {
const appId = ctx.appId
await appBuilder.eventManager.addEvent(appId, aDeployConfig.events)
},
},
{
title: '添加事件回调',
task: async (ctx, task) => {
const appId = ctx.appId
await appBuilder.eventManager.addEventCallBack(appId, aDeployConfig.eventCallbackUrl)
},
},
{
title: '启用机器人',
task: async (ctx) => {
const appId = ctx.appId
await appBuilder.enableBot(appId)
},
},
{
title: '添加权限范围',
task: async (ctx) => {
const appId = ctx.appId
await appBuilder.addScope(appId, aDeployConfig.scopeIds)
},
},
{
title: '添加机器人回调',
task: async (ctx) => {
const appId = ctx.appId
await appBuilder.botManager.addBotCallBack(appId, aDeployConfig.cardRequestUrl)
},
},
{
title: '创建并发布下一个版本',
task: async (ctx) => {
const appId = ctx.appId
await appBuilder.versionManager.createAndPublishNextVersion(appId)
},
},
])
await tasks.run()
greenIt((`🚀 机器人 ${aDeployConfig.botName}(${appId}) 部署成功`))
}

function readClipboard() {
Expand Down
12 changes: 9 additions & 3 deletions src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class DeployConfig {
required: ['events', 'encryptKey', 'verificationToken', 'scopeIds', 'cardRequestUrl', 'verificationUrl'],
},
},
required: ['name', 'desc', 'avatar', 'platform', 'feishuConfig'],
required: ['name', 'platform'],
}
}

Expand Down Expand Up @@ -116,7 +116,7 @@ export class DeployConfig {

async loadLocalFile(path: string): Promise<string> {
if (!await this.isFileExist(path))
throw new Error('the file is not exist')
return ''

return fs.readFile(path, 'utf-8')
.catch((error) => {
Expand Down Expand Up @@ -145,6 +145,8 @@ export class DeployConfig {
return false

const config = await this.loadFileByPath(path)
if (!config)
return false
return this.validateConfig(JSON.parse(config))
}

Expand Down Expand Up @@ -185,7 +187,7 @@ export class DeployConfig {
}

get ifFirstDeploy() {
return this.config.feishuConfig.appId === undefined
return !this.appId
}

get events() {
Expand All @@ -206,4 +208,8 @@ export class DeployConfig {
get cardRequestUrl() {
return this.config.feishuConfig.cardRequestUrl
}

get appId() {
return this.config.feishuConfig.appId
}
}
Loading

0 comments on commit 8dd7e06

Please sign in to comment.