Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit 553a39f

Browse files
committed
新增: MiraiBot.UploadGroupVoice();
1 parent cf00833 commit 553a39f

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

include/mirai/mirai_bot.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ namespace Cyan
154154
* \return 临时消息图片
155155
*/
156156
TempImage UploadTempImage(const string& fileName);
157+
/**
158+
* @brief 上传可以发给群组的语音
159+
* @param filename 文件名(amr文件)
160+
* @return MiraiVoice
161+
*/
162+
MiraiVoice UploadGroupVoice(const string& filename);
157163
/**
158164
* \brief 获得好友列表
159165
* \return vector<Friend_t>

src/mirai_bot.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,31 @@ namespace Cyan
255255
}
256256

257257

258+
MiraiVoice MiraiBot::UploadGroupVoice(const string& filename)
259+
{
260+
string voice_data = ReadFile(filename);
261+
httplib::MultipartFormDataItems items =
262+
{
263+
{ "sessionKey", sessionKey_, "", "" },
264+
{ "type", "group", "", "" },
265+
{ "voice", voice_data, std::to_string(rand()) + ".amr", "application/octet-stream" }
266+
};
267+
268+
auto res = http_client_.Post("/uploadVoice", items);
269+
270+
if (!res)
271+
throw runtime_error("网络错误");
272+
if (res->status != 200)
273+
throw std::runtime_error("[mirai-http-api error]: " + res->body);
274+
json re_json = json::parse(res->body);
275+
MiraiVoice result;
276+
result.Id = re_json["voiceId"].get<string>();
277+
if (!re_json["url"].is_null())
278+
result.Url = re_json["url"].get<string>();
279+
result.Path = re_json["path"].get<string>();
280+
return result;
281+
}
282+
258283
vector<Friend_t> MiraiBot::GetFriendList()
259284
{
260285
auto res = http_client_.Get(("/friendList?sessionKey=" + sessionKey_).data());
@@ -628,9 +653,9 @@ namespace Cyan
628653
}
629654

630655
MiraiBot& MiraiBot::RegisterCommand(
631-
const string& commandName,
632-
const vector<string> alias,
633-
const string& description,
656+
const string& commandName,
657+
const vector<string> alias,
658+
const string& description,
634659
const string& helpMessage)
635660
{
636661
json data =

0 commit comments

Comments
 (0)