@@ -183,53 +183,54 @@ function parseFeishuBotRemovedChatId(value: unknown): string | null {
183183 return readString ( value . chat_id ) ?? null ;
184184}
185185
186+ function firstString ( ...values : unknown [ ] ) : string | undefined {
187+ for ( const value of values ) {
188+ const stringValue = readString ( value ) ;
189+ const trimmed = stringValue ?. trim ( ) ;
190+ if ( trimmed ) {
191+ return trimmed ;
192+ }
193+ }
194+ return undefined ;
195+ }
196+
186197function parseFeishuCardActionEventPayload ( value : unknown ) : FeishuCardActionEvent | null {
187198 if ( ! isRecord ( value ) ) {
188199 return null ;
189200 }
190- const operator = value . operator ;
201+ const operator = isRecord ( value . operator ) ? value . operator : { } ;
191202 const action = value . action ;
192- const context = value . context ;
193- if ( ! isRecord ( operator ) || ! isRecord ( action ) || ! isRecord ( context ) ) {
203+ const context = isRecord ( value . context ) ? value . context : { } ;
204+ if ( ! isRecord ( action ) ) {
194205 return null ;
195206 }
196207 const token = readString ( value . token ) ;
197- const openId = readString ( operator . open_id ) ;
198- const userId = readString ( operator . user_id ) ;
199- const unionId = readString ( operator . union_id ) ;
208+ const openId = firstString ( operator . open_id , value . open_id , context . open_id ) ;
209+ const userId = firstString ( operator . user_id , value . user_id , context . user_id ) ;
210+ const unionId = firstString ( operator . union_id ) ;
200211 const tag = readString ( action . tag ) ;
201212 const actionValue = action . value ;
202- const contextOpenId = readString ( context . open_id ) ;
203- const contextUserId = readString ( context . user_id ) ;
204- const chatId = readString ( context . chat_id ) ;
205- if (
206- ! token ||
207- ! openId ||
208- ! userId ||
209- ! unionId ||
210- ! tag ||
211- ! isRecord ( actionValue ) ||
212- ! contextOpenId ||
213- ! contextUserId ||
214- ! chatId
215- ) {
213+ const contextOpenId = firstString ( context . open_id , openId ) ;
214+ const contextUserId = firstString ( context . user_id , userId ) ;
215+ const chatId = firstString ( context . chat_id , context . open_chat_id ) ;
216+ if ( ! token || ! openId || ! tag || ! isRecord ( actionValue ) ) {
216217 return null ;
217218 }
218219 return {
219220 operator : {
220221 open_id : openId ,
221- user_id : userId ,
222- union_id : unionId ,
222+ ... ( userId ? { user_id : userId } : { } ) ,
223+ ... ( unionId ? { union_id : unionId } : { } ) ,
223224 } ,
224225 token,
225226 action : {
226227 value : actionValue ,
227228 tag,
228229 } ,
229230 context : {
230- open_id : contextOpenId ,
231- user_id : contextUserId ,
232- chat_id : chatId ,
231+ ... ( contextOpenId ? { open_id : contextOpenId } : { } ) ,
232+ ... ( contextUserId ? { user_id : contextUserId } : { } ) ,
233+ ... ( chatId ? { chat_id : chatId } : { } ) ,
233234 } ,
234235 } ;
235236}
0 commit comments