MatrixTikTok/index.ts

54 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-02-22 17:30:56 -05:00
import {
MatrixClient,
SimpleFsStorageProvider,
AutojoinRoomsMixin,
RustSdkCryptoStorageProvider
} from "matrix-bot-sdk";
2023-02-22 22:25:16 -05:00
//Import
import { VidDownload } from "./ttDownloader";
let downloader = new VidDownload;
2023-02-22 17:30:56 -05:00
//import config data
2023-02-22 18:00:25 -05:00
import settings from "./config/config.Dev.json";
2023-02-22 17:30:56 -05:00
2023-02-22 18:00:25 -05:00
const homeserverUrl = settings.matrixAddress;
const accessToken = settings.accessToken;
2023-02-22 22:25:16 -05:00
const ptInstance = settings.ptInstance;
2023-02-22 17:30:56 -05:00
const storage = new SimpleFsStorageProvider("./config/bot.json");
const cryptoProvider = new RustSdkCryptoStorageProvider("./config/crypto");
const client = new MatrixClient(homeserverUrl, accessToken, storage, cryptoProvider);
AutojoinRoomsMixin.setupOnClient(client);
2023-02-22 22:25:16 -05:00
// Test logs, remove later
console.log("Your active homeserver is: " + homeserverUrl);
2023-02-22 17:30:56 -05:00
client.on("room.message", handleCommand);
client.start().then(() => console.log("Now listening for TikTok URLs..."));
async function handleCommand(roomId: string, event: any) {
// Don't handle unhelpful events (ones that aren't text messages, are redacted, or sent by us)
if (event['content']?.['msgtype'] !== 'm.text') return;
if (event['sender'] === await client.getUserId()) return;
// Check to ensure that the `!hello` command is being run
const body = event['content']['body'];
2023-02-22 22:25:16 -05:00
//if (!body?.startsWith("!hello")) return;
if(body?.includes("tiktok.com") && downloader.isValidUrl(body)) {
await client.replyNotice(roomId, event, "You sent a valid TikTok URL");
if(body?.includes("vm.tiktok.com")) {
console.log("Short form TikTok URL detected... Unshortening...");
downloader.UnshortenUrl(body);
}
}
else {
await client.replyNotice(roomId, event, "You did not send a valid URL");
}
}