MatrixTikTok/index.ts
2023-02-22 22:25:16 -05:00

54 lines
1.7 KiB
TypeScript

import {
MatrixClient,
SimpleFsStorageProvider,
AutojoinRoomsMixin,
RustSdkCryptoStorageProvider
} from "matrix-bot-sdk";
//Import
import { VidDownload } from "./ttDownloader";
let downloader = new VidDownload;
//import config data
import settings from "./config/config.Dev.json";
const homeserverUrl = settings.matrixAddress;
const accessToken = settings.accessToken;
const ptInstance = settings.ptInstance;
const storage = new SimpleFsStorageProvider("./config/bot.json");
const cryptoProvider = new RustSdkCryptoStorageProvider("./config/crypto");
const client = new MatrixClient(homeserverUrl, accessToken, storage, cryptoProvider);
AutojoinRoomsMixin.setupOnClient(client);
// Test logs, remove later
console.log("Your active homeserver is: " + homeserverUrl);
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'];
//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");
}
}