这个库封装了 JSON 解码处理,可以轻松地将传入的 JSON 字符串解码为可操作的对象。
这个库封装了将对象转换为 Telegram Bot API 请求参数的处理,以及执行请求的处理,从而可以轻松地处理传入的更新。
将此项目作为依赖项添加到你的 Package.swift 文件中。
.package(url: "https://github.com/shaneqi/ZEGBot.git", from: Version(4, 2, 8))
查看此处的示例:./Example。
或者,你可以直接将以下代码放入你的项目的 main.swift
文件中。
import ZEGBot
// Don't forget to fill in your bot token.
let bot = ZEGBot(token: "TYPE YOUR TOKEN HERE")
do {
try bot.send(message: "Hello world!", to: AnyChat(chatId: CHAT_ID))
} catch let error {
NSLog("Bot exit due to: \(error)")
}
import ZEGBot
// Don't forget to fill in your bot token.
let bot = ZEGBot(token: "YOUR_BOT_TOKEN")
do {
try bot.run { updates, bot in
// Handle updates here...
}
} catch let error {
NSLog("Bot exit due to: \(error)")
}
获取传入消息的文本内容
...
if let text = update.message?.text {
// Do something.
}
...
如果存在,获取其他类型的内容
...
if let voice = update.message?.voice {
// Do something.
}
...
向聊天发送文本消息
...
if let message = update.message {
do {
try bot.send(message: "bar", to: message.chat)
} catch let error {
NSLog("Failed to send message due to: \(error)")
}
}
...
或者
...
do {
try bot.send(message: "bar", to: AnyChat(chatId: CHAT_ID))
} catch let error {
NSLog("Failed to send message due to: \(error)")
}
...
发送一条带有无预览 markdown 链接的静默消息到聊天
...
do {
try bot.send(
message: "[Google](https://google.com)",
to: AnyChat(chatId: CHAT_ID),
parseMode: .markdown,
disableWebPagePreview: true,
disableNotification: true)
} catch let error {
NSLog("Failed to send message due to: \(error)")
}
...
在所有发送方法中,发送到 Message 对象意味着回复此特定消息。而发送到 Chat 对象意味着发送到此聊天,不回复任何人。
...
if let message = update?.message {
do {
/* This sends a message replying to another message. */
try bot.send(message: "bar", to: message)
/* This doesn't reply to a message. */
try bot.send(message: "bar", to: message.chat)
} catch let error {
NSLog("Failed to send message due to: \(error)")
}
}
...
并非所有类型都受支持,请在 Telegram Bot API 上查看更多详细信息。
并非所有方法都受支持,请在 Telegram Bot API 上查看更多详细信息。
这个项目的目标不是保持更新到最新的 Telegram bot API,而是为社区服务。因此,即使你在此项目中没有看到你需要 API 功能,请 联系我,我会尽力添加你请求的功能。
此项目根据 Apache License v2.0 获得许可。