Advertisement
makeeval

Untitled

Jul 4th, 2024
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getUserProperty(userId) {
  2.   const userStatus = {
  3.     mode: 'awaiting',
  4.     obj: ''
  5.   };
  6.  
  7.   const propertyName = `user_${userId}`
  8.  
  9.   const scriptProperties = PropertiesService.getScriptProperties();
  10.   let userProperty = scriptProperties.getProperty(propertyName);
  11.  
  12.   if (!userProperty) {
  13.     scriptProperties.setProperty(propertyName, JSON.stringify(userStatus));
  14.     userProperty = JSON.stringify(userStatus); // Установите userProperty для дальнейшего использования
  15.   }
  16.  
  17.   let property;
  18.   try {
  19.     property = JSON.parse(userProperty);
  20.   } catch (e) {
  21.     property = null;
  22.   }
  23.  
  24.   return property;
  25. }
  26.  
  27. function setUserProperty(userId, newMode, newObj) {
  28.   const propertyName = `user_${userId}`
  29.   const scriptProperties = PropertiesService.getScriptProperties();
  30.   let userProperty = scriptProperties.getProperty(propertyName);
  31.  
  32.   if (!userProperty) {
  33.     throw new Error(`User property for userId ${propertyName} does not exist.`);
  34.   }
  35.  
  36.   let property;
  37.   try {
  38.     property = JSON.parse(userProperty);
  39.   } catch (e) {
  40.     throw new Error(`Failed to parse user property for userId ${propertyName}.`);
  41.   }
  42.  
  43.   if (newMode !== undefined) {
  44.     property.mode = newMode;
  45.   }
  46.  
  47.   if (newObj !== undefined) {
  48.     property.obj = newObj;
  49.   }
  50.  
  51.   scriptProperties.setProperty(propertyName, JSON.stringify(property));
  52. }
  53.  
  54.  
  55.  
  56. ....
  57.  if (msg.chat.type === 'private') {
  58.  
  59.         const userStatus = getUserProperty(chat_id)
  60.  
  61.         if (text == "/start") {
  62.  
  63.           setUserProperty(chat_id, 'awaiting', "")
  64.  
  65.           Bot.sendMessage({
  66.             chat_id: chat_id,
  67.             text: "Привет! Начнём?",
  68.             reply_markup: {
  69.               keyboard: [
  70.                 [{ text: 'НАЧАТЬ сбор фотографий' }],
  71.               ],
  72.               resize_keyboard: true,
  73.               one_time_keyboard: true
  74.             }
  75.           });
  76.         }
  77.  
  78.         if (text == "НАЧАТЬ сбор фотографий" && userStatus.mode == 'awaiting') {
  79.  
  80.           setUserProperty(chat_id, 'started', "")
  81.  
  82.           Bot.sendMessage({
  83.             chat_id: chat_id,
  84.             text: "Хорошо. Режим сбора фотографий запущен.\n\n<b>Введи идентификацию объекта в формате '<i>номер(имя) город</i>'</b>",
  85.             reply_markup: {
  86.               keyboard: [
  87.                 [{ text: 'ЗАВЕРШИТЬ сбор фотографий' }],
  88.               ],
  89.               resize_keyboard: true,
  90.               one_time_keyboard: true
  91.             }
  92.           });
  93.         }
  94.  
  95.         if (text && userStatus.mode == 'started') {
  96.  
  97.           setUserProperty(chat_id, 'active', text)
  98.  
  99.           Bot.sendMessage({
  100.             chat_id: chat_id,
  101.             text: "Отлично. Можно приступать к работе. Начинай отправлять фото, а по завершении нажми кноку <i>ЗАВЕРШИТЬ сбор фотографий</i> внизу чата",
  102.             reply_markup: {
  103.               keyboard: [
  104.                 [{ text: 'ЗАВЕРШИТЬ сбор фотографий' }],
  105.               ],
  106.               resize_keyboard: true,
  107.               one_time_keyboard: true
  108.             }
  109.           });
  110.         }
  111.  
  112.         if ((contents.message?.photo || contents.message?.document) && userStatus.mode == 'active') {
  113.  
  114.           try {
  115.             let objectName = userStatus.obj !== "" ? userStatus.obj : chatTitle;
  116.             let url = msg.photo ? saveFileToDrive(msg.photo[msg.photo.length - 1].file_id, getFolderId(objectName)) : saveFileToDrive(msg.document.file_id, getFolderId(objectName));
  117.  
  118.             /* let response = Bot.sendMessage({
  119.              chat_id: chat_id,
  120.              text: 'Сохранил 👍',
  121.              disable_notification: true,
  122.              reply_to_message_id: msg.message_id,
  123.              disable_web_page_preview: true
  124.            })  */
  125.  
  126.             ss.getSheetByName('WorkLog').appendRow([new Date(), url, '', msg.chat.title, msg.from.first_name]);
  127.           } catch (err) {
  128.             betterLog(err, '667yyy');
  129.           }
  130.  
  131.  
  132.         };
  133.  
  134.         if (text == "ЗАВЕРШИТЬ сбор фотографий") {
  135.  
  136.           setUserProperty(chat_id, 'awaiting', "")
  137.  
  138.           Bot.sendMessage({
  139.             chat_id: chat_id,
  140.             text: "Режим сбора фотографий отключен.",
  141.             reply_markup: {
  142.               keyboard: [
  143.                 [{ text: 'НАЧАТЬ сбор фотографий' }],
  144.               ],
  145.               resize_keyboard: true,
  146.               one_time_keyboard: true
  147.             }
  148.           });
  149.         }
  150.       }
  151. ....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement