Guest User

Untitled

a guest
May 13th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const TOKEN = "top secret";
  2.  
  3. const Telega = require("./build/index");
  4. const assert = require("assert");
  5.  
  6. function equal(a, b) {
  7.     var result;
  8.  
  9.     try {
  10.         assert.equal(a, b);
  11.         result = null;
  12.     } catch(e) {
  13.         result = e;
  14.     }
  15.  
  16.     return result;
  17. }
  18.  
  19. var bot;
  20.  
  21. if (!TOKEN) {
  22.     console.log("You should create a dedicated bot and put token to TOKEN const");
  23.     throw "No token";
  24. } else {
  25.     bot = new Telega(TOKEN);
  26.     bot.start();
  27. }
  28.  
  29. console.log("Actions that you need to preform yourself will start with >>>");
  30.  
  31. describe("Messages", function() {
  32.     it("Command receiving", function(next) {
  33.         console.log(">>> Write to bot command /test");
  34.  
  35.         bot.cmd("/test", function(message) {
  36.             next();
  37.  
  38.             bot.cmd("/test", undefined);
  39.         });
  40.     });
  41.  
  42.     it("Answers", function(next) {
  43.         console.log(">>> Write to bot command /ans");
  44.  
  45.         bot.cmd("/ans", function(message) {
  46.             const N = Math.random() + "";
  47.  
  48.             message.send("Now write " + N);
  49.             message.answer(function(message) {
  50.                 bot.cmd("/ans", undefined);
  51.  
  52.                 next(equal(N, message.text));
  53.             });
  54.         });
  55.     });
  56.  
  57.     it("Inline", function(next) {
  58.         console.log(">>> Write to bot command /inline");
  59.  
  60.         bot.cmd("/inline", function(message) {
  61.             message.inline("Click on 3:", [
  62.                 {
  63.                     text: "1",
  64.                     data: "d_1",
  65.                     row: 0
  66.                 },
  67.                 {
  68.                     text: "2",
  69.                     data: "d_2",
  70.                     row: 1
  71.                 },
  72.                 {
  73.                     text: "3",
  74.                     data: "d_3",
  75.                     row: 2
  76.                 }
  77.             ], function(message) {
  78.                 assert.equal("d_3", message.data);
  79.  
  80.                 next();
  81.  
  82.                 bot.cmd("/inline", undefined);
  83.             });
  84.         });
  85.     });
  86.  
  87.     it("Matches", function(next) {
  88.         console.log(">>> Write to bot 'hello world'");
  89.  
  90.         bot.match(/^(hello world)$/i, function(message) {
  91.             bot._matches = [];
  92.  
  93.             next(equal("hello world", message.text));
  94.         });
  95.     });
  96. });
  97.  
  98. describe("Advanced", function() {
  99.     it("Middlewares", function(_next) {
  100.         var order = [];
  101.  
  102.         console.log(">>> Write any message to bot once");
  103.  
  104.         bot.use(function(message, next) {
  105.             order.push(1);
  106.             setTimeout(() => next(), 1000);
  107.         });
  108.  
  109.         bot.use(function(message, next) {
  110.             order.push(2);
  111.             setTimeout(() => next(), 1000);
  112.         });
  113.  
  114.         bot.use(function(message, next) {
  115.             order.push(3);
  116.             setTimeout(() => next(), 1000);
  117.  
  118.             _next(equal("123", order.join("")));
  119.         });
  120.     });
  121.  
  122.     it("Action handling", function(next) {
  123.         console.log(">>> Create a group chat with bot");
  124.  
  125.         bot.action("group_chat_created", function(message) {
  126.             next(
  127.                 equal("group_chat_created", message.action_type),
  128.                 equal(true, !!message.action_data)
  129.             );
  130.  
  131.             bot.action("group_chat_created", undefined);
  132.         });
  133.     });
  134. });
Advertisement
Add Comment
Please, Sign In to add comment