使用场景
最近使用飞书聊天,觉得蛮好用的,刚好这段时间在备考,就想着每天推送一些励志语录 和风景图片什么的,提一下动力
推送机制
飞书群机器人会有对应的webhook,顾名思义就是网络钩子,通过对钩子发送网络请求,即可将消息推送至群内,本文使用的python完成
代码目录
def getttoken():
def uploadImage():
def getpic():
main()
代码详解
获取图片
在网上找到了一个图片API,更新序号即可获得不通过的图片,我以日期作为参考,每天获取一张新的照片,代码如下
def getpic():
current_time = time.strftime('%Y-%m-%d', time.localtime(time.time()))
start_date = '2022-2-17'
end_date = current_time
start_sec = time.mktime(time.strptime(start_date, '%Y-%m-%d'))
end_sec = time.mktime(time.strptime(end_date, '%Y-%m-%d'))
distance_days = int((end_sec - start_sec)/(24*60*60))
days = 121+distance_days
days = str(days)
url = "https://cdn.jsdelivr.net/gh/flipped-1121/APIPIC@master/scenery/"+days+".jpg"
name = "1.jpg"
conn = urllib.request.urlopen(url)
f = open(name, 'wb')
f.write(conn.read())
f.close()
print('Pic Saved!')
获取有效token
因为飞书中使用图片需要先将图片上传至飞书中,上传过程中需要获取tenant_access_token(企业自建应用)
图片功能需要机器人获得上传图片的权限,具体步骤点击这里
代码如下
def gettoken():
url='https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal?app_id=YOUR_ID&app_secret=YOUR_KEY'
res = requests.get(url=url)
token=res.json()
token_text=token['tenant_access_token']
return token_text
上传图片并得到Image_Key
上传图片文档请参考飞书官方文档 代码如下
def uploadImage(token):
url = "https://open.feishu.cn/open-apis/im/v1/images"
form = {'image_type': 'message',
'image': (open('./1.jpg', 'rb'))}
multi_form = MultipartEncoder(form)
headers = {
'Authorization': 'Bearer '+token,
}
headers['Content-Type'] = multi_form.content_type
response = requests.request("POST", url, headers=headers, data=multi_form)
res=bytes.decode(response.content)
res=eval(res)
print(res['data']['image_key'])
return res['data']['image_key']
使用
上传至服务器定时调用.py文件即可
完整代码
import time
import requests
import urllib.request
from requests_toolbelt import MultipartEncoder
def gettoken():
url='https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal?app_id=YOUR_ID&app_secret=YOUR_KEY'
res = requests.get(url=url)
token=res.json()
token_text=token['tenant_access_token']
return token_text
def uploadImage(token):
url = "https://open.feishu.cn/open-apis/im/v1/images"
form = {'image_type': 'message',
'image': (open('./1.jpg', 'rb'))}
multi_form = MultipartEncoder(form)
headers = {
'Authorization': 'Bearer '+token,
}
headers['Content-Type'] = multi_form.content_type
response = requests.request("POST", url, headers=headers, data=multi_form)
res=bytes.decode(response.content)
res=eval(res)
print(res['data']['image_key'])
return res['data']['image_key']
def getpic():
current_time = time.strftime('%Y-%m-%d', time.localtime(time.time()))
start_date = '2022-2-17'
end_date = current_time
start_sec = time.mktime(time.strptime(start_date, '%Y-%m-%d'))
end_sec = time.mktime(time.strptime(end_date, '%Y-%m-%d'))
distance_days = int((end_sec - start_sec)/(24*60*60))
days = 121+distance_days
days = str(days)
url = "https://cdn.jsdelivr.net/gh/flipped-1121/APIPIC@master/scenery/"+days+".jpg"
name = "1.jpg"
conn = urllib.request.urlopen(url)
f = open(name, 'wb')
f.write(conn.read())
f.close()
print('Pic Saved!')
if __name__ == '__main__':
token=gettoken()
getpic()
image_key=uploadImage(token)
url = "YOUR_WEBHOOK"
url_soup = 'https://apis.juhe.cn/fapig/soup/query?key=YOUR_KEY'
res_soup = requests.get(url=url_soup)
soup = res_soup.json()
soup_text = soup['result']['text']
current_time = time.strftime('%Y-%m-%d', time.localtime(time.time()))
start_date = current_time
end_date = '2022-12-25'
start_sec = time.mktime(time.strptime(start_date, '%Y-%m-%d'))
end_sec = time.mktime(time.strptime(end_date, '%Y-%m-%d'))
distance_days = int((end_sec - start_sec)/(24*60*60))
distance_days = str(distance_days)
data = {
"msg_type": "interactive",
"card": {
"config": {
},
"header": {
"template": "orange",
"title": {
"content": " YOUR_CONTENT",
"tag": "plain_text"
}
},
"i18n_elements": {
"zh_cn": [
{
"tag": "div",
"text": {
"content":"YOUR_CONTENT",
"tag": "lark_md"
}
},
{
"tag": "div",
"text": {
"content": "\""+soup_text+"\"",
"tag": "lark_md"
}
},
{
"alt": {
"content": "",
"tag": "plain_text"
},
"img_key": image_key,
"tag": "img"
}
]
}
}
}
res = requests.post(url=url, json=data)
print(res.text)
|