• 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

  • 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

  • 工资「喂饱肚子」,副业「养活灵魂」!

  • 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

  • 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 本站会放置Google广告用于维持域名以及网站服务器费用。

  • 问题反馈可发送邮件到stubbornhuang@qq.com

  • 感谢大家访问本站,希望本站的内容可以帮助到大家!

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

Python – 发送bark消息到服务器

Python 发布于2025-02-13 阅读 1,300次 0次评论 0次点赞 本文共2020个字,阅读需要6分钟。

1 python发送bark消息到服务器

bark的官方文档:https://bark.day.app/#/tutorial 上提到,我们可以通过get、post等多种方式发送bark消息到服务器,比如post

curl -X "POST" "https://api.day.app/your_key" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{
  "body": "Test Bark Server",
  "title": "Test Title",
  "badge": 1,
  "sound": "minuet",
  "icon": "https://day.app/assets/images/avatar.jpg",
  "group": "test",
  "url": "https://mritd.com"
}'

更多的请求参数可以参考bark官方文档。

我们在python中使用request库可实现发送消息到bark服务器

import os
import requests

BARK_SERVER_URL = 'https://你的bark服务器地址/你的key/'  #这里写你的bark服务器地址

class BarkNotify:
    def __init__(self, bark_server_url=None):
        if bark_server_url is None:
            self.bark_server_url = BARK_SERVER_URL
        else:
            self.bark_server_url = bark_server_url

    def send_bark_notification(self, message, title=None, badge=1, sound="minuet", icon=None, group=None, url=None):
        """
        Send a notification to the server using Bark.

        Parameters:
        - server_url (str): The URL of the Bark server.
        - message (str): The message to send.
        - title (str, optional): The title of the message. Defaults to None.
        - badge (int, optional): The badge number to display. Defaults to 1.
        - sound (str, optional): The sound to play. Defaults to "minuet".
        - icon (str, optional): The URL of the icon image. Defaults to None.
        - group (str, optional): The group name. Defaults to None.
        - url (str, optional): The URL to open when the notification is clicked. Defaults to None.
        """
        payload = {
            'body': message,
            'title': title,
            'badge': badge,
            'sound': sound,
            'icon': icon,
            'group': group,
            'url': url,
        }
        # Removing keys with None values
        payload = {key: value for key, value in payload.items() if value is not None}

        headers = {
            'Content-Type': 'application/json; charset=utf-8'
        }

        response = requests.post(self.bark_server_url, json=payload, headers=headers)
        if response.status_code == 200:
            print("Notification sent successfully!")
        else:
            print(f"Failed to send notification. Status code: {response.status_code}")
            print(f"Response: {response.text}")

if __name__ == '__main__':
    message = "Test Bark Server"
    title = "Test Title"
    badge = 1
    sound = "minuet"
    icon = "https://day.app/assets/images/avatar.jpg"
    group = "test"
    url = "https://stubbornhuang.com"

    bark_notify = BarkNotify()
    bark_notify.send_bark_notification(message, title, badge, sound, icon, group, url)

运行程序即可在手机端看到消息推送。

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:Python – 发送bark消息到服务器

原文链接:https://www.stubbornhuang.com/3122/

发布于:2025年02月13日 9:57:25

修改于:2025年02月13日 9:57:25

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
python - 为图片增加随机马赛克
Python
下一篇
python - 减少opencv-python保存的视频文件大小
Python
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号