Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. const config = require('../config.js');
  2. const cache = require('./cache.js');
  3.  
  4. function ticketHandler(bot, ctx) {
  5. ctx.getChat().then(function(chat) {
  6. if (chat.id.toString() === config.staffchat_id) {
  7. // let staff handle that
  8. staffChat(ctx, bot, chat);
  9. } else if (chat.type === 'private') {
  10. // create a ticket and send to staff
  11. customerChat(ctx, bot, chat);
  12. }
  13. });
  14. }
  15.  
  16. // reply to tickets in staff chat
  17. function staffChat(ctx, bot) {
  18. // check whether person is an admin
  19. ctx.getChatAdministrators()
  20. .then(function(admins) {
  21. admins = JSON.stringify(admins);
  22. let replyText;
  23. if (
  24. ctx.message.reply_to_message !== undefined &&
  25. admins.indexOf(ctx.from.id) > -1
  26. ) {
  27. // try whether a text or an image/video is replied to
  28. try {
  29. replyText = ctx.message.reply_to_message.text;
  30. if (replyText === undefined) {
  31. replyText = ctx.message.reply_to_message.caption;
  32. }
  33. let userid = replyText.match(new RegExp('#' + '(.*)' + ' ' + config.lang_from));
  34. if (userid === null || userid === undefined) {
  35. userid = replyText.match(new RegExp('#' + '(.*)' + '\n' + config.lang_from));
  36. }
  37. let name = replyText.match(new RegExp(config.lang_from + ' ' + '(.*)' + ' @'));
  38. if (ctx.message.text !== undefined && ctx.message.text === 'me') {
  39. // accept ticket
  40. bot.telegram.sendMessage(
  41. config.staffchat_id,
  42. '<b>' +
  43. config.lang_ticket +
  44. ' #' +
  45. userid[1] +
  46. '</b> ' +
  47. config.lang_acceptedBy +
  48. ' ' +
  49. ctx.message.from.first_name +
  50. ' -> /open',
  51. cache.noSound
  52. );
  53. } else {
  54. cache.ticketStatus[userid[1]] = false;
  55. bot.telegram.sendMessage(
  56. userid[1],
  57. config.lang_dear +
  58. ' <b>' +
  59. name[1] +
  60. '</b>,\n\n' +
  61. ctx.message.text +
  62. '\n\n' +
  63. config.lang_regards +
  64. '\n' +
  65. ctx.message.from.first_name,
  66. cache.html
  67. );
  68. console.log(
  69. 'Answer: ' +
  70. config.lang_ticket +
  71. ' #' +
  72. cache.tickedID +
  73. ' ' +
  74. config.lang_dear +
  75. ' ' +
  76. name[1] +
  77. ' ' +
  78. ctx.message.text +
  79. ' ' +
  80. config.lang_from +
  81. ' ' +
  82. ctx.message.from.first_name
  83. );
  84. }
  85. cache.ticketSent[cache.tickedID] = undefined;
  86. } catch (e) {
  87. console.log(e)
  88. bot.telegram.sendMessage(
  89. config.staffchat_id, 'An error occured, please report this to your admin: \n\n' + e,
  90. cache.noSound
  91. );
  92. }
  93. }
  94. })
  95. .catch(function(noAdmin) {
  96. console.log('Error with admins: ' + noAdmin);
  97. });
  98. }
  99.  
  100. function customerChat(ctx, bot, chat) {
  101. var bannedUsers = [ // ticket numbers of users to be banned
  102. '907093210', // banned user 1
  103. '422589730', // banned user 2
  104. '521741214' // banned user 3
  105. ];
  106. if (
  107. JSON.stringify(bannedUsers).indexOf(ctx.from.id) > -1
  108. ) {
  109.  
  110. cache.tickedID = ctx.message.from.id;
  111. if (cache.ticketIDs[cache.ticketID] === undefined) {
  112. cache.ticketIDs.push(cache.tickedID);
  113. }
  114. cache.ticketStatus[cache.tickedID] = true;
  115. userInfo = '';
  116. userInfo +=
  117. '</b> ' + config.lang_from + ' ' + ctx.message.from.first_name + ' ';
  118. userInfo +=
  119. '@' +
  120. ctx.message.from.username +
  121. ' ' +
  122. config.lang_language +
  123. ': ' +
  124. ctx.message.from.language_code +
  125. '\n\n';
  126. if (cache.ticketSent[cache.tickedID] === undefined) {
  127. bot.telegram.sendMessage(chat.id, config.lang_contactMessage);
  128. bot.telegram.sendMessage(
  129. config.staffchat_id,
  130. '<b>' +
  131. config.lang_ticket +
  132. ' #' +
  133. cache.tickedID +
  134. userInfo +
  135. ctx.message.text,
  136. cache.html
  137. );
  138. // wait 5 minutes before this message appears again and do not
  139. // send notificatoin sounds in that time to avoid spam
  140. setTimeout(function() {
  141. cache.ticketSent[cache.tickedID] = undefined;
  142. }, config.spam_time);
  143. cache.ticketSent[cache.tickedID] = 0;
  144. } else if (cache.ticketSent[cache.tickedID] < 5) {
  145. cache.ticketSent[cache.tickedID]++;
  146. bot.telegram.sendMessage(
  147. config.staffchat_id,
  148. '<b>' +
  149. config.lang_ticket +
  150. ' #' +
  151. cache.tickedID +
  152. userInfo +
  153. ctx.message.text,
  154. cache.noSound
  155. );
  156. } else if (cache.ticketSent[cache.tickedID] === 5) {
  157. cache.ticketSent[cache.tickedID]++;
  158. bot.telegram.sendMessage(chat.id, config.lang_blockedSpam);
  159. }
  160. console.log(
  161. 'Ticket: ' +
  162. ' #' +
  163. cache.tickedID +
  164. userInfo.replace('\n\n', ': ') +
  165. ctx.message.text
  166. );
  167.  
  168. }
  169. }
  170.  
  171. function videoHandler(bot, ctx) {
  172. forwardFile(bot, ctx, function(userInfo) {
  173. bot.telegram.sendVideo(config.staffchat_id, ctx.message.video.file_id, {
  174. caption:
  175. config.lang_ticket +
  176. ': #' +
  177. cache.ticketID +
  178. '\n' +
  179. userInfo +
  180. '\n' +
  181. (ctx.message.caption || ''),
  182. });
  183. });
  184. }
  185.  
  186. function photoHandler(bot, ctx) {
  187. forwardFile(bot, ctx, function(userInfo) {
  188. bot.telegram.sendPhoto(config.staffchat_id, ctx.message.photo[0].file_id, {
  189. caption:
  190. config.lang_ticket +
  191. ': #' +
  192. cache.ticketID +
  193. '\n' +
  194. userInfo +
  195. '\n' +
  196. (ctx.message.caption || ''),
  197. });
  198. });
  199. }
  200.  
  201. function documentHandler(bot, ctx) {
  202. forwardFile(bot, ctx, function(userInfo) {
  203. bot.telegram.sendDocument(
  204. config.staffchat_id,
  205. ctx.message.document.file_id,
  206. {
  207. caption:
  208. config.lang_ticket +
  209. ': #' +
  210. cache.ticketID +
  211. '\n' +
  212. userInfo +
  213. (ctx.message.caption || ''),
  214. }
  215. );
  216. });
  217. }
  218.  
  219. function forwardFile(bot, ctx, callback) {
  220. if (cache.ticketSent[cache.tickedID] === undefined) {
  221. fowardHandler(ctx, function(userInfo) {
  222. callback(userInfo);
  223. });
  224. // wait 5 minutes before this message appears again and do not
  225. // send notificatoin sounds in that time to avoid spam
  226. setTimeout(function() {
  227. cache.ticketSent[cache.tickedID] = undefined;
  228. }, config.spam_time);
  229. cache.ticketSent[cache.tickedID] = 0;
  230. } else if (cache.ticketSent[cache.tickedID] < 5) {
  231. cache.ticketSent[cache.tickedID]++;
  232. // TODO: add cache.noSound property for silent notifications
  233. fowardHandler(ctx, function(userInfo) {
  234. callback(userInfo);
  235. });
  236. } else if (cache.ticketSent[cache.tickedID] === 5) {
  237. cache.ticketSent[cache.tickedID]++;
  238. bot.telegram.sendMessage(chat.id, config.lang_blockedSpam);
  239. }
  240. }
  241.  
  242. function fowardHandler(ctx, callback) {
  243. ctx.getChat().then(function(chat) {
  244. if (chat.type === 'private') {
  245. cache.ticketID = ctx.message.from.id;
  246. userInfo = '';
  247. userInfo += config.lang_from + ' ' + ctx.message.from.first_name + ' ';
  248. userInfo +=
  249. '@' +
  250. ctx.message.from.username +
  251. ' ' +
  252. config.lang_language +
  253. ': ' +
  254. ctx.message.from.language_code +
  255. '\n\n';
  256. callback(userInfo);
  257. }
  258. });
  259. }
  260.  
  261. module.exports = {
  262. ticket: ticketHandler,
  263. photo: photoHandler,
  264. video: videoHandler,
  265. document: documentHandler,
  266. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement