diff --git a/adapters/lark/package.json b/adapters/lark/package.json index dacbf555..e76bc8f0 100644 --- a/adapters/lark/package.json +++ b/adapters/lark/package.json @@ -1,7 +1,7 @@ { "name": "@satorijs/adapter-lark", "description": "Lark (飞书) Adapter for Satorijs", - "version": "3.9.1", + "version": "3.9.4", "type": "module", "main": "lib/index.cjs", "types": "lib/index.d.ts", diff --git a/adapters/lark/src/bot.ts b/adapters/lark/src/bot.ts index 08b22f7c..53f94c9c 100644 --- a/adapters/lark/src/bot.ts +++ b/adapters/lark/src/bot.ts @@ -74,18 +74,26 @@ export class LarkBot extends Bot } private async refreshToken() { - const { tenant_access_token: token } = await this.internal.tenantAccessTokenInternalAuth({ - app_id: this.config.appId, - app_secret: this.config.appSecret, - }) - this.logger.debug('refreshed token %s', token) - this.token = token + // https://open.feishu.cn/document/server-docs/authentication-management/access-token/tenant_access_token_internal // tenant_access_token 的最大有效期是 2 小时。 - // 剩余有效期小于 30 分钟时,调用本接口会返回一个新的 tenant_access_token,这会同时存在两个有效的 tenant_access_token。 + // 剩余有效期小于 30 分钟时,调用本接口会返回一个新的 tenant_access_token,此时会同时存在两个有效的 tenant_access_token。 // 剩余有效期大于等于 30 分钟时,调用本接口会返回原有的 tenant_access_token。 - // https://open.feishu.cn/document/server-docs/authentication-management/access-token/tenant_access_token_internal + // 初次获得 token 后的半小时内必须刷新一次,因为初次获得的 token 可能是 1.5 小时前生成的。 + let timeout = Time.minute * 20 + try { + const { tenant_access_token: token } = await this.internal.tenantAccessTokenInternalAuth({ + app_id: this.config.appId, + app_secret: this.config.appSecret, + }) + this.logger.debug('refreshed token %s', token) + this.token = token + } catch (error) { + this.logger.error('failed to refresh token, retrying in 10s') + this.logger.error(error) + timeout = Time.second * 10 + } if (this._refresher) clearTimeout(this._refresher) - this._refresher = setTimeout(() => this.refreshToken(), Time.minute * 100) + this._refresher = setTimeout(() => this.refreshToken(), timeout) this.online() }