var telegram = require('telegram-bot-api');
var find = require('find');
var imageList = []
find.file(/\.(jpe?g|png|gif)$/i, process.cwd(), function(files) {
imageList = files.filter((name)=>{
return !name.match(/[\/|\\]node_modules[\//|\\]/);
});
console.log(imageList.length);
})
var api = new telegram({
token: '239742497:AAGJGHPf7bzOg9w0L675wgrCSGTVVL-axSs',
updates: {
enabled: true
}
});
api.on('update', function(update) {
console.log(update)
})
api.on('message', function(message) {
console.log(message)
if (!message) {
return;
}
if (message.from.id === 163910093/*'SooXiaoTong'*/) return;
if (!message.text) return;
if (message.text.match(/^\/plz_give_me_some_image(@|$)/)) {
if (imageList.length === 0) return;
var fileIndex = (imageList.length * Math.random()) | 0
var chosonPath = imageList[fileIndex];
console.log('start uploading ' + fileIndex + ' ' + chosonPath)
api.sendChatAction({
chat_id: message.chat.id,
action: "upload_photo"
})
.then(function(data)
{
console.log(data);
})
.catch(function(err)
{
console.log(err.message || err.stack);
});
api.sendPhoto({
chat_id: message.chat.id,
caption: 'Random file ' + fileIndex,
reply_to_message_id: message.message_id,
// you can also send file_id here as string (as described in telegram bot api documentation)
photo: chosonPath,
reply_markup: JSON.stringify({
inline_keyboard: [
[
{
text: 'Download Full file',
callback_data: message.chat.id + '|' + message.from.id + '|' + fileIndex + '|' + message.message_id
}
]
]
})
})
.then(function(data)
{
console.log(data);
})
.catch(function(err)
{
console.log(err.message || err.stack);
});
}
});
api.on('inline.callback.query', function(message) {
// New incoming callback query
console.log('inline-query', message);
if (!message.data || !message.data.match(/^-?\d+\|-?\d+\|\d+\|\d+$/)) {
return;
}
var temp = message.data.split(/\|/g);
temp = temp.map(i=>parseInt(i, 10));
var chatId = temp[0];
var fileIndex = temp[2];
if (fileIndex >= imageList.length) return;
chosonPath = imageList[fileIndex];
api.sendDocument({
chat_id: message.from.id,
caption: 'File ' + fileIndex,
// you can also send file_id here as string (as described in telegram bot api documentation)
document: chosonPath
})
.then(function(data) {
console.log(data);
})
.catch(function(err) {
console.log(err.message || err.stack);
return api.getMe({})
.then(function (botInfo) {
api.sendMessage({
chat_id: chatId,
text: '@' + message.from.username + ', please press start to me, otherwise i can\'t message to you',
// reply_to_message_id: messageId,
reply_markup: JSON.stringify({
inline_keyboard: [
[
{
text: 'Start Me',
url: "
http://telegram.me/" + botInfo.username
}
]
]
})
})
})
.catch((err)=>console.log(err.message || err.stack))
});
});
api.on('error', function (err) {
console.error(err.message || err.stack)
})