Guest User

Untitled

a guest
May 20th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const { App } = require('jovo-framework');
  4. const TelegramBot = require("node-telegram-bot-api");
  5. const mongoose = require("mongoose");
  6.  
  7. const config = {
  8. logging: true,
  9. };
  10.  
  11. const app = new App(config);
  12.  
  13. /* database config */
  14. mongoose.connect('mongodb://localhost/onix_ask');
  15. const db = mongoose.connection;
  16. const Schema = mongoose.Schema;
  17.  
  18. /*connect to DB*/
  19.  
  20. db.on("error", () => console.log("error"));
  21. db.once("open", () => {
  22. console.log("connected");
  23. QuestionAndAnswer.find({}, (error, docs) => {
  24. if (error) console.log(error);
  25. console.log(docs);
  26. })
  27. });
  28.  
  29.  
  30. /* define new schema and model */
  31. const userQuestionsAndAnswers = new Schema({
  32. status: Number,
  33. questionId: Number,
  34. messageId: Number,
  35. questionText: String,
  36. answerText: String
  37. });
  38.  
  39. let QuestionAndAnswer = mongoose.model("QuestionAndAnswer", userQuestionsAndAnswers);
  40.  
  41. /* telegram bot data */
  42. const token = "598788440:AAGJ5SxFgDhsBoxjyUo2HXm_Sbn6zvdQHQ4";
  43. const bot = new TelegramBot(token, { polling: true });
  44. const chatId = -306390448;
  45.  
  46. /* handle reply messages from telegram chat and save reply to DB */
  47. bot.on('message', msg => {
  48. QuestionAndAnswer.findOneAndUpdate(
  49. { messageId: msg.reply_to_message.message_id },
  50. { $set: { status: 1, answerText: msg.text } },
  51. { new: true },
  52. (error, docs) => {
  53. if (error) console.log(error);
  54. console.log("Updated");
  55. }
  56. )
  57. });
  58.  
  59. app.setHandler({
  60. 'LAUNCH': function () {
  61. this.toIntent('HelloWorldIntent');
  62. },
  63. 'HelloWorldIntent': function () {
  64. this.ask('Onix opened');
  65. },
  66. 'GetAnswersIntent': function () {
  67. console.log("dialogState:", this.alexaSkill().getDialogState());
  68. QuestionAndAnswer.count({ status: 1 }, (error, count) => {
  69. console.log("with answered status:", count);
  70. if (count === 0) this.tell("You have no answers right now, sorry");
  71. this.tell(`You have ${count} unread answer`);
  72. });
  73. },
  74. 'CatchAllIntent': function (
  75. anythinga,
  76. anythingb,
  77. anythingc,
  78. anythingd,
  79. anythinge,
  80. anythingf,
  81. anythingg,
  82. anythingh,
  83. anythingi,
  84. anythingj,
  85. anythingk,
  86. anythingl,
  87. anythingm,
  88. anythingn,
  89. anythingo
  90. ) {
  91. let anything = "";
  92. if (anythinga.value !== undefined) anything = anythinga.value;
  93. if (anythingb.value !== undefined)
  94. anything = anything + " " + anythingb.value;
  95. if (anythingc.value !== undefined)
  96. anything = anything + " " + anythingc.value;
  97. if (anythingd.value !== undefined)
  98. anything = anything + " " + anythingd.value;
  99. if (anythinge.value !== undefined)
  100. anything = anything + " " + anythinge.value;
  101. if (anythingf.value !== undefined)
  102. anything = anything + " " + anythingf.value;
  103. if (anythingg.value !== undefined)
  104. anything = anything + " " + anythingg.value;
  105. if (anythingh.value !== undefined)
  106. anything = anything + " " + anythingh.value;
  107. if (anythingi.value !== undefined)
  108. anything = anything + " " + anythingi.value;
  109. if (anythingj.value !== undefined)
  110. anything = anything + " " + anythingj.value;
  111. if (anythingk.value !== undefined)
  112. anything = anything + " " + anythingk.value;
  113. if (anythingl.value !== undefined)
  114. anything = anything + " " + anythingl.value;
  115. if (anythingm.value !== undefined)
  116. anything = anything + " " + anythingm.value;
  117. if (anythingn.value !== undefined)
  118. anything = anything + " " + anythingn.value;
  119. if (anythingo.value !== undefined)
  120. anything = anything + " " + anythingo.value;
  121.  
  122. bot.sendMessage(chatId, anything)
  123. .then(msg => {
  124. QuestionAndAnswer.count({}, (error, count) => {
  125. if (error) console.error(error);
  126. console.log("count:", count);
  127. let dataToDataBase = new QuestionAndAnswer({
  128. status: 0,
  129. questionId: ++count,
  130. messageId: msg.message_id,
  131. questionText: anything,
  132. });
  133. dataToDataBase.save(error => {
  134. if (error) console.log(error);
  135. });
  136. this.tell("Your message has been sent");
  137. });
  138. });
  139. // this.tell("your words", anything);
  140. }
  141. });
  142.  
  143.  
  144.  
  145. module.exports.app = app;
Add Comment
Please, Sign In to add comment