Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const { App } = require('jovo-framework');
- const TelegramBot = require("node-telegram-bot-api");
- const mongoose = require("mongoose");
- const config = {
- logging: true,
- };
- const app = new App(config);
- /* database config */
- mongoose.connect('mongodb://localhost/onix_ask');
- const db = mongoose.connection;
- const Schema = mongoose.Schema;
- /*connect to DB*/
- db.on("error", () => console.log("error"));
- db.once("open", () => {
- console.log("connected");
- QuestionAndAnswer.find({}, (error, docs) => {
- if (error) console.log(error);
- console.log(docs);
- })
- });
- /* define new schema and model */
- const userQuestionsAndAnswers = new Schema({
- status: Number,
- questionId: Number,
- messageId: Number,
- questionText: String,
- answerText: String
- });
- let QuestionAndAnswer = mongoose.model("QuestionAndAnswer", userQuestionsAndAnswers);
- /* telegram bot data */
- const token = "598788440:AAGJ5SxFgDhsBoxjyUo2HXm_Sbn6zvdQHQ4";
- const bot = new TelegramBot(token, { polling: true });
- const chatId = -306390448;
- /* handle reply messages from telegram chat and save reply to DB */
- bot.on('message', msg => {
- QuestionAndAnswer.findOneAndUpdate(
- { messageId: msg.reply_to_message.message_id },
- { $set: { status: 1, answerText: msg.text } },
- { new: true },
- (error, docs) => {
- if (error) console.log(error);
- console.log("Updated");
- }
- )
- });
- app.setHandler({
- 'LAUNCH': function () {
- this.toIntent('HelloWorldIntent');
- },
- 'HelloWorldIntent': function () {
- this.ask('Onix opened');
- },
- 'GetAnswersIntent': function () {
- console.log("dialogState:", this.alexaSkill().getDialogState());
- QuestionAndAnswer.count({ status: 1 }, (error, count) => {
- console.log("with answered status:", count);
- if (count === 0) this.tell("You have no answers right now, sorry");
- this.tell(`You have ${count} unread answer`);
- });
- },
- 'CatchAllIntent': function (
- anythinga,
- anythingb,
- anythingc,
- anythingd,
- anythinge,
- anythingf,
- anythingg,
- anythingh,
- anythingi,
- anythingj,
- anythingk,
- anythingl,
- anythingm,
- anythingn,
- anythingo
- ) {
- let anything = "";
- if (anythinga.value !== undefined) anything = anythinga.value;
- if (anythingb.value !== undefined)
- anything = anything + " " + anythingb.value;
- if (anythingc.value !== undefined)
- anything = anything + " " + anythingc.value;
- if (anythingd.value !== undefined)
- anything = anything + " " + anythingd.value;
- if (anythinge.value !== undefined)
- anything = anything + " " + anythinge.value;
- if (anythingf.value !== undefined)
- anything = anything + " " + anythingf.value;
- if (anythingg.value !== undefined)
- anything = anything + " " + anythingg.value;
- if (anythingh.value !== undefined)
- anything = anything + " " + anythingh.value;
- if (anythingi.value !== undefined)
- anything = anything + " " + anythingi.value;
- if (anythingj.value !== undefined)
- anything = anything + " " + anythingj.value;
- if (anythingk.value !== undefined)
- anything = anything + " " + anythingk.value;
- if (anythingl.value !== undefined)
- anything = anything + " " + anythingl.value;
- if (anythingm.value !== undefined)
- anything = anything + " " + anythingm.value;
- if (anythingn.value !== undefined)
- anything = anything + " " + anythingn.value;
- if (anythingo.value !== undefined)
- anything = anything + " " + anythingo.value;
- bot.sendMessage(chatId, anything)
- .then(msg => {
- QuestionAndAnswer.count({}, (error, count) => {
- if (error) console.error(error);
- console.log("count:", count);
- let dataToDataBase = new QuestionAndAnswer({
- status: 0,
- questionId: ++count,
- messageId: msg.message_id,
- questionText: anything,
- });
- dataToDataBase.save(error => {
- if (error) console.log(error);
- });
- this.tell("Your message has been sent");
- });
- });
- // this.tell("your words", anything);
- }
- });
- module.exports.app = app;
Add Comment
Please, Sign In to add comment