Skip to content

Instantly share code, notes, and snippets.

@hube12
Created October 2, 2018 21:54
Show Gist options
  • Save hube12/e9906c7930deab4ce207f14c9957d3b3 to your computer and use it in GitHub Desktop.
Save hube12/e9906c7930deab4ce207f14c9957d3b3 to your computer and use it in GitHub Desktop.
quick amz tlg bot
process.env.NTBA_FIX_319 = 1;
const TelegramBot = require('node-telegram-bot-api');
const rp = require('request-promise');
const cheerio = require('cheerio');
// replace the value below with the Telegram token you receive from @BotFather
const token = 'my awesome token';
// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, {
polling: true
});
// Matches "/amazon [item]"
bot.onText(/\/amz (.+)/, (msg, match) => {
const chatId = msg.chat.id;
const resp = match[1];
const start = "https://www.amazon.fr/s?field-keywords="
const options = {
uri: start + resp,
transform: function (body) {
return cheerio.load(body);
}
};
rp(options)
.then(($) => {
var firstResult = $('li[id="result_0"]')
var image = firstResult.find("img").attr("src")
var price = firstResult.find('span[class="a-size-base a-color-price s-price a-text-bold"]').text()
var name = firstResult.find('h2').text()
bot.sendPhoto(chatId, image, {
caption: "You searched for: " + resp + " \n which gave this item: " + name.substring(0, 40) + "...\n at the formidable price of " + price,
"reply_markup": {
"keyboard": [["Too cheap", "Too expensive"], ["Lame"], ["I want to buy it"]]
}
});
})
.catch((err) => {
console.log(err);
});
console.log(resp,msg.from.first_name)
});
// Listen for any kind of message. There are different kinds of
// messages.
bot.on('message', (msg) => {
const chatId = msg.chat.id;
// send a message to the chat acknowledging receipt of their message
bot.sendMessage(chatId, 'Received your message');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment