packages/missgram: rename

This commit is contained in:
2025-12-27 11:40:10 +08:00
parent 4d1f6eb6b5
commit 0c80dde1d1
13 changed files with 43 additions and 43 deletions

1
packages/missgram/.envrc Normal file
View File

@@ -0,0 +1 @@
use flake .#missgram

1
packages/missgram/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
config.yaml

View File

@@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.14)
project(missgram VERSION 0.0.0 LANGUAGES CXX)
enable_testing()
include(GNUInstallDirs)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message("Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()
find_package(biu REQUIRED)
find_package(httplib REQUIRED)
add_executable(missgram src/main.cpp)
target_link_libraries(missgram PRIVATE biu::biu httplib::httplib)
target_compile_features(missgram PRIVATE cxx_std_23)
if(DEFINED MISSGRAM_CONFIG_FILE)
target_compile_definitions(missgram PRIVATE MISSGRAM_CONFIG_FILE="${MISSGRAM_CONFIG_FILE}")
endif()
install(TARGETS missgram RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
get_property(ImportedTargets DIRECTORY "${CMAKE_SOURCE_DIR}" PROPERTY IMPORTED_TARGETS)
message("Imported targets: ${ImportedTargets}")
message("List of compile features: ${CMAKE_CXX_COMPILE_FEATURES}")
message("CMake build type: ${CMAKE_BUILD_TYPE}")

View File

@@ -0,0 +1,8 @@
{ lib, stdenv, cmake, pkg-config, biu, configFile ? null, httplib }: stdenv.mkDerivation
{
name = "missgram";
src = ./.;
buildInputs = [ biu httplib ];
nativeBuildInputs = [ cmake pkg-config ];
cmakeFlags = lib.optional (configFile != null) [ "-DMISSGRAM_CONFIG_FILE=${configFile}" ];
}

View File

@@ -0,0 +1,72 @@
# include <biu.hpp>
# include <httplib.h>
# include <tgbot/tgbot.h>
# ifndef MISSGRAM_CONFIG_FILE
# define MISSGRAM_CONFIG_FILE "./config.yaml"
# endif
int main()
{
using namespace biu::literals;
biu::Logger::Guard log;
struct Config
{
std::string Secret;
std::string TelegramBotToken;
std::string TelegramChatId;
int ServerPort;
} config = YAML::LoadFile(MISSGRAM_CONFIG_FILE).as<Config>();
biu::Logger::try_exec([&]
{
httplib::Server svr;
svr.Post("/", [&](const httplib::Request& req, httplib::Response& res)
{
biu::Logger::try_exec([&]
{
if (req.get_header_value("x-misskey-hook-secret") != config.Secret)
throw std::runtime_error("Invalid secret key.");
struct Content
{
std::string type, server;
struct
{
struct Note
{
std::string text, visibility;
std::optional<std::string> replyId;
struct Renote { std::string id; };
std::optional<Renote> renote;
};
std::optional<Note> note;
} body;
};
auto content = YAML::Load(req.body).as<Content>();
if
(
content.type != "note" // 只转发 note 的情况
|| !content.body.note // 大概不会发生,但还是判断一下
|| content.body.note->visibility != "public" // 只转发公开的 note
|| content.body.note->replyId // 不转发回复
) return;
std::string text = content.body.note->text;
if (content.body.note->renote)
text += "\n🔁 Renote: {}/notes/{}"_f(content.server, content.body.note->renote->id);
TgBot::Bot bot(config.TelegramBotToken);
// bot.getApi().sendMessage(config.TelegramChatId, text);
res.status = 200;
res.body = "OK";
log.debug(req.body);
});
});
svr.listen("0.0.0.0", config.ServerPort);
return 0;
});
}