)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Chat (Ia.Ftn.Cl.Models.Telegram)

Public general use code classes and xml files that we've compiled and used over the years:

Telegram Chat/Group/User support class of Fixed Telecommunications Network (FTN) business and data model.

    1: using System;
    2: using System.Collections.Generic;
    3: using System.Configuration;
    4: using System.Data;
    5: using System.Linq;
    6: using Telegram.Bot;
    7: using Telegram.Bot.Types.Enums;
    8:  
    9: namespace Ia.Ftn.Cl.Models.Telegram
   10: {
   11:     ////////////////////////////////////////////////////////////////////////////
   12:  
   13:     /// <summary publish="true">
   14:     /// Telegram Chat/Group/User support class of Fixed Telecommunications Network (FTN) business and data model.
   15:     /// </summary>
   16:     /// 
   17:     /// <remarks> 
   18:     /// Copyright © 2018-2024 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   19:     ///
   20:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   21:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   22:     ///
   23:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   24:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   25:     /// 
   26:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   27:     /// 
   28:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   29:     /// </remarks> 
   30:     public class Chat
   31:     {
   32:         private static List<int> usedImageIdList = new List<int>();
   33:         private static List<Ia.Ftn.Cl.Models.Telegram.Chat> chatList;
   34:  
   35:         private static readonly object objectLock = new object();
   36:  
   37:         /// <summary/>
   38:         public enum ChatType { Unknown, PrivateGroup, PrivateChannel, PublicGroup, PublicChannel }
   39:  
   40:         /// <summary/>
   41:         public int ImageId { get; set; }
   42:  
   43:         /// <summary/>
   44:         public string NameId { get; set; }
   45:  
   46:         /// <summary/>
   47:         public long ChatId { get; set; }
   48:  
   49:         /// <summary/>
   50:         public string Name { get; set; }
   51:  
   52:         /// <summary/>
   53:         public string Description { get; set; }
   54:  
   55:         /// <summary/>
   56:         public Ia.Ftn.Cl.Models.Telegram.Chat.ChatType Type { get; set; }
   57:  
   58:         /// <summary/>
   59:         public bool RestrictedByFrameworkSite { get; set; }
   60:  
   61:         /// <summary/>
   62:         public string InviteLinkUrl { get { return "https://t.me/joinchat/" + ChatNameIdToInviteLinkUrlDictionary[this.NameId]; } }
   63:  
   64:         /// <summary/>
   65:         public Ia.Ftn.Cl.Models.Business.Administration.Framework Framework { get; set; }
   66:  
   67:         /// <summary/>
   68:         public string TelegramGroupUrl { get { return "https://web.telegram.org/#/im?p=g" + System.Math.Abs(this.ChatId) + ""; } }
   69:  
   70:         /// <summary/>
   71:         public string TelegramLogoImageUrl { get { return "/image/telegram/logo.png"; } }
   72:  
   73:         /// <summary/>
   74:         public string TelegramLogoHtmlImageUrl { get { return TelegramLogoImageUrl.Replace("", "../.."); ; } }
   75:  
   76:         /// <summary/>
   77:         public static string FtnUserBotApiToken { get { return Ia.Cl.Models.ApplicationConfiguration.GetSetting("AppSettings:FtnUserBotApiToken"); } }
   78:  
   79:         /// <summary/>
   80:         public static readonly string FtnUserBotNameId = "ftnUserBot"; // logo 32.png
   81:  
   82:         ////////////////////////////////////////////////////////////////////////////
   83:  
   84:         /// <summary>
   85:         ///
   86:         /// </summary>
   87:         public Chat() { }
   88:  
   89:         ////////////////////////////////////////////////////////////////////////////
   90:  
   91:         /// <summary>
   92:         ///
   93:         /// <see cref="https://stackoverflow.com/questions/9311857/convert-alphabetic-string-into-integer-in-c-sharp"/>
   94:         /// </summary>
   95:         private static long ConvertAlphabeticStringIntoLong(string line)
   96:         {
   97:             long l;
   98:             System.Text.Encoding ascii;
   99:             Byte[] encodedBytes;
  100:  
  101:             l = 0;
  102:             ascii = System.Text.Encoding.ASCII;
  103:             encodedBytes = ascii.GetBytes(line);
  104:  
  105:             foreach (Byte b in encodedBytes) l += b;
  106:  
  107:             return l;
  108:         }
  109:  
  110:         ////////////////////////////////////////////////////////////////////////////
  111:  
  112:         /// <summary>
  113:         ///
  114:         /// </summary>
  115:         public string LogoImageUrl
  116:         {
  117:             get
  118:             {
  119:                 return "/image/telegram/ftth-logo/" + this.ImageId + ".png";
  120:             }
  121:         }
  122:  
  123:         ////////////////////////////////////////////////////////////////////////////
  124:  
  125:         /// <summary>
  126:         ///
  127:         /// </summary>
  128:         public string LogoHtmlImageUrl
  129:         {
  130:             get
  131:             {
  132:                 return "/image/telegram/ftth-logo/" + this.ImageId + ".png";
  133:             }
  134:         }
  135:  
  136:         ////////////////////////////////////////////////////////////////////////////
  137:  
  138:         /// <summary>
  139:         ///
  140:         /// </summary>
  141:         public string LogoImageAbsolutePathUrl
  142:         {
  143:             get
  144:             {
  145:                 return Ia.Cl.Models.Default.AbsolutePath() + @"image\telegram\ftth-logo\" + this.ImageId + ".png";
  146:             }
  147:         }
  148:  
  149:         ////////////////////////////////////////////////////////////////////////////
  150:  
  151:         /// <summary>
  152:         ///
  153:         /// </summary>
  154:         public static Ia.Ftn.Cl.Models.Telegram.Chat ReadFromFramework(Ia.Ftn.Cl.Models.Business.Administration.Framework framework)
  155:         {
  156:             var chat = (from t in Ia.Ftn.Cl.Models.Telegram.Chat.ChatList
  157:                         where t.Framework == framework
  158:                         select t).SingleOrDefault();
  159:  
  160:             return chat;
  161:         }
  162:  
  163:         ////////////////////////////////////////////////////////////////////////////
  164:  
  165:         /// <summary>
  166:         ///
  167:         /// </summary>
  168:         public static long ReadChatIdFromNameId(string nameId)
  169:         {
  170:             long chatId;
  171:  
  172:             chatId = ChatNameIdToChatIdDictionary.ContainsKey(nameId) ? ChatNameIdToChatIdDictionary[nameId] : 0;
  173:  
  174:             // chatId = (from c in List where c.NameId == nameId select c.ChatId).SingleOrDefault();
  175:  
  176:             return chatId;
  177:         }
  178:  
  179:         ////////////////////////////////////////////////////////////////////////////
  180:  
  181:         /// <summary>
  182:         ///
  183:         /// </summary>
  184:         public static Ia.Ftn.Cl.Models.Telegram.Chat Read(long chatId)
  185:         {
  186:             var chat = (from c in Ia.Ftn.Cl.Models.Telegram.Chat.ChatList where c.ChatId == chatId select c).SingleOrDefault();
  187:  
  188:             if (chat == null)
  189:             {
  190:                 chat = new Chat() { Type = ChatType.Unknown };
  191:             }
  192:  
  193:             return chat;
  194:         }
  195:  
  196:         ////////////////////////////////////////////////////////////////////////////
  197:  
  198:         /// <summary>
  199:         ///
  200:         /// </summary>
  201:         public static List<Ia.Ftn.Cl.Models.Telegram.Chat> ChatList
  202:         {
  203:             get
  204:             {
  205:                 if (chatList == null || chatList.Count == 0)
  206:                 {
  207:                     lock (objectLock)
  208:                     {
  209:                         chatList = Ia.Ftn.Cl.Models.Telegram.Chat._ChatList();
  210:                     }
  211:                 }
  212:  
  213:                 return chatList;
  214:             }
  215:         }
  216:  
  217:         ////////////////////////////////////////////////////////////////////////////
  218:  
  219:         /// <summary>
  220:         ///
  221:         /// </summary>
  222:         private static List<Ia.Ftn.Cl.Models.Telegram.Chat> _ChatList()
  223:         {
  224:             long chatId;
  225:             string nameId;
  226:             Ia.Ftn.Cl.Models.Telegram.Chat chat;
  227:             Ia.Ftn.Cl.Models.Business.Administration.Framework framework;
  228:             List<Ia.Ftn.Cl.Models.Business.Administration.Framework> frameworkList;
  229:  
  230:             var list = new List<Ia.Ftn.Cl.Models.Telegram.Chat>(40);
  231:  
  232:             string frameworkArabicName;
  233:  
  234:             // FTN department groups
  235:             frameworkArabicName = "إدارة شبكة الألياف الضوئية";
  236:             framework = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList where f.ArabicName == frameworkArabicName select f).SingleOrDefault();
  237:             list.Add(new Ia.Ftn.Cl.Models.Telegram.Chat()
  238:             {
  239:                 ImageId = ChatNameIdToLogoNameImageIdAndQrCodeImageIdDictionary["ftnGroupId"],
  240:                 NameId = "ftnGroupId",
  241:                 ChatId = ChatNameIdToChatIdDictionary["ftnGroupId"],
  242:                 Name = NameText(frameworkArabicName),
  243:                 Description = DescriptionText(frameworkArabicName),
  244:                 Type = Ia.Ftn.Cl.Models.Telegram.Chat.ChatType.PrivateGroup,
  245:                 RestrictedByFrameworkSite = false,
  246:                 Framework = framework
  247:             });
  248:  
  249:             frameworkArabicName = "مراقبة تقنية المعلومات";
  250:             framework = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList where f.ArabicName == frameworkArabicName select f).SingleOrDefault();
  251:             list.Add(new Ia.Ftn.Cl.Models.Telegram.Chat()
  252:             {
  253:                 ImageId = ChatNameIdToLogoNameImageIdAndQrCodeImageIdDictionary["ftnItGroupId"],
  254:                 NameId = "ftnItGroupId",
  255:                 ChatId = ChatNameIdToChatIdDictionary["ftnItGroupId"],
  256:                 Name = NameText(frameworkArabicName),
  257:                 Description = DescriptionText(frameworkArabicName),
  258:                 Type = Ia.Ftn.Cl.Models.Telegram.Chat.ChatType.PrivateGroup,
  259:                 RestrictedByFrameworkSite = false,
  260:                 Framework = framework
  261:             });
  262:  
  263:             frameworkArabicName = "مراقبة الدعم الفنى للشبكة";
  264:             framework = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList where f.ArabicName == frameworkArabicName select f).SingleOrDefault();
  265:             list.Add(new Ia.Ftn.Cl.Models.Telegram.Chat()
  266:             {
  267:                 ImageId = ChatNameIdToLogoNameImageIdAndQrCodeImageIdDictionary["ftnSupportGroupId"],
  268:                 NameId = "ftnSupportGroupId",
  269:                 ChatId = ChatNameIdToChatIdDictionary["ftnSupportGroupId"],
  270:                 Name = NameText(frameworkArabicName),
  271:                 Description = DescriptionText(frameworkArabicName),
  272:                 Type = Ia.Ftn.Cl.Models.Telegram.Chat.ChatType.PrivateGroup,
  273:                 RestrictedByFrameworkSite = false,
  274:                 Framework = framework
  275:             });
  276:  
  277:             frameworkArabicName = "مراقبة تشغيل الشبكة";
  278:             framework = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList where f.ArabicName == frameworkArabicName select f).SingleOrDefault();
  279:             list.Add(new Ia.Ftn.Cl.Models.Telegram.Chat()
  280:             {
  281:                 ImageId = ChatNameIdToLogoNameImageIdAndQrCodeImageIdDictionary["ftnOperationGroupId"],
  282:                 NameId = "ftnOperationGroupId",
  283:                 ChatId = ChatNameIdToChatIdDictionary["ftnOperationGroupId"],
  284:                 Name = NameText(frameworkArabicName),
  285:                 Description = DescriptionText(frameworkArabicName),
  286:                 Type = Ia.Ftn.Cl.Models.Telegram.Chat.ChatType.PrivateGroup,
  287:                 RestrictedByFrameworkSite = false,
  288:                 Framework = framework
  289:             });
  290:  
  291:             frameworkArabicName = "مراقبة خدمات الشبكة";
  292:             framework = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList where f.ArabicName == frameworkArabicName select f).SingleOrDefault();
  293:             list.Add(new Ia.Ftn.Cl.Models.Telegram.Chat()
  294:             {
  295:                 ImageId = ChatNameIdToLogoNameImageIdAndQrCodeImageIdDictionary["ftnServiceGroupId"],
  296:                 NameId = "ftnServiceGroupId",
  297:                 ChatId = ChatNameIdToChatIdDictionary["ftnServiceGroupId"],
  298:                 Name = NameText(frameworkArabicName),
  299:                 Description = DescriptionText(frameworkArabicName),
  300:                 Type = Ia.Ftn.Cl.Models.Telegram.Chat.ChatType.PrivateGroup,
  301:                 RestrictedByFrameworkSite = false,
  302:                 Framework = framework
  303:             });
  304:  
  305:  
  306:             // FTN channel(s)
  307:             frameworkArabicName = "إدارة شبكة الألياف الضوئية";
  308:             framework = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList where f.ArabicName == frameworkArabicName select f).SingleOrDefault();
  309:             list.Add(new Ia.Ftn.Cl.Models.Telegram.Chat()
  310:             {
  311:                 ImageId = ChatNameIdToLogoNameImageIdAndQrCodeImageIdDictionary["ftnChannelId"],
  312:                 NameId = "ftnChannelId",
  313:                 ChatId = ChatNameIdToChatIdDictionary["ftnChannelId"],
  314:                 Name = NameText(frameworkArabicName),
  315:                 Description = ChannelDescriptionText(frameworkArabicName),
  316:                 Type = Ia.Ftn.Cl.Models.Telegram.Chat.ChatType.PrivateChannel,
  317:                 RestrictedByFrameworkSite = false,
  318:                 Framework = framework
  319:             });
  320:  
  321:  
  322:             // groups: framework site restricted
  323:             foreach (KeyValuePair<string, long> kvp in ChatNameIdToChatIdDictionary)
  324:             {
  325:                 nameId = kvp.Key;
  326:                 chatId = kvp.Value;
  327:  
  328:                 frameworkList = (from f in Ia.Ftn.Cl.Models.Data.Administration.FrameworkList
  329:                                  where f.Type == "division" && f.Sites.Count > 0 && f.Sites.Any(u => nameId.ToUpper().Contains(u.Name))
  330:                                  select f).ToList();  // division: مراقبة
  331:  
  332:                 chat = new Ia.Ftn.Cl.Models.Telegram.Chat();
  333:  
  334:                 if (frameworkList.Count > 0)
  335:                 {
  336:                     foreach (var f in frameworkList)
  337:                     {
  338:                         chat = new Ia.Ftn.Cl.Models.Telegram.Chat()
  339:                         {
  340:                             ImageId = ChatNameIdToLogoNameImageIdAndQrCodeImageIdDictionary[nameId],
  341:                             NameId = nameId,
  342:                             ChatId = chatId,
  343:                             Name = NameText(f.ArabicName),
  344:                             Description = DescriptionText(f.ArabicName),
  345:                             Type = Ia.Ftn.Cl.Models.Telegram.Chat.ChatType.PrivateGroup,
  346:                             RestrictedByFrameworkSite = true,
  347:                             Framework = f
  348:                         };
  349:                     }
  350:  
  351:                     list.Add(chat);
  352:                 }
  353:                 else
  354:                 {
  355:                 }
  356:             }
  357:  
  358:             return list;
  359:         }
  360:  
  361:         ////////////////////////////////////////////////////////////////////////////
  362:  
  363:         /// <summary>
  364:         /// 
  365:         /// </summary>
  366:         private static string NameText(string frameworkArabicName)
  367:         {
  368:             var text = frameworkArabicName + ": موظفي وفنيي التواصل مع شبكة الألياف الضوئية";
  369:  
  370:             return text;
  371:         }
  372:  
  373:         ////////////////////////////////////////////////////////////////////////////
  374:  
  375:         /// <summary>
  376:         /// 
  377:         /// </summary>
  378:         private static string DescriptionText(string frameworkArabicName)
  379:         {
  380:             var text = @"هذه مجموعة تيليجرام الخاصة بموظفي وفنيي " + frameworkArabicName + @" للتواصل مع خادم (سيرفر) إدارة شبكة الألياف الضوئية. للحصول على معلومات وقوائم تصليح، تراسَل مع المستخدم @" + Ia.Ftn.Cl.Models.Telegram.Chat.FtnUserBotNameId + @" معك في هذه المجموعة وسيرد عليك بالمعلومات المطلوبة.";
  381:  
  382:             return text;
  383:         }
  384:  
  385:         ////////////////////////////////////////////////////////////////////////////
  386:  
  387:         /// <summary>
  388:         /// 
  389:         /// </summary>
  390:         private static string ChannelDescriptionText(string frameworkArabicName)
  391:         {
  392:             var text = @"هذه قناة تيليجرام الخاصة بموظفي وفنيي " + frameworkArabicName + @" لاستلام إشعارات خادم (سيرفر) إدارة شبكة الألياف الضوئية. للحصول على معلومات وقوائم تصليح، تراسَل مع المستخدم @" + Ia.Ftn.Cl.Models.Telegram.Chat.FtnUserBotNameId + @" معك في هذه المجموعة وسيرد عليك بالمعلومات المطلوبة.";
  393:  
  394:             return text;
  395:         }
  396:  
  397:         ////////////////////////////////////////////////////////////////////////////
  398:  
  399:         /// <summary>
  400:         /// 
  401:         /// </summary>
  402:         public static Dictionary<string, long> ChatNameIdToChatIdDictionary
  403:         {
  404:             get
  405:             {
  406:                 return new Dictionary<string, long>
  407:                 {
  408:                     ["ftnChannelId"] = -1002423858856,
  409:                     ["ftnGroupId"] = -4555183048,
  410:  
  411:                     // مراقبات الادارة
  412:                     ["ftnItGroupId"] = 0, // -606519699,
  413:                     ["ftnServiceGroupId"] = 0, // -456397807,
  414:                     ["ftnOperationGroupId"] = 0, // -653210251,
  415:                     ["ftnSupportGroupId"] = 0, // -648835158,
  416:  
  417:                     ["ftnSurGroupId"] = 0, // -1001248403784, //- 270294758,
  418:                     ["ftnSkbGroupId"] = 0, // -294568632,
  419:                     ["ftnSlbGroupId"] = 0, // -391495618,
  420:                     ["ftnQrnGroupId"] = 0, // -320056533,
  421:                     ["ftnMsfGroupId"] = 0, // -1001235014246, //- 376519373,
  422:                     ["ftnRslGroupId"] = 0, // -370905362,
  423:                     ["ftnJlsGroupId"] = 0, // -389361722,
  424:                     ["ftnArdGroupId"] = 0, // -377650856,
  425:                     ["ftnRkkGroupId"] = 0, // -321982802,
  426:                     ["ftnSbaGroupId"] = 0, // -353233683,
  427:                     ["ftnMgfGroupId"] = 0, // -385099568,
  428:                     ["ftnKhpGroupId"] = 0, // -193997759,
  429:                     ["ftnKhrGroupId"] = 0, // -336590544,
  430:                     ["ftnJhbGroupId"] = 0, // -261167826
  431:                 };
  432:             }
  433:         }
  434:  
  435:         ////////////////////////////////////////////////////////////////////////////
  436:  
  437:         /// <summary>
  438:         /// 
  439:         /// </summary>
  440:         public static Dictionary<string, int> ChatNameIdToLogoNameImageIdAndQrCodeImageIdDictionary
  441:         {
  442:             get
  443:             {
  444:                 return new Dictionary<string, int>
  445:                 {
  446:                     ["ftnChannelId"] = 12,
  447:                     ["ftnGroupId"] = 21,
  448:  
  449:                     // مراقبات الادارة
  450:                     ["ftnItGroupId"] = 20,
  451:                     ["ftnServiceGroupId"] = 3,
  452:                     ["ftnOperationGroupId"] = 8,
  453:                     ["ftnSupportGroupId"] = 33,
  454:  
  455:                     ["ftnSurGroupId"] = 1,
  456:                     ["ftnSkbGroupId"] = 2,
  457:                     ["ftnSlbGroupId"] = 30,
  458:                     ["ftnQrnGroupId"] = 11,
  459:                     ["ftnMsfGroupId"] = 8,
  460:                     ["ftnRslGroupId"] = 3,
  461:                     ["ftnJlsGroupId"] = 18,
  462:                     ["ftnArdGroupId"] = 26,
  463:                     ["ftnRkkGroupId"] = 33,
  464:                     ["ftnSbaGroupId"] = 32,
  465:                     ["ftnMgfGroupId"] = 24,
  466:                     ["ftnRkkGroupId"] = 6,
  467:                     ["ftnKhpGroupId"] = 10,
  468:                     ["ftnKhrGroupId"] = 17,
  469:                     ["ftnJhbGroupId"] = 20
  470:                 };
  471:             }
  472:         }
  473:  
  474:         ////////////////////////////////////////////////////////////////////////////
  475:  
  476:         /// <summary>
  477:         /// 
  478:         /// </summary>
  479:         public static Dictionary<string, string> ChatNameIdToInviteLinkUrlDictionary
  480:         {
  481:             get
  482:             {
  483:                 return new Dictionary<string, string>
  484:                 {
  485:                     ["ftnChannelId"] = "QXaZAd9GT7QxNmFk",
  486:                     ["ftnGroupId"] = "HqrjC5My9-M2Y2M0",
  487:  
  488:                     // مراقبات الادارة
  489:                     ["ftnItGroupId"] = "", // "5toJDAjknBdhZmY0",
  490:                     ["ftnServiceGroupId"] = "", // "nbCOnl_im9Q1Zjg0",
  491:                     ["ftnOperationGroupId"] = "", // "33AbuBAefpE1YzA0",
  492:                     ["ftnSupportGroupId"] = "", // "KnINNjpSUPIyNjc0",
  493:  
  494:                     ["ftnSurGroupId"] = "", // "EfaCvkppIUjlz-XYvFjpTA",
  495:                     ["ftnSkbGroupId"] = "", // "EfaCvhGOwrjrcidLqUMK8g",
  496:                     ["ftnSlbGroupId"] = "", // "EfaCvhdVv8LeGFt4vAWGYQ",
  497:                     ["ftnQrnGroupId"] = "", // "EfaCvhMTrNUaYk2vYWm_nA",
  498:                     ["ftnMsfGroupId"] = "", // "EfaCvkmc0mb0D0zG05cGiw",
  499:                     ["ftnRslGroupId"] = "", // "EfaCvhYbkRI47_BrgPAduA",
  500:                     ["ftnJlsGroupId"] = "", // "EfaCvhc1MDqIKE7gN8sohw",
  501:                     ["ftnArdGroupId"] = "", // "EfaCvhaCfqj4aZuF2gC7vw",
  502:                     ["ftnSbaGroupId"] = "", // "EfaCvhUN6xNZF7FrzXhtTQ",
  503:                     ["ftnMgfGroupId"] = "", // "EfaCvhb0JzBdxp3ijYsY4Q",
  504:                     ["ftnRkkGroupId"] = "", // "EfaCvhMxEVLZ6nFfe2-pMw",
  505:                     ["ftnKhpGroupId"] = "", // "EfaCvguQK7_4V-ldpvau7w",
  506:                     ["ftnKhrGroupId"] = "", // "EfaCvhQP9tB9544rTJzhww",
  507:                     ["ftnJhbGroupId"] = "", // "EfaCvg-RGtKwNiEN7QA12g"
  508:                 };
  509:             }
  510:         }
  511:     }
  512:  
  513:     ////////////////////////////////////////////////////////////////////////////
  514:     ////////////////////////////////////////////////////////////////////////////
  515:  
  516:  
  517:     ////////////////////////////////////////////////////////////////////////////
  518:  
  519:     /// <summary>
  520:     ///
  521:     /// </summary>
  522:     public class User
  523:     {
  524:         /// <summary/>
  525:         public long UserId { get; set; }
  526:  
  527:         /// <summary/>
  528:         public List<Ia.Ftn.Cl.Models.Telegram.Chat> ChatList { get; set; }
  529:  
  530:         /// <summary/>
  531:         public bool IsRestrictedBySite
  532:         {
  533:             get
  534:             {
  535:                 // a user is restricted by site if all his chats are restricted by site
  536:                 bool isRestrictedBySite;
  537:  
  538:                 isRestrictedBySite = true;
  539:  
  540:                 foreach (var c in this.ChatList)
  541:                 {
  542:                     isRestrictedBySite = isRestrictedBySite & c.RestrictedByFrameworkSite;
  543:                 }
  544:  
  545:                 return isRestrictedBySite;
  546:             }
  547:         }
  548:  
  549:         /// <summary/>
  550:         public Ia.Ftn.Cl.Models.Business.Administration.Framework Framework
  551:         {
  552:             get
  553:             {
  554:                 // if a user is not restrected, framework will be null, otherwise I will take the first framework
  555:                 Ia.Ftn.Cl.Models.Business.Administration.Framework framework;
  556:  
  557:                 if (!this.IsRestrictedBySite) framework = null;
  558:                 else
  559:                 {
  560:                     framework = (from c in this.ChatList select c.Framework).First();
  561:                 }
  562:  
  563:                 return framework;
  564:             }
  565:         }
  566:  
  567:         ////////////////////////////////////////////////////////////////////////////
  568:  
  569:         /// <summary>
  570:         ///
  571:         /// </summary>
  572:         public static List<Ia.Ftn.Cl.Models.Telegram.User> List()
  573:         {
  574:             int index;
  575:             User user;
  576:  
  577:             var list = new List<long>();
  578:             var userList = new List<Ia.Ftn.Cl.Models.Telegram.User>();
  579:  
  580:             foreach (var uccm in Ia.Ftn.Cl.Models.Telegram.UserIdChatIdChatMember.List())
  581:             {
  582:                 if (!list.Contains(uccm.UserId))
  583:                 {
  584:                     list.Add(uccm.UserId);
  585:  
  586:                     user = new Ia.Ftn.Cl.Models.Telegram.User();
  587:                     user.ChatList = new List<Ia.Ftn.Cl.Models.Telegram.Chat>();
  588:  
  589:                     user.UserId = uccm.UserId;
  590:                     user.ChatList.Add(uccm.Chat);
  591:  
  592:                     userList.Add(user);
  593:                 }
  594:                 else
  595:                 {
  596:                     index = userList.FindIndex(u => u.UserId == uccm.UserId);
  597:  
  598:                     userList[index].ChatList.Add(uccm.Chat);
  599:                 }
  600:             }
  601:  
  602:             return userList;
  603:         }
  604:  
  605:         ////////////////////////////////////////////////////////////////////////////
  606:  
  607:         /// <summary>
  608:         ///
  609:         /// </summary>
  610:         public static Ia.Ftn.Cl.Models.Telegram.User Read(ITelegramBotClient telegramBotClient, long userId)
  611:         {
  612:             var user = (from u in Ia.Ftn.Cl.Models.Telegram.User.List() where u.UserId == userId select u).SingleOrDefault();
  613:  
  614:             if (user == null)
  615:             {
  616:                 // update readings
  617:                 Ia.Ftn.Cl.Models.Telegram.UserIdChatIdChatMember.UpdateAllChatListForUserId(telegramBotClient, userId);
  618:  
  619:                 user = (from u in Ia.Ftn.Cl.Models.Telegram.User.List() where u.UserId == userId select u).SingleOrDefault();
  620:             }
  621:  
  622:             return user;
  623:         }
  624:  
  625:         ////////////////////////////////////////////////////////////////////////////
  626:         ////////////////////////////////////////////////////////////////////////////
  627:     }
  628:  
  629:     ////////////////////////////////////////////////////////////////////////////
  630:     ////////////////////////////////////////////////////////////////////////////
  631:  
  632:     /// <summary>
  633:     ///
  634:     /// </summary>
  635:     public class UserIdChatIdChatMember
  636:     {
  637:         private static List<UserIdChatIdChatMember> list = new List<UserIdChatIdChatMember>();
  638:  
  639:         /// <summary/>
  640:         public string Id { get; }
  641:  
  642:         /// <summary/>
  643:         public long UserId { get; set; }
  644:  
  645:         /// <summary/>
  646:         public long ChatId { get; set; }
  647:  
  648:         /// <summary/>
  649:         public global::Telegram.Bot.Types.ChatMember ChatMember { get; set; }
  650:  
  651:         /// <summary/>
  652:         public Ia.Ftn.Cl.Models.Telegram.Chat Chat { get; set; }
  653:  
  654:         ////////////////////////////////////////////////////////////////////////////
  655:  
  656:         /// <summary>
  657:         ///
  658:         /// </summary>
  659:         public UserIdChatIdChatMember(long userId, long chatId, global::Telegram.Bot.Types.ChatMember chatMember)
  660:         {
  661:             Id = userId + ":" + chatId;
  662:  
  663:             UserId = userId;
  664:             ChatId = chatId;
  665:             ChatMember = chatMember;
  666:  
  667:             Chat = (from c in Ia.Ftn.Cl.Models.Telegram.Chat.ChatList where c.ChatId == chatId select c).SingleOrDefault();
  668:         }
  669:  
  670:         ////////////////////////////////////////////////////////////////////////////
  671:  
  672:         /// <summary>
  673:         ///
  674:         /// </summary>
  675:         public static List<UserIdChatIdChatMember> List()
  676:         {
  677:             return list;
  678:         }
  679:  
  680:         ////////////////////////////////////////////////////////////////////////////
  681:  
  682:         /// <summary>
  683:         ///
  684:         /// </summary>
  685:         public static List<UserIdChatIdChatMember> List(int userId)
  686:         {
  687:             return (from l in list where l.UserId == userId select l).ToList();
  688:         }
  689:  
  690:         ////////////////////////////////////////////////////////////////////////////
  691:  
  692:         /// <summary>
  693:         ///
  694:         /// </summary>
  695:         public static List<UserIdChatIdChatMember> List(long chatId)
  696:         {
  697:             return (from l in list where l.ChatId == chatId select l).ToList();
  698:         }
  699:  
  700:         ////////////////////////////////////////////////////////////////////////////
  701:  
  702:         /// <summary>
  703:         ///
  704:         /// </summary>
  705:         public static UserIdChatIdChatMember List(int userId, long chatId)
  706:         {
  707:             return (from l in list where l.UserId == userId && l.ChatId == chatId select l).SingleOrDefault();
  708:         }
  709:  
  710:         ////////////////////////////////////////////////////////////////////////////
  711:  
  712:         /// <summary>
  713:         ///
  714:         /// </summary>
  715:         public static bool UpdateAllChatListForUserId(ITelegramBotClient telegramBotClient, long userId)
  716:         {
  717:             bool isUpdated;
  718:             long chatId;
  719:             global::Telegram.Bot.Types.ChatMember chatMember;
  720:  
  721:             isUpdated = false;
  722:  
  723:             foreach (KeyValuePair<string, long> kvp in Ia.Ftn.Cl.Models.Telegram.Chat.ChatNameIdToChatIdDictionary)
  724:             {
  725:                 chatId = kvp.Value;
  726:  
  727:                 if (chatId != 0)
  728:                 {
  729:                     try
  730:                     {
  731:                         chatMember = telegramBotClient.GetChatMemberAsync(chatId, userId).Result;
  732:  
  733:                         // chatMember.Status == ChatMemberStatus.Left is added due to a bug in Telegram I guess. 
  734:                         if (chatMember.Status == ChatMemberStatus.Creator || chatMember.Status == ChatMemberStatus.Administrator || chatMember.Status == ChatMemberStatus.Member || chatMember.Status == ChatMemberStatus.Left)
  735:                             isUpdated = isUpdated | Update(userId, chatId, chatMember);
  736:                     }
  737:                     catch (Exception e)
  738:                     {
  739:                     }
  740:                 }
  741:             }
  742:  
  743:             return isUpdated;
  744:         }
  745:  
  746:         ////////////////////////////////////////////////////////////////////////////
  747:  
  748:         /// <summary>
  749:         ///
  750:         /// </summary>
  751:         public static bool Update(long userId, long chatId, global::Telegram.Bot.Types.ChatMember chatMember)
  752:         {
  753:             bool isUpdated;
  754:             int index;
  755:  
  756:             index = list.FindIndex(c => c.UserId == userId && c.ChatId == chatId);
  757:  
  758:             if (index >= 0)
  759:             {
  760:                 if (list[index].ChatMember == chatMember)
  761:                 {
  762:                     isUpdated = false;
  763:                 }
  764:                 else
  765:                 {
  766:                     list[index].ChatMember = chatMember;
  767:  
  768:                     list[index].Chat = (from c in Ia.Ftn.Cl.Models.Telegram.Chat.ChatList where c.ChatId == chatId select c).SingleOrDefault();
  769:  
  770:                     isUpdated = true;
  771:                 }
  772:             }
  773:             else
  774:             {
  775:                 list.Add(new UserIdChatIdChatMember(userId, chatId, chatMember));
  776:  
  777:                 isUpdated = true;
  778:             }
  779:  
  780:             return isUpdated;
  781:         }
  782:     }
  783:  
  784:     ////////////////////////////////////////////////////////////////////////////
  785:     ////////////////////////////////////////////////////////////////////////////   
  786: }