Ejemplo n.º 1
0
        public static Chat ToChat(TgChat chat, long ownerId)
        {
            ImmutableList <InAttachment> photos = ImmutableList <InAttachment> .Empty;

            if (chat.Photo != null)
            {
                photos = ImmutableList.Create(
                    new InAttachment(chat.Photo.SmallFileId, TgConstants.Name, AttachmentType.Photo, null, null),
                    new InAttachment(chat.Photo.BigFileId, TgConstants.Name, AttachmentType.Photo, null, null)
                    );
            }

            var type = chat.Type switch
            {
                TgChatType.Private => ChatType.Private,
                TgChatType.Channel => ChatType.Community,
                _ => ChatType.Group
            };

            return(new Chat(
                       chat.Id,
                       TgConstants.Name,
                       chat.Title ?? chat.Username,
                       ownerId,
                       type)
            {
                PinnedMessage = ToMessage(chat.PinnedMessage),
                Photos = photos
            });
        }
Ejemplo n.º 2
0
 public static User ToUser(TgChat chat)
 {
     return(new User(
                chat.Id,
                TgConstants.Name,
                true)
     {
         FirstName = chat.FirstName,
         LastName = chat.LastName,
         Username = chat.Username
     });
 }
Ejemplo n.º 3
0
        private async Task <string> GetChatLink(Chat chat)
        {
            if (chat.Username.IsNotBlank())
            {
                return($"https://t.me/{chat.Username}".ToHtmlLink(chat.Title));
            }

            var savedChat = await _chatRepository.GetByName(chat.Title);

            return(savedChat != null
                ? savedChat.JoinLink.ToHtmlLink(savedChat.ExactName)
                : $"«{chat.Title}»");
        }
Ejemplo n.º 4
0
        private static void OnBotWokeUp(Update update)
        {
            Telegram.Bot.Types.Chat chat = null;
            Telegram.Bot.Types.User user = null;
            try
            {
                if (update?.Message != null)
                {
                    chat = update.Message.Chat;
                    user = update.Message.From;
                }
                else if (update?.CallbackQuery != null)
                {
                    chat = update.CallbackQuery.Message.Chat;
                    user = update.CallbackQuery.From;
                }
                else
                {
                    return;
                }

                if (Chats.TryGetValue(chat.Id, out _))
                {
                    return;
                }

                var chatRoom = CreateChatRoom(chat);
                if (chatRoom == null)
                {
                    return;
                }
                chatRoom.ChatIo.SendMessageAsync(new EnglishTexts().DidYouWriteSomething).Wait();
                chatRoom.ChatIo.OnUpdate(
                    new Update {
                    Message = new Message {
                        Text = "/start", From = user
                    }
                });

                RunChatRoom(chat, chatRoom);
            }
            catch (Exception e)
            {
                Botlog.WriteError(chat?.Id, "WokeUpfailed" + e, true);
            }
        }
Ejemplo n.º 5
0
 public static void DeleteStatusByUserAndChat(Telegram.Bot.Types.User user, Telegram.Bot.Types.Chat chat)
 {
     using (var db = new ApplicationContext())
     {
         try
         {
             var status = db.UserStatuses
                          .Where(s => s.Chat.ChatId == chat.Id)
                          .FirstOrDefault(s => s.User.UserId == user.Id);
             db.UserStatuses.Remove(status);
             db.SaveChanges();
         }
         catch
         {
             Console.WriteLine("Error with deleting statys");
         }
     }
 }
Ejemplo n.º 6
0
        private static MainFlow GetOrCreate(Telegram.Bot.Types.Chat chat)
        {
            if (Chats.TryGetValue(chat.Id, out var existedChatRoom))
            {
                return(existedChatRoom);
            }

            var newChatRoom = CreateChatRoom(chat);

            if (newChatRoom == null)
            {
                Console.WriteLine($"Chat {chat.Id} rejected");
                return(null);
            }
            Console.WriteLine($"Chat {chat.Id} joined");
            RunChatRoom(chat, newChatRoom);
            return(newChatRoom);
        }
Ejemplo n.º 7
0
 public static void CheckAndAddChatToDb(Telegram.Bot.Types.Chat chat)
 {
     try
     {
         using (ApplicationContext db = new ApplicationContext())
         {
             if (db.Chats.FirstOrDefault(c => c.ChatId == chat.Id) == null)
             {
                 db.Chats.Add(new Database.Chat()
                 {
                     ChatId = chat.Id
                 });
                 db.SaveChanges();
             }
         }
     }
     catch
     {
         Console.WriteLine("Error with db access");
     }
 }
Ejemplo n.º 8
0
        private async Task AddThisChat(string inviteLink, Chat chat, int replyToMessageId)
        {
            if (chat.IsPrivate())
            {
                await SendMessage(chat.Id, "Зачем ты пытаешься добавить наш личный чат в список чатов? >_>", replyToMessageId);

                return;
            }

            if (!chat.IsGroup())
            {
                return;
            }

            if (inviteLink.IsBlank() && chat.InviteLink.IsBlank())
            {
                try
                {
                    chat.InviteLink = await BotClient.ExportChatInviteLinkAsync(chat.Id);
                }
                catch (ApiRequestException e)
                {
                    Logger.LogWarning("Can't get invite link for chat {chatId}! [ExMessage: {exMessage}, StackTrace: {stackTrace}]", chat.Id, e.Message, e.StackTrace);
                }

                if (chat.InviteLink.IsBlank())
                {
                    await SendMessage(chat.Id, "Или дайте мне ссылку-приглашение вместе с коммандой, или сделайте админом, чтобы я сам мог создать её.", replyToMessageId);

                    return;
                }
            }

            await _chatRepository.AddOrUpdate(new SavedChat(chat.Id, chat.Title, chat.InviteLink));

            await SendMessage(chat.Id, "Чат добавлен! Спасибо за помощь боту!", replyToMessageId);
        }
Ejemplo n.º 9
0
        static async void Bot_OnMessage(object sender, MessageEventArgs messageEventArgs)
        {
            chatid = messageEventArgs.Message.Chat;

            var message = messageEventArgs.Message;

            if (message == null || message.Type != MessageType.Text)
            {
                return;
            }

            switch (message.Text.Split(' ').First())
            {
            //  tự động nhắc việc
            case "/auto":
                await auto(message);

                break;

            // thêm việc
            case "/add":
                await add(message);

                break;

            // hiển thị danh sách việc
            case "/list":
                await list(message);

                break;

            // hiển thị danh sách công việc đã hoàn thành
            case "/listdone":
                await listdone(message);

                break;

            // xóa việc
            case "/delete":
                await delete(message);

                break;

            // sửa việc
            case "/edit":
                await edit(message);

                break;

            // hoàn thành
            case "/done":
                await done(message);

                break;

            default:
                await list(message);

                break;
            }
        }
Ejemplo n.º 10
0
 public static void AddMemberByUserAndChat(Telegram.Bot.Types.User[] messageNewChatMembers, Telegram.Bot.Types.Chat messageChat)
 {
     try
     {
         foreach (var VARIABLE in messageNewChatMembers)
         {
             CheckAndAddUserStatusToDb(VARIABLE.Id, messageChat.Id);
         }
     }
     catch
     {
         Console.WriteLine("Error with database");
     }
 }