Skip to content

Commit

Permalink
Merge pull request #23 from LukeSkywalker92/voice-reply-to-chat
Browse files Browse the repository at this point in the history
Voice reply to chat
  • Loading branch information
LukeSkywalker92 authored Jul 3, 2019
2 parents ca2bef7 + 62cf9d4 commit c0b6e4c
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 100 deletions.
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

### Automatic Installation (Raspberry Pi only!)

*Electron*, the app wrapper around Teleframe, only supports the Raspberry Pi 2/3. The Raspberry Pi 0/1 is currently **not** supported.
*Electron*, the app wrapper around Teleframe, only supports the Raspberry Pi 2/3/4. The Raspberry Pi 0/1 is currently **not** supported.

Note that you will need to install the lastest full version of Raspbian, **don't use the Lite version**.

Expand All @@ -40,10 +40,11 @@ bash -c "$(curl -sL https://raw.githubusercontent.com/LukeSkywalker92/TeleFrame/
### Manual Installation

1. Download and install the latest *Node.js* version.
2. Install *Electron* globally with `npm install -g electron`.
3. Clone the repository and check out the master branch: `git clone https://github.com/LukeSkywalker92/TeleFrame.git`
4. Enter the repository: `cd TeleFrame/`
5. Install and run the app with: `npm install && npm start`
2. If you like to use the voice reply feature you need to install sox
3. Install *Electron* globally with `npm install -g electron`.
4. Clone the repository and check out the master branch: `git clone https://github.com/LukeSkywalker92/TeleFrame.git`
5. Enter the repository: `cd TeleFrame/`
6. Install and run the app with: `npm install && npm start`

Also note that:

Expand Down Expand Up @@ -92,18 +93,18 @@ When you start your TeleFrame and send a "Hi" to the bot it will send you back t

## Voice Replies using TeleFrame

A very simple way to respond to the images is by using TeleFrame`s voice reply feature. The feature is intended to work like this: Who ever comes by the frame presses a button, speaks their message into the frame, when there is 2 seconds of silence or the maximum time is reached the recording will stop and the telegram bot will send it to all chat IDs set in the configuration.
A very simple way to respond to the images is by using TeleFrame`s voice reply feature. The feature is intended to work like this: Who ever comes by the frame presses a button, speaks their message into the frame, when there is 2 seconds of silence or the maximum time is reached the recording will stop and the telegram bot will send it to the chat where the current image came from.


| **Option** | **Description** |
| ----------------------- | -------------------------------------------------------------------------------------- |
| `key` | The keyboardkey to start the voice recording |
| `sendTo` | Array of chat IDs that recieve the voice message |
| `maxRecordTime` | How long the recorder will record if there is no silence detected (in milliseconds) |
| `recordingMessageTitle` | The title of the recording dialog displayed on the frame during record |
| `recordingMessage` | The message of the recording dialog displayed on the frame during record |
| `recordingDone` | The message of the recording dialog displayed on the frame when recording has finished |
| `recordingError` | The error message of the recording dialog displayed when recording has failed |
| **Option** | **Description** |
| ----------------------- | ----------------------------------------------------------------------------------------- |
| `key` | The keyboardkey to start the voice recording |
| `maxRecordTime` | How long the recorder will record if there is no silence detected (in milliseconds) |
| `recordingMessageTitle` | The title of the recording dialog displayed on the frame during record |
| `recordingPreMessage` | The message of the recording dialog displayed on the frame during record before chat name |
| `recordingPostMessage` | The message of the recording dialog displayed on the frame during record after char name |
| `recordingDone` | The message of the recording dialog displayed on the frame when recording has finished |
| `recordingError` | The error message of the recording dialog displayed when recording has failed |


## Updating
Expand Down
4 changes: 2 additions & 2 deletions config/config.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ var config = {
},
voiceReply: {
key: "a",
sendTo: [],
maxRecordTime: 60*1000,
recordingMessageTitle: "Voice Message",
recordingMessage: "Recording in progress...",
recordingPreMessage: "Recording for",
recordingPostMessage: "in progress...",
recordingDone: "Voice message sent sucessfully!",
recordingError: "Voice message has failed!"
}
Expand Down
73 changes: 43 additions & 30 deletions js/bot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Telegraf = require("telegraf");
const Telegram = require("telegraf/telegram");
const Extra = require('telegraf/extra')
const download = require("image-downloader");
const moment = require("moment");

Expand Down Expand Up @@ -44,14 +45,14 @@ var Bot = class {
if (
!(
this.whitelistChats.length > 0 &&
this.whitelistChats.indexOf(ctx.update.message.from.id) !== -1
this.whitelistChats.indexOf(ctx.message.chat.id) !== -1
)
) {
console.log(
this.logger.info(
"Whitelist triggered:",
ctx.update.message.from.id,
ctx.message.chat.id,
this.whitelistChats,
this.whitelistChats.indexOf(ctx.update.message.from.id)
this.whitelistChats.indexOf(ctx.message.chat.id)
);
ctx.reply(
"Hey there, this bot is whitelisted, pls add your chat id to the config file"
Expand All @@ -68,10 +69,19 @@ var Bot = class {
dest: this.imageFolder + "/" + moment().format("x") + ".jpg"
})
.then(({ filename, image }) => {
var chatName = ''
if (ctx.message.chat.type == 'group') {
chatName = ctx.message.chat.title;
} else if (ctx.message.chat.type == 'private') {
chatName = ctx.message.from.first_name;
}
this.newImage(
filename,
ctx.message.from.first_name,
ctx.message.caption
ctx.message.caption,
ctx.message.chat.id,
chatName,
ctx.message.message_id
);
})
.catch((err) => {
Expand All @@ -85,14 +95,14 @@ var Bot = class {
if (
!(
this.whitelistChats.length > 0 &&
this.whitelistChats.indexOf(ctx.update.message.from.id) !== -1
this.whitelistChats.indexOf(ctx.message.chat.id) !== -1
)
) {
console.log(
this.logger.info(
"Whitelist triggered:",
ctx.update.message.from.id,
ctx.message.chat.id,
this.whitelistChats,
this.whitelistChats.indexOf(ctx.update.message.from.id)
this.whitelistChats.indexOf(ctx.message.chat.id)
);
ctx.reply(
"Hey there, this bot is whitelisted, pls add your chat id to the config file"
Expand All @@ -108,10 +118,19 @@ var Bot = class {
dest: this.imageFolder + "/" + moment().format("x") + ".mp4"
})
.then(({ filename, image }) => {
var chatName = ''
if (ctx.message.chat.type == 'group') {
chatName = ctx.message.chat.title;
} else if (ctx.message.chat.type == 'private') {
chatName = ctx.message.from.first_name;
}
this.newImage(
filename,
ctx.message.from.first_name,
ctx.message.caption
ctx.message.caption,
ctx.message.chat.id,
chatName,
ctx.message.message_id
);
})
.catch((err) => {
Expand All @@ -130,7 +149,7 @@ var Bot = class {
ctx.reply(
`Hey there ${ctx.chat.first_name} \n Your ChatID is ${ctx.chat.id}`
);
console.log(ctx.chat);
this.logger.info(ctx.chat);
});

this.logger.info("Bot created!");
Expand All @@ -143,46 +162,40 @@ var Bot = class {
setTimeout(() => self.startBot(), 30000)
);
this.logger.info("Bot started!");
/*
this.sendMessage("Bot ready!")
.then(() => {
console.log("success");
})
.catch((err) => {
console.log("error", err);
});
*/
}

newImage(src, sender, caption) {
newImage(src, sender, caption, chatId, chatName, messageId) {
//tell imageWatchdog that a new image arrived
this.imageWatchdog.newImage(src, sender, caption);
this.imageWatchdog.newImage(src, sender, caption, chatId, chatName, messageId);
}

sendMessage(message) {
// function to send messages, used for whitlist handling
return this.bot.telegram.sendMessage(this.whitelistChats[0], message);
}

sendAudio(filename) {
sendAudio(filename, chatId, messageId) {
// function to send recorded audio as voice reply
fs.readFile(
filename,
function(err, data) {
if (err) {
console.log(err);
this.logger.error(err);
return;
}
for (let i = 0; i < this.voiceReply.sendTo.length; i++) {
this.bot.telegram
.sendVoice(this.voiceReply.sendTo[i], {
this.telegram
.sendVoice(chatId, {
source: data
}, {
reply_to_message_id: messageId
})
.then(() => {
console.log("success");
this.logger.info("success");
})
.catch((err) => {
console.log("error", err);
this.logger.error("error", err);
});
}

}.bind(this)
);
}
Expand Down
9 changes: 7 additions & 2 deletions js/imageWatchdog.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ var ImageWatchdog = class {
}
}

newImage(src, sender, caption) {
newImage(src, sender, caption, chatId, chatName, messageId) {
//handle new incoming image
// TODO: message ID and chat name to reply to specific image and to show
// chat name for voice recording message
this.images.unshift({
'src': src,
'sender': sender,
'caption': caption
'caption': caption,
'chatId': chatId,
'chatName': chatName,
'messageId': messageId
});
if (this.images.length >= this.imageCount) {
this.images.pop();
Expand Down
Loading

0 comments on commit c0b6e4c

Please sign in to comment.