1 Star 1 Fork 0

Easy/serverchan-sdk

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

serverchan-sdk

Server酱SDK

这个sdk很简单,只有一个函数。同时支持 sct.ftqq.com 和 sc3.ft07.com, 也就是说,用它你就无需关系用户用的是哪个版本,它会从key中自动适配。 所有语言的参数均为:

  1. sendkey
  2. title
  3. desp - 消息内容,支持markdown,可以外链图片(请使用https)。
  4. options - 其他更多选项

更多选项包括以下:

{
    tags?: string; // SC3专用:标签列表,多个标签使用竖线分隔 
    short?: string; // SC3和SCT:消息卡片的内容,特别适用desp为markdown时,
    noip?: 1; // SCT专用:是否隐藏调用IP,1为隐藏
    channel?: string; // SCT专用:推送使用的消息通道,多个通道值用竖线隔开
    openid?: string; // SCT专用:消息抄送的 openid,仅用于企业微信应用消息和测试号通道,多个 openid 用逗号或竖线分隔
}

返回格式定义:

{
    code: number; // 返回的状态码, 0 为正确 
    message: string;  // 返回的信息,和状态码对应
    data?: any;  // 可选的返回数据,SCT和SC3这部分格式不同
}

以下是各个语言的SDK及其用法,点击查看详细

JavaScript SDK ( Both web & nodejs )

SDK 地址

安装

npm install serverchan-sdk

调用实例

import {scSend} from 'serverchan-sdk'; 

const sendkey = '...'; // 替换成真实的 Server 酱 sendkey
const title = 'Test Notification';
const desp = 'This is a test message';

const response = await scSend(sendkey, title, desp, { tags: '服务器报警|报告' });
console.log('Response:', response);
PHP SDK

SDK 地址

安装

composer require easychen/serverchan-sdk

调用实例

$ret = scSend('sendkey', 'title', 'desp', ['tags'=>'服务器报警|图片']);
print_r($ret);
Python SDK

SDK 地址

安装

pip install serverchan-sdk

调用实例

from serverchan_sdk import sc_send

# 发送消息
sendkey = "..."
title = "测试标题"
desp = "这是消息内容"
options = {"tags": "服务器报警|图片"}  # 可选参数

response = sc_send(sendkey, title, desp, options)
Golang SDK

SDK 地址

安装

go get github.com/easychen/serverchan-sdk-golang

调用实例

package main

import (
    "fmt"
    "github.com/easychen/serverchan-sdk-golang"
)

func main() {
    sendkey := "your-sendkey"
    title := "Test Message"
    desp := "This is a test message"
    
    resp, err := serverchan_sdk.ScSend(sendkey, title, desp, nil)
    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Println("Response:", resp)
    }
}
Dart/Flutter SDK

SDK 地址

Pub Package - https://pub.dev/packages/serverchan_sdk

此 package 依赖 http 包

安装

pubspec.yaml 添加依赖

dependencies:
    serverchan_sdk:

然后运行

dart pub get 
# 或者
flutter pub get

调用实例

import 'package:serverchan_sdk/serverchan_sdk.dart';

void main() async {
  const sendkey = 'your-sendkey';
  const title = '测试标题';
  const desp = '这是消息的详细内容';

  try {
    ScSendResponse response = await scSend(sendkey, title, desp: desp);
    print('Response Code: ${response.code}');
    print('Response Message: ${response.message}');
  } catch (e) {
    print('Error: $e');
  }
}

没有覆盖的语言

如果您使用的语言没有SDK,您可以把以下提示词粘贴到 ChatGPT/Claude/DeepSeek/Kimi,让AI立马帮你写一个。

请参考以下TypeScript代码,编写<xxx语言>对应的实现:

\`\`\`typescript
import fetch from 'cross-fetch';

// ScSendOptions 定义了推送函数的可选参数
export interface ScSendOptions {
    /** sctp 专用:标签列表,多个标签使用竖线分隔 */
    tags?: string;
    /** sctp 和非 sctp:消息卡片的简短描述 */
    short?: string;
    /** 非 sctp:是否隐藏调用IP,1为隐藏 */
    noip?: 1;
    /** 非 sctp:推送使用的消息通道,多个通道值用竖线隔开 */
    channel?: string;
    /** 非 sctp:消息抄送的 openid,多个 openid 用逗号或竖线分隔 */
    openid?: string;
}

// ScSendResponse 定义了推送函数返回的响应结果
export interface ScSendResponse {
    /** 返回的状态码 */
    code: number;
    /** 返回的消息 */
    message: string;
    /** 可选的返回数据 */
    data?: any;
}

// 调用 Server 酱的推送函数
export async function scSend(
    sendkey: string,
    title: string,
    desp: string = '',
    options: ScSendOptions = {}
): Promise<ScSendResponse> {
    
    const url = sendkey.startsWith('sctp') 
        ? `https://${sendkey}.push.ft07.com/send`
        : `https://sctapi.ftqq.com/${sendkey}.send`;
    
    const params = {
        title,
        desp,
        ...options,
    };

    const response = await fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json;charset=utf-8',
        },
        body: JSON.stringify(params),
    });

    const result = await response.json() as ScSendResponse;
    return result;
}
\`\`\`
MIT License Copyright (c) 2024 Easy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/easychen/serverchan-sdk.git
git@gitee.com:easychen/serverchan-sdk.git
easychen
serverchan-sdk
serverchan-sdk
master

搜索帮助