浣跨敤 Serverless + 椋炰功鎵撻€犱綘鐨勪釜鎬у寲娑堟伅鎻愰啋绯荤粺
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浣跨敤 Serverless + 椋炰功鎵撻€犱綘鐨勪釜鎬у寲娑堟伅鎻愰啋绯荤粺相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%8e%a8%e9%80%81' title='鎺ㄩ€?>鎺ㄩ€?/a> puts more href 鏌ョ湅 info NPU 鏈湴
涓€銆佸墠瑷€
鍦ㄦ棩甯稿伐浣滃涔犵敓娲讳腑锛屾垜浠彲鑳戒細閬囧埌浠ヤ笅鎯呭舰锛?/p>
- 鑷繁绠$悊鐨勬煇鍙版湇鍔″櫒瀹曟満浜嗭紝浣嗘槸娌℃湁寰楀埌鍙婃椂鐨勬彁閱掞紝瀵艰嚧涓氬姟鍙楀埌鎹熷け
- 鏌愪簺鑷繁寰堟兂娉ㄥ唽鐨勭綉绔欐倓鎮勫紑鏀炬敞鍐岋紝浣嗘槸鑷繁骞舵病鏈夊強鏃跺緱鐭ワ紝浜庢槸鍙兘缁х画婕棤鐩殑鐨勭瓑寰?/li>
- 鈥︹€?/li>
濡傛灉姣忎欢浜嬮兘鑺辨椂闂村幓鍏虫敞锛岄偅鎴戜滑鐨勬椂闂村繀鐒朵細涓嶅鐢紝閭f湁娌℃湁浠€涔堝姙娉曞彲浠ヨ杩欎簺娑堟伅闆嗕腑璧锋潵骞朵笖鍙婃椂鎺ㄩ€佸憿锛熷湪杩欓噷鎴戞兂鍚戝ぇ瀹舵帹鑽愪竴涓В鍐虫柟妗堬紝閭e氨鏄?strong>浣跨敤 Serverless + 椋炰功鎵撻€犲睘浜庤嚜宸辩殑涓€у寲娑堟伅鎻愰啋绯荤粺
銆?/p>
椋炰功缃戦〉鐗?/a>鐧诲綍
鎵撳紑椋炰功寮€鏀惧钩鍙?/a>锛岀偣鍑?strong>鍒涘缓浼佷笟鑷缓搴旂敤锛屽苟杈撳叆搴旂敤鍚嶇О鍜?strong>搴旂敤鍓爣棰?/strong>锛岀劧鍚庣偣鍑?strong>纭畾鍒涘缓
- 鍦ㄤ紒涓氳嚜寤哄簲鐢ㄥ垪琛ㄤ腑鐐瑰嚮鍒氬垰鍒涘缓鎴愬姛鐨勫簲鐢紝骞惰褰?App ID 鍜?App Secret
.env
TENCENT_SECRET_ID=AKID********************************
TENCENT_SECRET_KEY=********************************
娉細杩欓噷鐨?TENCENT_SECRET_ID
鍜?TENCENT_SECRET_KEY
鍙湪鑵捐浜戞帶鍒跺彴鐨?a href="https://console.cloud.tencent.com/cam/capi">璁块棶瀵嗛挜
涓幏鍙栵紝濡傛灉娌℃湁瀵嗛挜鍒欓渶瑕佽嚜宸辨柊寤轰竴涓?/p>
serverless.yml
myFunction:
component: "@serverless/tencent-scf"
inputs:
name: feishu-notify-py
codeUri: "./"
handler: index.main_handler
runtime: Python3.6
region: ap-guangzhou
description: My Serverless Function Used to Notify Myself
memorySize: 128
events:
- apigw:
name: serverless
parameters:
protocols:
- https
endpoints:
- path: "/"
method: POST
娉細鍙互鐐瑰嚮杩欓噷鏌ョ湅
serverless.yml
涓墍鏈夊彲鐢ㄥ睘鎬х殑灞炴€у垪琛?/p>
index.py
def main_handler(event, context):
import requests
import json
print(event)
CONFIG = {
"app_id": "********************",
"app_secret": "********************************"
}
# my auth
if 鈥榤yauth鈥?not in event[鈥榪ueryString鈥榏 or event[鈥榪ueryString鈥榏[鈥榤yauth鈥榏 != 鈥榝eishu1鈥?
return 鈥榝orbidden鈥? # Get content
postContent = event[鈥榖ody鈥榏
try:
postContent = json.loads(postContent)
except:
return 鈥榚rror in json_loads(line: 19)鈥? if 鈥榗ontent鈥?not in postContent:
return 鈥榠nvalid params鈥? content = postContent[鈥榗ontent鈥榏
# Get tenant_access_token
try:
res = requests.post(鈥榟ttps://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/鈥? {
"app_id": CONFIG[鈥榓pp_id鈥榏,
"app_secret": CONFIG[鈥榓pp_secret鈥榏
})
except:
return 鈥榚rror in get_tenant_access_token鈥? data = json.loads(res.text)
if data[鈥榗ode鈥榏 != 0:
return data[鈥榤sg鈥榏
token = data[鈥榯enant_access_token鈥榏
# Get chat_id
try:
res = requests.get(鈥榟ttps://open.feishu.cn/open-apis/chat/v4/list鈥? headers={
鈥楢uthorization鈥? 鈥楤earer %s鈥?% (token)
})
except:
return 鈥榚rror in get_chat_id鈥? data = json.loads(res.text)
if data[鈥榗ode鈥榏 != 0:
return data[鈥榤sg鈥榏
groupList = data[鈥榙ata鈥榏[鈥榞roups鈥榏
myGroupId = groupList[0][鈥榗hat_id鈥榏
# Send message
try:
res = requests.post(鈥榟ttps://open.feishu.cn/open-apis/message/v4/send/鈥? json={
"chat_id": myGroupId,
"msg_type": "text",
"content": {
"text": content
}
}, headers={
鈥楢uthorization鈥? 鈥楤earer %s鈥?% (token),
鈥楥ontent-Type鈥? 鈥榓pplication/json鈥? })
except:
return 鈥榚rror in send message鈥? data = json.loads(res.text)
if data[鈥榗ode鈥榏 != 0:
return data[鈥榤sg鈥榏
return 鈥榮uccess鈥?
鍏充簬 index.py
锛岃繖閲屾湁鍑犵偣闇€瑕佷綔鍑鸿鏄庯細
- 浠g爜涓殑
app_id
鍜?app_secret
椤归渶濉啓鍦ㄥ噯澶囧伐浣滆褰曠殑 App ID 鍜?App Secret - 鏈€缁堟垜浠娇鐢?
POST
鏂规硶鍙戦€佹秷鎭?/li> - 鍦ㄨ皟鐢ㄦ椂锛屾垜浠繕闇€瑕佸湪
query
澶勫姞涓??myauth=feishu1
锛岀洰鐨勬槸浣滅畝鍗曢獙璇佷互闃叉浠栦汉鍙戦€侊紝渚嬪https://service-********-**********.**.apigw.tencentcs.com/release/?myauth=feishu1
涓夈€侀儴缃?Serverless 鏈嶅姟
- 閫氳繃 npm 瀹夎 Serverless
$ npm install -g serverless
- 閫氳繃
serverless
鍛戒护杩涜閮ㄧ讲锛屽苟娣诲姞--debug
鍙傛暟鏌ョ湅閮ㄧ讲杩囩▼涓殑淇℃伅
$ serverless --debug
- 浠庣粓绔幏鍙?API 缃戝叧鐨?URL
鑾峰彇 API 缃戝叧鐨?URL
椋炰功寮€鏀惧钩鍙?/a>锛屽湪浼佷笟鑷缓搴旂敤鍒楄〃涓偣鍑诲垰鍒氬垱寤烘垚鍔熺殑搴旂敤
鐐瑰嚮搴旂敤鍔熻兘-鏈哄櫒浜?/strong>锛岀偣鍑?strong>鍚敤鏈哄櫒浜?/strong>
- 鐐瑰嚮鐗堟湰绠$悊涓庡彂甯?/strong>-鍒涘缓鐗堟湰锛屽弬鑰冧笅鍥捐繘琛岄厤缃紙鍏堜笉瑕佺偣淇濆瓨锛?/li>
- 鍦?strong>鍙敤鎬х姸鎬?/strong>澶勭偣鍑?strong>缂栬緫锛岄€夋嫨鎵€鏈夊憳宸ワ紝鐒跺悗鐐瑰嚮淇濆瓨
- 鐐瑰嚮鐢宠鍙戝竷
- 鐐瑰嚮椋炰功缃戦〉鐗堢殑澶村儚锛岃繘鍏ラ涔︾鐞嗗悗鍙?/li>
- 鐐瑰嚮宸ヤ綔鍙?/strong>-搴旂敤瀹℃牳锛岀劧鍚庣偣鍑?strong>瀹℃牳
- 鐐瑰嚮閫氳繃
鍙傛暟
绫诲瀷
蹇呭~/閫夊~
璇存槑
榛樿鍊?/th>
瀹炰緥
Content-Type
string
蹇呭~
Content-Type
application/json
璇锋眰 Query锛?/strong>
鍙傛暟 | 绫诲瀷 | 蹇呭~/閫夊~ | 璇存槑 | 榛樿鍊?/th> | 瀹炰緥 |
---|---|---|---|---|---|
myauth | string | 蹇呭~ | 绠€鍗曢獙璇?/td> | feishu1 |
璇锋眰 Body锛?/strong>
{
"content": "杩欓噷濉叆浣犳兂瑕佸彂閫佺殑淇℃伅"
}
鍙互鐪嬪埌锛岄涔︾殑娑堟伅鎺ㄩ€佸緢鍙婃椂
One More Thing
3 绉掍綘鑳藉仛浠€涔堬紵鍠濅竴鍙f按锛岀湅涓€灏侀偖浠讹紝杩樻槸 鈥斺€?閮ㄧ讲涓€涓畬鏁寸殑 Serverless 搴旂敤锛?/p>
澶嶅埗閾炬帴鑷?PC 娴忚鍣ㄨ闂細https://serverless.cloud.tencent.com/deploy/express
3 绉掓瀬閫熼儴缃诧紝绔嬪嵆浣撻獙鍙蹭笂鏈€蹇殑 Serverless HTTP 瀹炴垬寮€鍙戯紒
浼犻€侀棬锛?/strong>
- GitHub: github.com/serverless
- 瀹樼綉锛?a href="https://serverless.com/">serverless.com
娆㈣繋璁块棶锛?a href="https://serverlesscloud.cn/">Serverless 涓枃缃?/a>锛屾偍鍙互鍦?鏈€浣冲疄璺?/a> 閲屼綋楠屾洿澶氬叧浜?Serverless 搴旂敤鐨勫紑鍙戯紒
鎺ㄨ崘闃呰锛?a href="https://union-click.jd.com/jdc?e=jdext-1235945425166544896-0&p=AyIGZRNeEAAbAF0aXyUFFg5VG10RAxUAUisfSlpMWGVCHlBDUAxLBQNQVk4YDk5ER1xOGVIfUhUCFANUHFwSHUtCCUZrEkdsezZSJhVgRn0cGhxzX2FdJRldZQ4eN1USWxcKEwZVHFglAhMGVR9eFgEUBmUrWxAyU2lSH1scAxU3VCtbEQsUAlEYWREBEgNXK1sdBiLR%2B4%2BOnb3Lt%2FDN8bvXn7eAkvDBvJQ3ZStYJVlHUxxeRxUBEANdHl8QARYHUhxfEAcbD1IHWiUCEwZVElMRAhMFOxo1FQoSBFAaWxMEFjdVK1slXVZaCCtZFAMQBQ%3D%3D">銆奡erverless 鏋舵瀯锛氫粠鍘熺悊銆佽璁″埌椤圭洰瀹炴垬銆?/a>
以上是关于浣跨敤 Serverless + 椋炰功鎵撻€犱綘鐨勪釜鎬у寲娑堟伅鎻愰啋绯荤粺的主要内容,如果未能解决你的问题,请参考以下文章