Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const TelegramBot = require('node-telegram-bot-api');
  2. const mongoose = require('mongoose');
  3. const User = require('./models/User')
  4. const utils = require("./modules/utils");
  5. mongoose.connect('mongodb://localhost/test' , {
  6.     useNewUrlParser: true,
  7.     useUnifiedTopology: true
  8.   })
  9.     .then(() => {
  10.         console.log('mongo db connected');
  11.     })
  12.     .catch(error => console.log(error));
  13. const token = 'токен';
  14.  
  15.  
  16. const bot = new TelegramBot(token, {polling: true});
  17.  
  18.  
  19. bot.onText(/\/echo (.+)/, (msg, match) => {
  20.  
  21.   const chatId = msg.chat.id;
  22.   const resp = match[1];
  23.  
  24.  
  25.   bot.sendMessage(chatId, resp);
  26. });
  27.  
  28.  
  29. bot.on('message', async (msg) => {
  30.   const chatId = msg.chat.id;
  31.   const count = await User.countDocuments();
  32.   var fromId = msg.from.id
  33.   console.log('[+] Runned /start command')
  34.   User.find({id: fromId}, (err, user) => {
  35.     if (err) throw err
  36.     if (user.length === 0) {
  37.       const newUser = new User({
  38. id: msg.from.id,
  39. uid: count + 1,
  40. balance: 0,
  41. username: msg.from.username,
  42. firstname: msg.from.first_name,
  43. lastname: msg.from.last_name,
  44. createdAt: getUnix(),
  45. gender: 0,
  46. chats: 0,
  47. active: 'Не задано',
  48. active1: 0,
  49. vip: false,
  50. admin: false,
  51. premium: false,
  52. notifications: true,
  53. ban: false,
  54. free: 0,
  55. free2: 0,
  56. free3: 0,
  57. free4: 0,
  58. free5: 0,
  59. free6: 0,
  60. free7: 0,
  61. free8: 0,
  62. free9: 0,
  63. boolean: false,
  64. boolean1: false,
  65. boolean2: false,
  66. boolean3: false,
  67. boolean4: false,
  68. boolean5: false,
  69. boolean6: false,
  70. boolean7: false,
  71. boolean8: false,
  72. boolean9: false,
  73. string: 'string',
  74. string1: 'string',
  75. string2: 'string',
  76. string3: 'string',
  77. string4: 'string',
  78. string5: 'string',
  79. string6: 'string',
  80. string7: 'string',
  81. string8: 'string',
  82. string9: 'string'
  83.  
  84.       })
  85.      
  86.       newUser.save((err) => {
  87.         if (err) throw err
  88.         bot.sendMessage(msg.chat.id, `Привет ${msg.from.first_name}, спасибо что зарегистрировались в нашем боте! \n Наш бот находится в разработке, поэтому Вы пока что не сможете найти тут собеседников. Прошу Вас пока что присоединиться к нашему телеграм каналу, чтобы следить за новостями - t.me/RuAnon \n Как только бот будет готов, мы обо всём там напишем и Вы сможете общаться 😏 \n По всем вопросам - @superman_da`);
  89.       })
  90.    
  91.     }
  92.   })
  93. })
  94. bot.on('sticker', function(msg) {
  95.     sendX('sticker', msg);
  96. });
  97.  
  98. bot.on('audio', function(msg) {
  99.     sendX('audio', msg);
  100. });
  101.  
  102. bot.on('document', function(msg) {
  103.     sendX('document', msg);
  104. });
  105.  
  106. bot.on('photo', function(msg) {
  107.     sendX('photo', msg);
  108. });
  109.  
  110. bot.on('video', function(msg) {
  111.     sendX('video', msg);
  112. });
  113.  
  114. bot.on('voice', function(msg) {
  115.     sendX('voice', msg);
  116. });
  117.  
  118. bot.on('contact', function(msg) {
  119.     sendX('contact', msg);
  120. });
  121.  
  122. bot.on('location', function(msg) {
  123.     sendX('location', msg);
  124. });
  125.  
  126.  
  127.  
  128. bot.onText(/\профиль/, async msg => {
  129.     var fromId = msg.from.id
  130.     let muser = await User.findOne({id: fromId})
  131.    
  132.  
  133.     bot.sendMessage(msg.chat.id,`
  134.     🔎 ID: ${muser.uid}
  135.    
  136.     Дата регистрации:  ${unixStamp(muser.createdAt)}`)
  137.  
  138.   })
  139.  
  140.   bot.onText(/\/start/, async msg => {
  141.     var fromId = msg.from.id
  142.     let muser = await User.findOne({id: fromId})
  143.    
  144.  
  145.     bot.sendMessage(msg.chat.id,` ${muser.firstname}, Вы уже зарегестрированы!`)
  146.  
  147.   })
  148.  
  149.   bot.onText(/\/sharelink/, async msg => {
  150.      
  151.     var fromId = msg.from.id
  152.     let muser = await User.findOne({
  153.         id: fromId
  154.     })
  155.     console.log(`${muser.id}`)
  156.     let id = msg.chat.id;
  157.     let message = `[Вот ссылка на Ваш аккаунт](tg://user?id=${muser.id})`
  158.     console.log(message)
  159.     return bot.sendMessage(id, message, {parse_mode: 'Markdown'})
  160. })
  161.  
  162. bot.onText(/\стат/, async msg => {
  163.     var fromId = msg.from.id
  164.     let muser = await User.findOne({
  165.         id: fromId
  166.     })
  167.     await muser.set("free", msg.from.id)
  168.     let _users = await User.countDocuments();
  169.     bot.sendMessage(msg.chat.id, `🙎‍♂️ Пользователей: ${_users}`)
  170.     bot.sendMessage(msg.chat.id, `🙎‍♂️ ${muser.free}`)
  171. })
  172.  
  173. bot.onText(/\/send/, async msg => {
  174.     var fromId = msg.from.id
  175.     let muser = await User.findOne({
  176.         id: fromId
  177.     })
  178.     await muser.set("free", msg.from.id)
  179.    
  180.  
  181.     bot.sendMessage(msg.chat.id, `айди ${muser.free}
  182. айди в боте ${muser.uid} `)
  183. })
  184.  
  185.  
  186. bot.onText(/\/search/, async msg => {
  187.     var fromId = msg.from.id
  188.     let muser = await User.findOne({
  189.         id: fromId
  190.     })
  191.     let users = await User.find({
  192.         free: {
  193.             $gte
  194.         }
  195.     });
  196.    
  197.  
  198.     let user2 = users[utils.random(0, users.length - 1)]
  199.  
  200.     let user1 = await User.findOne({
  201.         id: msg.chat.id
  202.     });
  203.     sendX('Собеседник найден!')
  204.     await muser.set("free", msg.from.id)
  205. });
  206.  
  207.  
  208. console.log('[START]')
  209.  
  210. User.prototype.inc = function(field, value) {
  211.     this[field] += value;
  212.     return this.save();
  213. }
  214.  
  215. User.prototype.dec = function(field, value) {
  216.     this[field] -= value;
  217.     return this.save();
  218. }
  219.  
  220. User.prototype.set = function(field, value) {
  221.     this[field] = value;
  222.     return this.save();
  223. }
  224. function flipString(string) {
  225.     let result = "";
  226.     string = string.toLowerCase().split("").reverse();
  227.     string.map((symbol) => {
  228.         result += flipTable[symbol] || symbol;
  229.     });
  230.     return result;
  231. }
  232.  
  233.  
  234. function time() {
  235.     let date = new Date();
  236.     let days = date.getDate();
  237.     let hours = date.getHours();
  238.     let minutes = date.getMinutes();
  239.     let seconds = date.getSeconds();
  240.     if (hours < 10) hours = "0" + hours;
  241.     if (minutes < 10) minutes = "0" + minutes;
  242.     if (seconds < 10) seconds = "0" + seconds;
  243.     var times = hours + ':' + minutes + ':' + seconds
  244.     return times;
  245. }
  246. //------------------------------------------------------------------------------------\\
  247. function date() {
  248.     var date = new Date();
  249.     let days = date.getDate();
  250.     let month = date.getMonth() + 1;
  251.     let year = date.getFullYear()
  252.     if (month < 10) month = "0" + month;
  253.     if (days < 10) days = "0" + days;
  254.     var datas = days + '.' + month + '.' + year;
  255.     return datas;
  256. }
  257.  
  258. function timeConverter(UNIX_timestamp) {
  259.     let a = new Date(UNIX_timestamp * 1000);
  260.     let months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
  261.     let year = a.getFullYear();
  262.     let month = months[a.getMonth()];
  263.     let date = a.getDate();
  264.     let hour = a.getHours();
  265.     let min = a.getMinutes();
  266.     let sec = a.getSeconds();
  267.     if (hour < 10) hour = `0${hour}`;
  268.     if (min < 10) min = `0${min}`;
  269.     if (sec < 10) sec = `0${sec}`;
  270.     if (date < 10) date = `0${date}`;
  271.     let read_time = `${hour}:${min}:${sec}`;
  272.     let read_date = date + '.' + month + '.' + year;
  273.     let time_date = date + '.' + month + '.' + year + ' | ' + hour + ':' + min + ':' + sec;
  274.     let time = {
  275.         moment: time_date,
  276.         date: read_date,
  277.         time: read_time,
  278.         years: year,
  279.         months: month,
  280.         number: date,
  281.         hours: hour,
  282.         minutes: min,
  283.         secunds: sec
  284.     }
  285.     return time;
  286. }
  287.  
  288. function getUnix() {
  289.     return Date.now();
  290. }
  291.  
  292. function unixStamp(stamp) {
  293.     let a = new Date(stamp);
  294.     let months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
  295.     let year = a.getFullYear();
  296.     let month = months[a.getMonth()];
  297.     let date = a.getDate();
  298.     let hour = a.getHours();
  299.     let min = a.getMinutes();
  300.     let sec = a.getSeconds();
  301.     if (hour < 10) hour = `0${hour}`;
  302.     if (min < 10) min = `0${min}`;
  303.     if (sec < 10) sec = `0${sec}`;
  304.     if (date < 10) date = `0${date}`;
  305.     let read_time = `${hour}:${min}:${sec}`;
  306.     let read_date = date + '.' + month + '.' + year;
  307.     let time_date = date + '.' + month + '.' + year + ' , ' + hour + ':' + min + ':' + sec;
  308.     return time_date;
  309. }
  310.  
  311. function unixStampLeft(stamp) {
  312.     stamp = stamp / 1000;
  313.  
  314.     let s = stamp % 60;
  315.     stamp = (stamp - s) / 60;
  316.  
  317.     let m = stamp % 60;
  318.     stamp = (stamp - m) / 60;
  319.  
  320.     let h = (stamp) % 24;
  321.     let d = (stamp - h) / 24;
  322.  
  323.     let text = ``;
  324.  
  325.     if (d > 0) text += Math.floor(d) + " д. ";
  326.     if (h > 0) text += Math.floor(h) + " ч. ";
  327.     if (m > 0) text += Math.floor(m) + " мин. ";
  328.     if (s > 0) text += Math.floor(s) + " с.";
  329.  
  330.     return text;
  331. }                
  332.  
  333. async function sendX(string, msg){
  334.  
  335.     let users = await User.find({
  336.         free
  337.     });
  338.     var currentChatId = msg.chat.id;
  339.     var partnerChatId = users.free;
  340.  
  341.     if(partnerChatId == undefined)
  342.         if(includes.inAnObject(msg.chat.id)){
  343.             bot.sendMessage(currentChatId, "нет пользователей");
  344.             return;
  345.         } else {
  346.             bot.sendMessage(currentChatId,"ура dialog начат" );
  347.             return;
  348.         }
  349.  
  350.     switch(string){
  351.         case 'text':
  352.             bot.sendMessage(partnerChatId, msg.text);
  353.             break;
  354.         case 'sticker':
  355.             bot.sendSticker(partnerChatId, msg.sticker.file_id);
  356.             break;
  357.         case 'audio':
  358.             bot.sendAudio(partnerChatId, msg.audio.file_id);
  359.             break;
  360.         case 'document':
  361.             bot.sendDocument(partnerChatId, msg.document.file_id);
  362.             break;
  363.         case 'photo':
  364.             bot.sendPhoto(partnerChatId, msg.photo[msg.photo.length - 1].file_id, {caption: msg.caption});
  365.             break;
  366.         case 'video':
  367.             bot.sendVideo(partnerChatId, msg.video.file_id, {caption: msg.caption});
  368.             break;
  369.         case 'voice':
  370.             bot.sendVoice(partnerChatId, msg.voice.file_id);
  371.             break;
  372.         case 'contact':
  373.             bot.sendContact(partnerChatId, msg.contact.phone_number, msg.contact.first_name);
  374.             break;
  375.         case 'location':
  376.             bot.sendLocation(partnerChatId, msg.location.latitude, msg.location.longitude);
  377.             break;
  378.     }
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement