fromfastapiimportFastAPI,Form,UploadFile,Fileimportuvicornapp=FastAPI()@app.post("/receive_msg")asyncdefreceive_msg(type:str=Form(...),content:str=Form(...),source:str=Form(None),is_mentioned:str=Form(None),is_msg_from_self:str=Form(None),file:UploadFile=File(None)):iftypeandcontent:# 根据消息类型执行相应操作iftype=='text':# 处理文本消息print('Received text message:',content)eliftype=='file':# 处理文件消息iffile:# 处理文件上传逻辑print('Received file:',file.filename)eliftype=='urlLink':# 处理链接卡片消息print('Received URL link message:',content)# 其他消息类型的处理逻辑# 其他字段的处理ifsource:print('Source data:',source)ifis_mentioned:print('Is mentioned:',is_mentioned)ifis_msg_from_self:print('Is message from self:',is_msg_from_self)return{'message':'Message received successfully'}return{'error':'Invalid message format'}if__name__=='__main__':uvicorn.run(app,host="0.0.0.0",port=8000)
但是再看日志就出现报错:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[2024-03-29T12:26:02.984][INFO] - starting fetching api: http://localhost:8000/receive_msg
[2024-03-29T12:26:03.122][ERROR] - Error occurred when trying to send Data to RecvdApi FetchError: request to http://localhost:8000/receive_msg failed, reason: connect ECONNREFUSED ::1:8000
at ClientRequest.<anonymous> (/app/node_modules/.pnpm/node-fetch-commonjs@3.3.2/node_modules/node-fetch-commonjs/index.js:2223:11) at ClientRequest.emit (node:events:529:35) at Socket.socketErrorListener (node:_http_client:501:9) at Socket.emit (node:events:517:28) at emitErrorNT (node:internal/streams/destroy:151:8) at emitErrorCloseNT (node:internal/streams/destroy:116:3) at process.processTicksAndRejections (node:internal/process/task_queues:82:21){ type: 'system',
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
erroredSysCall: 'connect'}