Skip to content

Commit

Permalink
add voiceTranscript to filters
Browse files Browse the repository at this point in the history
  • Loading branch information
soberhacker committed Sep 19, 2023
1 parent 56241c0 commit e507794
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 37 deletions.
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Telegram Sync for Obsidian

<a href="https://github.com/soberhacker/obsidian-telegram-sync/releases/latest">
<img src="https://img.shields.io/github/v/release/soberhacker/obsidian-telegram-sync?label=plugin&display_name=tag&logo=obsidian&color=purple&logoColor=violet">
</a>&nbsp;<a href="https://github.com/soberhacker/obsidian-telegram-sync">
Expand All @@ -9,41 +10,40 @@
<img src="https://img.shields.io/badge/Telegram-Support-red.svg?logo=telegram&logoColor=f5f5f5&color=red">
</a><br><br>

Transfer messages and files from Telegram to your Obsidian vault. You can easily save text, voice transcripts, images, and other files from your Telegram chats to Obsidian for further processing and organization. This plugin is only available for desktops and would never be available on mobile platforms.
Transfer messages and files from [Telegram](https://telegram.org/) to your [Obsidian](https://obsidian.md/plugins?id=telegram-sync) vault. You can easily save text, voice transcripts, images, and other files from your Telegram chats to Obsidian for further processing and organization. This plugin is only available for desktops and would never be available on mobile platforms.

---

## 📚 Table of Contents

- [Features](#-features)
- [Installation](#-installation)
- [Manual Installation](#-manual-installation)
- [Usage](#-usage)
- [Supporters & Donations](#-supporters--donations)
- [Contributing](#-contributing)
- [Features](#-features)
- [Installation](#-installation)
- [Manual Installation](#-manual-installation)
- [Usage](#-usage)
- [Supporters & Donations](#-supporters--donations)
- [Contributing](#-contributing)

## 🚀 Features

- Synchronize text messages and files
- Save messages as individual notes or append to an existing note
- Transcript voices and video notes (for Telegram Premium subscribers only)
- Use customizable templates for new notes
- Set folders for new notes and files
- Automatically format text messages with markdown
- Delete processed messages from Telegram
- Synchronize text messages and files
- Save messages as individual notes or append to an existing note
- Transcript voices and video notes (for Telegram Premium subscribers only)
- Use customizable templates for new notes
- Set folders for new notes and files
- Automatically format text messages with markdown
- Delete processed messages from Telegram

## 📦 Installation

1. Open Obsidian and navigate to Settings > Third-party plugin
2. Make sure Safe mode is off
3. Click the Browse button to open the Community plugins window
4. Search for **Telegram Sync** in the search bar
5. Click the Install button, then enable the plugin by toggling the switch
1. Click on [Telegram Sync](https://obsidian.md/plugins?id=telegram-sync) link
2. Allow to open Obsidian app
3. Make sure that community plugins is turned on
4. Click the Install button, then enable the plugin by toggling the switch

## 👏 Manual Installation

1. Download main.js, styles.css, manifest.json from the [latest release](https://github.com/soberhacker/obsidian-telegram-sync/releases//latest)
2. Copy the downloaded files to <pathToYourVault>/.obsidian/plugins/telegram-sync/
2. Copy the downloaded files to <path-to-your-vault>/.obsidian/plugins/telegram-sync/
3. Restart Obsidian and enable **Telegram Sync** in the Community plugins tab

## 📮 Usage
Expand Down Expand Up @@ -91,4 +91,4 @@ If you're thinking about contributing, please check out the [Contributing Guide]
<a href="https://github.com/soberhacker/obsidian-telegram-sync/graphs/contributors">
<img src="https://contrib.rocks/image?repo=soberhacker/obsidian-telegram-sync" />
</a>
</div>
</div>
2 changes: 2 additions & 0 deletions docs/Template Variables List.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{{topic=VALUE}} - messages in topic with name VALUE
{{forwardFrom=VALUE}} - messages forwarded from chat or user with name VALUE
{{content~VALUE}} - messages contain text VALUE
{{voiceTranscript~VALUE}} - voice transcripts contain text VALUE
```

#### Filter examples:
Expand All @@ -26,6 +27,7 @@

```ts
{{content:XX}} - XX characters of the message text
{{voiceTranscript:XX}} - XX characters of voice transcript
{{chat}} - link to the chat (bot | group | channel)
{{chatId}} - id of the chat (bot | group | channel)
{{chat:name}} - name of the chat (bot | group | channel)
Expand Down
13 changes: 11 additions & 2 deletions release-notes.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { compareVersions } from "compare-versions";

export const version = "2.0.0";
// TODO getting messages one by one instead of parallel processing
// TODO add Demo gif and screenshots to readme.md
// ## Demo
// --------F----------
// TODO logging what distribution rule was selected
// TODO getting messages one by one instead of parallel processing
// TODO add possibility to change appending order
// TODO add messagesLeftCnt displaying in status bar
// TODO translating messages
// --------P--------
// TODO getting messages older than 24 hours
// TODO getting messages from other bots in group chats
// TODO post messages in selected chats
// ## Demo gif example
//![](https://raw.githubusercontent.com/vslinko/obsidian-outliner/main/demos/demo1.gif)<br>
export const showNewFeatures = true;
export let showBreakingChanges = true;
Expand Down
1 change: 0 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export default class TelegramSyncPlugin extends Plugin {
lastPollingErrors: string[] = [];
restartingIntervalId?: NodeJS.Timer;
restartingIntervalTime = _15sec;
// TODO: add messagesLeftCnt displaying in status bar
messagesLeftCnt = 0;
connectionStatusIndicator? = new ConnectionStatusIndicator(this);
status: PluginStatus = "loading";
Expand Down
2 changes: 1 addition & 1 deletion src/settings/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export class TelegramSyncSettingTab extends PluginSettingTab {
this.plugin.settings.messageDistributionRules.forEach((rule, index) => {
const setting = new Setting(this.containerEl);
const preElement = document.createElement("pre");
preElement.textContent = "• " + getMessageDistributionRuleDisplayedName(rule);
preElement.textContent = getMessageDistributionRuleDisplayedName(rule);
setting.infoEl.replaceWith(preElement);
setting.settingEl.classList.add("my-custom-list-item");
setting.addExtraButton((btn) => {
Expand Down
7 changes: 4 additions & 3 deletions src/settings/messageDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum ConditionType {
TOPIC = "topic",
FORWARD_FROM = "forwardFrom",
CONTENT = "content",
VOICE_TRANSCRIPT = "voiceTranscript",
}

export enum ConditionOperation {
Expand Down Expand Up @@ -107,11 +108,11 @@ export function extractConditionsFromFilterQuery(messageFilterQuery: string): Me

export function getMessageDistributionRuleDisplayedName(distributionRule: MessageDistributionRule): string {
if (!distributionRule.messageFilterConditions || distributionRule.messageFilterConditions.length == 0)
return "Error! Remove messageDistributionRules from data.json";
return "• error: wrong filter query!";
let displayedName = "";
for (const condition of distributionRule.messageFilterConditions) {
if (condition.conditionType == ConditionType.ALL) return "all messages";
if (condition.conditionType == ConditionType.ALL) return "all messages";
displayedName = displayedName + `${condition.conditionType} ${condition.operation} ${condition.value}; `;
}
return displayedName;
return "• " + (displayedName.length > 50 ? displayedName.slice(0, 50) + "..." : displayedName);
}
21 changes: 17 additions & 4 deletions src/telegram/bot/message/filterEvaluations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import TelegramBot from "node-telegram-bot-api";
import { MessageDistributionRule, MessageFilterCondition, ConditionType } from "src/settings/messageDistribution";
import { getForwardFromName, getTopic } from "./getters";
import TelegramSyncPlugin from "src/main";
import * as Client from "src/telegram/user/client";

export function isUserFiltered(msg: TelegramBot.Message, userNameOrId: string): boolean {
if (!msg?.from || !userNameOrId) return false;
Expand Down Expand Up @@ -46,6 +47,16 @@ export async function isContentFiltered(msg: TelegramBot.Message, substring: str
return (msg.text || msg.caption || "").contains(substring);
}

export async function isVoiceTranscriptFiltered(
plugin: TelegramSyncPlugin,
msg: TelegramBot.Message,
substring: string,
): Promise<boolean> {
let voiceTranscript = "";
if (plugin.bot) voiceTranscript = await Client.transcribeAudio(plugin.bot, msg, await plugin.getBotUser());
return voiceTranscript.contains(substring);
}

export async function isMessageFiltered(
plugin: TelegramSyncPlugin,
msg: TelegramBot.Message,
Expand All @@ -64,17 +75,19 @@ export async function isMessageFiltered(
return await isTopicFiltered(plugin, msg, condition.value);
case ConditionType.CONTENT:
return await isContentFiltered(msg, condition.value);
case ConditionType.VOICE_TRANSCRIPT:
return await isVoiceTranscriptFiltered(plugin, msg, condition.value);
default:
return false;
}
}

export async function doesMessageMatchAllConditions(
export async function doesMessageMatchRule(
plugin: TelegramSyncPlugin,
msg: TelegramBot.Message,
conditions: MessageFilterCondition[],
rule: MessageDistributionRule,
): Promise<boolean> {
for (const condition of conditions) {
for (const condition of rule.messageFilterConditions) {
const isFiltered = await isMessageFiltered(plugin, msg, condition);
if (!isFiltered) return false;
}
Expand All @@ -86,7 +99,7 @@ export async function getMessageDistributionRule(
msg: TelegramBot.Message,
): Promise<MessageDistributionRule | undefined> {
for (const rule of plugin.settings.messageDistributionRules) {
if (await doesMessageMatchAllConditions(plugin, msg, rule.messageFilterConditions)) return rule;
if (await doesMessageMatchRule(plugin, msg, rule)) return rule;
}
return undefined;
}
9 changes: 5 additions & 4 deletions src/telegram/bot/message/processors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ export async function applyNotePathTemplate(
): Promise<string> {
if (!notePathTemplate) return "";

let textContentMd = "";
if (notePathTemplate.includes("{{content")) textContentMd = (await convertMessageTextToMarkdown(msg)).slice(0, 100);

let processedPath = notePathTemplate.endsWith("/") ? notePathTemplate + defaultNoteNameTemplate : notePathTemplate;
let textContentMd = "";
if (processedPath.includes("{{content")) textContentMd = await convertMessageTextToMarkdown(msg);
processedPath = await processBasicVariables(plugin, msg, processedPath, textContentMd);
if (!path.extname(processedPath)) processedPath = processedPath + ".md";
if (processedPath.endsWith(".")) processedPath = processedPath + "md";
return sanitizeFilePath(processedPath);
}

Expand All @@ -161,6 +161,7 @@ export async function applyFilesPathTemplate(
.replace(/{{file:name}}/g, fileName)
.replace(/{{file:extension}}/g, fileExtension);
if (!path.extname(processedPath)) processedPath = processedPath + "." + fileExtension;
if (processedPath.endsWith(".")) processedPath = processedPath + fileExtension;
return sanitizeFilePath(processedPath);
}

Expand All @@ -186,7 +187,7 @@ export async function processBasicVariables(
for (let i = 0; i < lines.length; i++) {
const line = lines[i];

if (line.includes("{{content") && (messageContent || messageText)) {
if (line.includes("{{content")) {
lines[i] = pasteText(plugin, "content", line, messageContent || messageText || "", messageText || "");
}

Expand Down
23 changes: 22 additions & 1 deletion src/utils/fsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function sanitizeFileName(fileName: string): string {

export function sanitizeFilePath(filePath: string): string {
const invalidCharacters = /[\\:*?"<>|\n\r]/g;
return normalizePath(filePath.replace(invalidCharacters, "_"));
return normalizePath(truncatePathComponents(filePath.replace(invalidCharacters, "_")));
}

export async function getUniqueFilePath(
Expand Down Expand Up @@ -98,6 +98,27 @@ export function base64ToString(base64: string): string {
return Buffer.from(base64, "base64").toString("utf-8");
}

export function truncatePathComponents(filePath: string, maxLength = 200): string {
const parsedPath = path.parse(filePath);

// Split the path into its components (folders, subfolders, etc.)
const pathComponents = parsedPath.dir.split("/");

// Truncate each path component if it exceeds maxLength characters
const truncatedComponents = pathComponents.map((component) =>
component.length > maxLength ? component.substring(0, maxLength) : component,
);

// Truncate the file name if it exceeds maxLength characters
const truncatedFileName =
parsedPath.name.length > maxLength ? parsedPath.name.substring(0, maxLength) : parsedPath.name;

// Reassemble the full path
const truncatedPath = path.join(...truncatedComponents, truncatedFileName + parsedPath.ext);

return truncatedPath;
}

export async function replaceMainJs(vault: Vault, mainJs: Buffer | "main-prod.js") {
const mainJsPath = normalizePath(vault.configDir + "/plugins/telegram-sync/main.js");
const mainProdJsPath = normalizePath(vault.configDir + "/plugins/telegram-sync/main-prod.js");
Expand Down

0 comments on commit e507794

Please sign in to comment.