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