Skip to content

Commit 543ad1f

Browse files
authored
code comments and readme updates (#2)
1 parent f4d59f1 commit 543ad1f

File tree

4 files changed

+44
-14
lines changed

4 files changed

+44
-14
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
# @api3/discord-bot
1+
# api3-telegram-bot
2+
3+
There are two Api3 groups in Telegram. In Telegram a group is sometime called a chat or chat group.
4+
5+
- Api3 - public with invite URL
6+
- Api3 Administrators - private
7+
8+
This bot drives AI processing of Telegram messages. The AI prompt determines if a message violates the Api3 group rules. The rules are publicly available, the AI prompt is not. Other features include keyboard actions in the Api3 Administrators group for the following:
9+
10+
- Timeout 24 hours
11+
- Timeout forever
12+
- Clear timeout
13+
- Restore message
214

315
## Documentation
416

src/actions.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
/*
2-
3-
https://hackage.haskell.org/package/telegram-bot-api-7.4.4/docs/Telegram-Bot-API-Methods-RestrictChatMember.html
4-
Date when restrictions will be lifted for the user, unix time. If user is restricted for more
5-
than 366 days or less than 30 seconds from the current time,
6-
they are considered to be restricted forever.
7-
1+
/**
2+
* Actions (keyboard actions) are only available on a per message basis in the group Api3
3+
* Administrators which is a private group with a few manually added admins.
84
*/
95

106
const { bot } = require('./bot');
@@ -14,7 +10,7 @@ const { PERMISSIONS_TIMEOUT } = require('./permissions');
1410
const CONFIG = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))[process.env.NODE_ENV];
1511

1612
/**
17-
* Timeout 24 hours
13+
* Timeout a user for 24 hours.
1814
* @param {*} bot
1915
*/
2016
async function startActionTimeout24() {
@@ -51,7 +47,7 @@ async function startActionTimeout24() {
5147
}
5248

5349
/**
54-
* Timeout forever
50+
* Timeout a user forever.
5551
* @param {*} bot
5652
*/
5753
async function startActionTimeoutForever() {
@@ -87,7 +83,7 @@ async function startActionTimeoutForever() {
8783
}
8884

8985
/**
90-
* Clear timeout
86+
* Clears any timeout that may have been applied to a user.
9187
* @param {*} bot
9288
*/
9389
async function startActionTimeoutClear() {
@@ -124,7 +120,7 @@ async function startActionTimeoutClear() {
124120
}
125121

126122
/**
127-
* Restore message
123+
* Restore message from disk storage. Any timeout applied to the user is removed.
128124
* @param {*} bot
129125
*/
130126
async function startActionRestoreMessage() {
@@ -165,6 +161,13 @@ async function startActionRestoreMessage() {
165161
});
166162
}
167163

164+
/**
165+
* Gets a message that has been stored on disk for a max of 7.1 days.
166+
* @param {*} ctx
167+
* @param {*} action
168+
* @param {*} msgId
169+
* @returns
170+
*/
168171
async function getMessageFromDisk(ctx, action, msgId) {
169172
try {
170173
return JSON.parse(fs.readFileSync(`../telegram-messages/${msgId}.json`));

src/bot.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* This file creates the bot for other files to use including setting the bot options such
3+
* actions. The bot is exported.
4+
*
5+
* It also purges messages that are stored on disk once they exceed 7.1 days in age.
6+
*/
7+
18
const { Telegraf } = require('telegraf');
29
const fs = require('fs');
310
const path = require('path');
@@ -7,7 +14,7 @@ const CONFIG = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))[process.env
714
let bot = undefined;
815

916
if (!bot) {
10-
// Initialize bot with bot token, bot is used to talk to the Telegram API via Telegraf
17+
// Initialize bot with bot token, bot is used to talk to the Telegram API via Telegraf.
1118
bot = new Telegraf(CONFIG.token);
1219
console.info('The bot is ready.');
1320
}
@@ -36,7 +43,7 @@ setInterval(function () {
3643
} catch (err) {
3744
console.error('Error reading directory:', err);
3845
}
39-
}, 3.6e6); // Executes every hour
46+
}, 3.6e6); // Interval executes every hour
4047

4148
module.exports = {
4249
bot

src/handlers.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
const { chat } = require('./llm');
22
const fs = require('fs');
33

4+
/**
5+
* Returns the opinion of an AI openrouter model for a message passed. The call from here
6+
* continues onto the llm.js file function chat(messages).
7+
*
8+
* Example usage: const returnedArray = await handleMessage(ctx.update.message.text);
9+
* @param {*} message
10+
* @returns
11+
*/
412
const handleMessage = async (message) => {
513
// Get the prompt
614
const prompt = fs.readFileSync('./prompt.txt', 'utf-8');

0 commit comments

Comments
 (0)