Python通过飞书机器人推送消息至飞书群中

news/2024/11/17 9:58:39/

首先需要在飞书开发者页面定制飞书消息卡片,然后将卡片的json复制出来即可。

import json
import requests
from requests_toolbelt import MultipartEncoderclass Send_feishu_msg:# 应用凭证,获取方式查看上面的步骤app_id = "cli_xxxxxxxxxxxxxxx"app_secret = "nEkQ8A2x13_xxxxxxxxxxx"# 机器人webhookchatGPT_url = 'https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxx'headers = {"Content-Type": "application/json; charset=utf-8",}json_data = {"config": {"wide_screen_mode": True},"header": {"template": "green","title": {"tag": "plain_text","content": "📢📢📢xxxxxxxxxxx📢📢📢"}},"elements": [{"tag": "markdown","content": "📌**xxxxxxxxxxx**"},{"alt": {"content": "","tag": "plain_text"},"img_key": "${Picture_1}","tag": "img","mode": "fit_horizontal","compact_width": False},{"tag": "hr"},{"tag": "markdown","content": "📌**xxxxxxxxxxx**"},{"tag": "img","img_key": "${Picture_2}","alt": {"tag": "plain_text","content": ""},"mode": "fit_horizontal","preview": True,"compact_width": False},{"tag": "action","actions": [{"tag": "button","text": {"tag": "plain_text","content": "点击查看更多详情"},"type": "primary","multi_url": {"url": "https://lotuscars.feishu.cn/sheets/xxxxxxxxx","pc_url": "","android_url": "","ios_url": ""}}]},{"tag": "div","text": {"content": "<at id=all></at>","tag": "lark_md"}}]}def __init__(self):url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/xxxxxxxx"payload_data = {"app_id": self.app_id,"app_secret": self.app_secret}response = requests.post(url=url, data=json.dumps(payload_data), headers=self.headers).json()self.token = response['tenant_access_token']def uploadImage(self, picture_path):get_image_key_url = "https://open.feishu.cn/open-apis/im/v1/xxxxxx"form = {'image_type': 'message','image': (open(picture_path, 'rb'))}  # 需要替换具体的pathmulti_form = MultipartEncoder(form)image_key_headers = {'Authorization': 'Bearer ' + self.token,}image_key_headers['Content-Type'] = multi_form.content_typeresponse = requests.request("POST", get_image_key_url, headers=image_key_headers, data=multi_form)content = json.loads(response.content)img_key = str(content['data']['image_key'])print(response.headers['X-Tt-Logid'])  # for debug or oncallprint(f'The img_key is {img_key}')  # Print Responsereturn img_keydef send_msg_talk(self, picture1_img_key, picture2_img_key):self.json_data['elements'][1]['img_key'] = picture1_img_keyself.json_data['elements'][4]['img_key'] = picture2_img_keymsg_card = json.dumps(self.json_data)body = json.dumps({"msg_type": "interactive", "card": msg_card})response = requests.post(url=self.chatGPT_url, data=body, headers=self.headers)print(response)

http://www.ppmy.cn/news/966652.html

相关文章

基于 LLM 的应用程序如何集成自定义函数 (API)? How a LLM-based application, integrates a custom function (API)?

Hi The general problem here is how to create conversational applications based on GPT3 able to accomplish task-oriented deterministic activities, overtaking the ‘partially-no-deterministic’ elaborations of generative systems. Hi这里的一般问题是如何创建基…

Ae 效果:CC Kaleida

风格化/CC Kaleida Stylize/CC Kaleida 万花筒是一种装置或玩具&#xff0c;通过多次反射和镜像&#xff0c;将图像分割成多个对称和重复的图案。CC Kaleida&#xff08;CC 万花筒&#xff09; 效果通过类似的方式在图像上创建镜像和对称的视觉效果。 提示&#xff1a; 由于 CC…

我用 ChatGPT 做了一道菜 !!

自从使用ChatGPT之后&#xff0c;我的生活变得更加丰富了。 它以一个厨师的身份&#xff0c;为我提供各种健康美味的菜谱&#xff0c;以及干货知识、烹饪技巧等等&#xff0c;让我能够轻松地做出口感和营养都兼备的佳肴。 ChatGPT不仅为我提供各种各样的菜谱&#xff0c;包括…

告别窝囊庸俗的餐桌,跟着ChatGPT做法做菜没毛病!

这篇文章将为您带来一个全新的餐桌体验&#xff0c;告别了一成不变的庸俗菜肴和餐桌礼仪。 我们教你如何借助 ChatGPT AI技术简单快速地做出一顿令人难忘的美食。 这是一次创意尝试&#xff0c;探索更多食物的新奇做法&#xff0c;所需工具不再花哨&#xff0c;技巧也不再烦琐…

ChatGPT,又爆了...

这马斯克&#xff0c;Bengio等千位AI科技人士签署暂停巨型 AI 实验公开信&#xff0c;在这封信里马斯克等人呼吁所有人工智能实验室立即暂停训练比 GPT-4 更强大的人工智能系统&#xff08;包括目前正在训练的 GPT-5&#xff09;至少 6 个月&#xff0c;同时利用这段时间共同开…

打破创作界限,1天创作1000篇精品小红书带货笔记!chatgpt超级写手机器人创作流程介绍。

【Chatgpt超级写手机器人教程】 1天创作1000篇精品小红书带货笔记&#xff01;✨ 难听的真话&#xff1a;做运营不用RPA好比猴子不用工具。RPA就是非技术人最佳自动化利器&#xff0c;学三个月Python不如学一周RPA 我是RPA帮创始人阿愚呱呱&#xff0c;特会玩赚RPA的运营人。[R…

叮叮猫 NKOJ P3722 (树状数组+容斥原理)

问题描述 【问题描述】 叮叮猫&#xff0c;学名蜻蜓&#xff0c;无脊椎动物。一般体型较大&#xff0c;翅膀长而窄&#xff0c;膜质&#xff0c;有清 晰的网状翅脉。 有个叮叮猫飞到了nodgd房间里&#xff0c;nodgd赶紧用高速照相机连拍了?张清晰 的照片&#xff0c…

单片机如何关掉蜂鸣器_【开源】蜂鸣器怎样实现类似高级冰箱上很清脆“叮叮”声......

作者&#xff1a;阿莫amigenius&#xff0c;排版整理&#xff1a;晓宇 微信公众号&#xff1a;芯片之家(ID&#xff1a;chiphome-dy) 阿莫上有位兄弟发帖问蜂鸣器如何产生清脆的“叮叮”的声音&#xff0c;帖子不小心&#x1f525;了。 很快就盖了100多层楼&#xff0c;大家普遍…