Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ Also note that:

## Configuration

### Basic Configuration
Basic Configuration is very Easy. Just follow these Steps:
1. Send /start to your Bot
2. Send /settings to yout Bot an do the Basic Configuration

### Advanced Configuration

1. Copy `TeleFrame/config/config.example.json` to `TeleFrame/config/config.json`. \
**Note:** If you used the installer script. This step is already done for you.

Expand Down
3 changes: 3 additions & 0 deletions config/i18n/bot/de.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ var i18n = {
whitelistInfo: 'Hallo, dieser Bot benötigt eine Freischaltung. Bitte definiere deine Chat-Id in der Konfigurationsdatei',
whitelistAdminInfo: 'Hallo, die Administrierung dieses Bots benötigt eine Freischaltung. Bitte definiere deine Chat-Id in der Konfigurationsdatei',
hiReply: (name, chatId) => `Hallo ${name} \nDeine ChatID ist ${chatId}`,
hiAdminPrivateReply: (first_name, last_name, chatId) => `Hey Admin! \n${first_name} ${last_name} möchte seinen Chat mit der ID ${chatId} auf die Whitelist gesetzt bekommen.`,
hiAdminGroupReply: (first_name, last_name, groupName, chatId) => `Hey Admin! \n${first_name} ${last_name} möchte die Gruppe '${groupName}' mit der ID ${chatId} auf die Whitelist gesetzt bekommen.`,

// Text der ausgegeben wird, wenn das empfangene Dokument ein nicht unterstütztes Dateiformat hat
documentFormatError: 'Dieses Dokument hat ein unbekanntes Dateiformat.',
};
Expand Down
6 changes: 6 additions & 0 deletions config/i18n/bot/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ var i18n = {
// reply to 'hi' requires additional parameters - use a function
hiReply: (name, chatId) => `Hey there ${name} \nYour ChatID is ${chatId}`,

// reply for admin on welcome message from user
hiAdminPrivateReply: (first_name, last_name, chatId) => `Hey Admin! \n${first_name} ${last_name} wants to add his Chat with the ID ${chatId}.`,

// reply for admin on welcome message from user in a group
hiAdminGroupReply: (first_name, last_name, groupName, chatId) => `Hey Admin! \n${first_name} ${last_name} wants to add the Group '${groupName}' with ID ${chatId}.`,

// The error message if the received document has unknown format
documentFormatError: 'This document has an unknown format.',

Expand Down
41 changes: 33 additions & 8 deletions js/bot.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const Telegraf = require("telegraf");
const Telegram = require("telegraf/telegram");
const Extra = require('telegraf/extra')
const {MenuMiddleware} = require('telegraf-inline-menu')
const download = require("image-downloader");
const moment = require("moment");
const exec = require("child_process").exec;
const fs = require(`fs`);
const botReply = require('./botReply');
const botSendMessage = require('./botSendMessage');
const botConfigMenu = require('./botConfigMenu');

var Bot = class {
constructor(
Expand All @@ -29,7 +32,11 @@ var Bot = class {
});

//Welcome message on bot start
this.bot.start((ctx) => botReply(ctx, 'welcome'));
this.bot.start((ctx) => this.welcomeUser(ctx));

//Some small conversation
this.bot.hears(/^hi/i, (ctx) => this.welcomeUser(ctx));


//Help message
this.bot.help((ctx) => botReply(ctx, 'help'));
Expand Down Expand Up @@ -151,13 +158,6 @@ var Bot = class {
this.logger.error(err.stack);
});

//Some small conversation
this.bot.hears(/^hi/i, (ctx) => {
botReply(ctx, 'hiReply', ctx.chat.first_name, ctx.chat.id);
this.logger.info(ctx.chat);
});


//Add Admin Actions from config to Bot-Command
if(this.config.adminAction.allowAdminAction ){
var actions = this.config.adminAction.actions;
Expand Down Expand Up @@ -188,6 +188,11 @@ var Bot = class {

}

//Menu
const menuMiddleware = new MenuMiddleware('/', botConfigMenu)
this.bot.command('settings', isAdminWhitelisted, async ctx => menuMiddleware.replyToContext(ctx))
this.bot.use(menuMiddleware.middleware())

this.logger.info("Bot created!");
}

Expand All @@ -212,6 +217,26 @@ var Bot = class {
return this.bot.telegram.sendMessage(config.whitelistChats[0], message);
}

sendMessageToAdmin(ctx, constant, ...args) {
// function to send messages, used for informing Admin
config.whitelistAdmins.forEach(element => {
this.logger.info(element);
botSendMessage(this.bot.telegram, ctx, constant, element, ...args)
});
}

welcomeUser(ctx){
this.logger.info("User send start command to Bot!");

if(ctx.chat.type=='private'){
botReply(ctx, 'hiReply', ctx.chat.first_name, ctx.chat.id);
this.sendMessageToAdmin(ctx, "hiAdminPrivateReply", ctx.chat.first_name, ctx.chat.last_name, ctx.chat.id);
}else if(ctx.chat.type=='group'){
botReply(ctx, 'hiReply', ctx.chat.title, ctx.chat.id);
this.sendMessageToAdmin(ctx, "hiAdminGroupReply", ctx.from.first_name, ctx.from.last_name, ctx.chat.title, ctx.chat.id);
}
}

sendAudio(filename, chatId, messageId) {
// function to send recorded audio as voice reply
fs.readFile(
Expand Down
140 changes: 140 additions & 0 deletions js/botConfigMenu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
const langDefault = 'en';
const langPath = __dirname + '/../config/i18n/bot/';
const botPhrases = {};
const { logger, rendererLogger } = require("./logger");

const {MenuTemplate, MenuMiddleware, deleteMenuFromContext} = require('telegraf-inline-menu')

botPhrases[langDefault] = require(langPath + langDefault);


const menu = new MenuTemplate(() => 'Configuration-Menu');

menu.toggle('whitelistChats', 'whitelistChats', {
set: (_, newState) => {
logger.info("Config-Change for whitelistChats:");
if (newState) {
logger.info("New whitelistChats: "+config.whitelistAdmins);
config.whitelistChats = config.whitelistAdmins;
} else {
logger.info("WhitelistChats is now empty");
config.whitelistChats = [];
}
config.writeConfig();
// Update the menu afterwards
return true
},
isSet: () => config.whitelistChats.length > 0,
joinLastRow: false
})


menu.toggle('botReply', 'botReply', {
set: (_, newState) => {
config.botReply = newState;
config.writeConfig();
logger.info("Config-Change for botReply: "+newState);
// Update the menu afterwards
return true
},
isSet: () => config.botReply,
joinLastRow: false
})

menu.toggle('showVideos', 'showVideos', {
set: (_, newState) => {
config.showVideos = newState;
config.writeConfig();
logger.info("Config-Change for showVideos: "+newState);
// Update the menu afterwards
return true
},
isSet: () => config.showVideos,
joinLastRow: true
})

menu.toggle('playVideoAudio', 'playVideoAudio', {
set: (_, newState) => {
config.playVideoAudio = newState;
config.writeConfig();
logger.info("Config-Change for playVideoAudio: "+newState);
// Update the menu afterwards
return true
},
isSet: () => config.playVideoAudio,
joinLastRow: false
})

menu.toggle('randomOrder', 'randomOrder', {
set: (_, newState) => {
config.randomOrder = newState;
config.writeConfig();
logger.info("Config-Change for randomOrder: "+newState);
// Update the menu afterwards
return true
},
isSet: () => config.randomOrder,
joinLastRow: true
})

menu.toggle('autoDeleteImages', 'autoDeleteImages', {
set: (_, newState) => {
config.autoDeleteImages = newState;
config.writeConfig();
logger.info("Config-Change for autoDeleteImages: "+newState);
// Update the menu afterwards
return true
},
isSet: () => config.autoDeleteImages,
joinLastRow: false
})

menu.toggle('showSender', 'showSender', {
set: (_, newState) => {
config.showSender = newState;
config.writeConfig();
logger.info("Config-Change for showSender: "+newState);
// Update the menu afterwards
return true
},
isSet: () => config.showSender,
joinLastRow: true
})

menu.toggle('showCaption', 'showCaption', {
set: (_, newState) => {
config.showCaption = newState;
config.writeConfig();
logger.info("Config-Change for showCaption: "+newState);
// Update the menu afterwards
return true
},
isSet: () => config.showCaption,
joinLastRow: false
})

menu.toggle('useFullscreenForCaptionAndSender', 'useFullscreenForCaptionAndSender', {
set: (_, newState) => {
config.useFullscreenForCaptionAndSender = newState;
config.writeConfig();
logger.info("Config-Change for useFullscreenForCaptionAndSender: "+newState);
// Update the menu afterwards
return true
},
isSet: () => config.useFullscreenForCaptionAndSender,
joinLastRow: true
})

menu.interact('Exit', 'unique', {
do: async context => {
await deleteMenuFromContext(context);
return false;
}
})



/*************** DO NOT EDIT THE LINE BELOW ***************/
if (typeof module !== "undefined") {
module.exports = menu;
}
36 changes: 36 additions & 0 deletions js/botSendMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const langDefault = 'en';
const langPath = __dirname + '/../config/i18n/bot/';
const botPhrases = {};

botPhrases[langDefault] = require(langPath + langDefault);

/**
* Replies using the phrase for the sender's language
* @param {Object} telegram Telegraf Object
* @param {Object} ctx telegraf request
* @param {string} constant The constant to use
* @param {int} chatId The ChatId to send to
* @param {array} args [optional] Additional arguments to pass when the constant returns a function
*/
const botSendMessage = (telegram, ctx, constant, chatId, ...args) => {
const langSender = ctx.from.language_code.substr(0, 2).toLowerCase();
if (!botPhrases[langSender]) {
try {
botPhrases[langSender] = Object.assign({}, botPhrases[langDefault], require(langPath + langSender));
} catch (e) {
// language file does'nt exist. Reference default language
botPhrases[langSender] = botPhrases[langDefault];
}
}
// Whenthe phrase is a function, it is called and the additional arguments are passed
if (typeof botPhrases[langSender][constant] === 'function') {
telegram.sendMessage(chatId, botPhrases[langSender][constant](...args))
} else {
telegram.sendMessage(chatId, botPhrases[langSender][constant])
}
};

/*************** DO NOT EDIT THE LINE BELOW ***************/
if (typeof module !== "undefined") {
module.exports = botSendMessage;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"sweetalert": "^2.1.2",
"sweetalert2": "^9.5.4",
"telegraf": "^3.35.0",
"telegraf-inline-menu": "^5.3.0",
"velocity-animate": "^1.5.2",
"winston": "^3.2.1"
}
Expand Down