-
-
Save chepecarlos/49e5381cc278b7236fc3677f78c0e210 to your computer and use it in GitHub Desktop.
Codigo de raulV92 de Bot de Navidad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Creado por https://github.com/raulV92 | |
// version de proyecto bot telegram de ALSW | |
// | |
/* | |
funcionamiento: | |
se tienen 3 botones conectados a las entradas analogicas A2,A3,A4 | |
y 3 leds a los pines 2,3,4 del arduino (Nano) | |
al presionar un boton se prende o apaga el led del pin correspondiente | |
y el bot manda un mensaje confirmando que recibio la orden del boton | |
el bot tiene los comandos "/boton_#" que simulan haber presionado un boton | |
el comando /status manda un mensaje informando si cada led esta prendido o apagado | |
*/ | |
const fs = require('fs'); | |
const five = require('johnny-five'); | |
const Telegraf = require('telegraf'); | |
// datos y creacion del Bot: | |
const tkn = 'token del bot'; | |
const chatId = 'xxxxxxxxxx'; | |
var bot = new Telegraf(tkn); | |
// datos e init de la placa (Arduino Nano) | |
var entradas_analogicas = ["A2", "A3", "A4"]; | |
var salidas_digitales = [2, 3, 4]; | |
var botones = []; // aqui se crean las instancias que reciben senal de los botones | |
var salidas = []; // aqui se crea la instancia que controla el HIGH o LOW de las salidas digitales | |
var nano = new five.Board(); | |
nano.on("ready", nano_init); | |
function nano_init() { | |
for (let i = 0; i < entradas_analogicas.length; i++) { | |
botones.push(new five.Pin(entradas_analogicas[i])); | |
salidas.push(new five.Pin(salidas_digitales[i])); | |
} | |
ciclo(); // simula funcion loop del arduino | |
} | |
function ciclo() { | |
lee_botones(); | |
prende_o_apaga(); | |
setTimeout(ciclo, 400); // define el tiempo en que se repite la funcion "loop" si fuera arduino | |
} | |
function lee_botones() { | |
for (let i = 0; i < entradas_analogicas.length; i++) { | |
if (botones[i].value > 10) { | |
bot.telegram.sendMessage(chatId, `boton ${i+2} presionado!`) | |
focos[i].presiona_boton(); | |
} | |
} | |
} | |
function prende_o_apaga() { | |
for (let i = 0; i < focos.length; i++) { | |
if (focos[i].estado) { | |
salidas[i].high(); | |
} else { | |
salidas[i].low(); | |
} | |
} | |
} | |
// clase de Foco donde cada instancia guarda la informacion de cada led | |
// se llama al metodo esta instancia para prender o apagar el foco ya sea por el boton o por comando de telegram | |
class Foquito { | |
constructor(numero_boton) { | |
this.numero = numero_boton; | |
this.estado = false; | |
} | |
presiona_boton() { | |
if (this.estado) { | |
this.estado = false; | |
} else if (!this.estado) { | |
this.estado = true | |
} | |
} | |
} | |
var focos = []; | |
for (let i = 0; i < entradas_analogicas.length; i++) { | |
focos.push(new Foquito(i + 2)); | |
} | |
// commandos del bot: | |
bot.start((ctx) => { | |
ctx.reply(`Hola ${ctx.chat.first_name}! presiona en "help" para ver comandos disponobles \n /help`); | |
}) | |
bot.help((ctx) => { | |
let ayuda = fs.readFileSync('bot_help.txt').toString(); //escribi la ayuda en un .txt | |
ctx.reply(ayuda); | |
}) | |
bot.command('/status', (ctx) => { | |
var a_msj = { | |
true: "prendido", | |
false: "apagado" | |
} | |
var mensaje_completo = ''; | |
for (let i = 0; i < focos.length; i++) { | |
mensaje_completo = mensaje_completo + `El foco ${focos[i].numero} esta ${a_msj[focos[i].estado]}` + '\n'; | |
} | |
ctx.reply(mensaje_completo) | |
}) | |
bot.command('/boton_2', (ctx) => { | |
ctx.reply('Orden recibida!') | |
focos[0].presiona_boton(); | |
}) | |
bot.command('/boton_3', (ctx) => { | |
ctx.reply('Orden recibida!') | |
focos[1].presiona_boton(); | |
}) | |
bot.command('/boton_4', (ctx) => { | |
ctx.reply('Orden recibida!') | |
focos[2].presiona_boton(); | |
}) | |
bot.launch(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment