Exemple #1
0
        private async Task ExecuteNonPrivateChatCommandAsync(Update update)
        {
            try
            {
                var command = ResolveBotCommand(update);

                switch (command)
                {
                case BotCommand.Start:

                    await _messageBO.SendWelcomeMessageAsync(false, update.Message.Chat.Id);

                    break;

                case BotCommand.LinksUrl:

                    await _messageBO.SendLinksRecoverMessageAsync(update.Message.Chat.Id, update.Message.From.Id, update.Message.Chat.Title, update.Message.MessageId);

                    break;

                case BotCommand.Help:

                    await _messageBO.SendHelpMessageAsync(false, update.Message.Chat.Id, update.Message.MessageId, update.Message.From.Id);

                    break;

                case BotCommand.DisableSavedMsg:
                case BotCommand.DisableLinkAlreadyExistsMsg:
                case BotCommand.DisableInvalidLinkMsg:
                case BotCommand.EnableSavedMsg:
                case BotCommand.EnableLinkAlreadyExistsMsg:
                case BotCommand.EnableInvalidLinkMsg:

                    var adminUsers = await BotClient.GetChatAdministratorsAsync(update.Message.Chat.Id);

                    if (!adminUsers.Any(p => p.User.Id == update.Message.From.Id))
                    {
                        await _messageBO.SendBotOptionNotAllowedMessageAsync(update.Message.Chat.Id, update.Message.MessageId);

                        return;
                    }

                    await _configBO.SaveAsync(update.Message.Chat.Id, command);

                    await _messageBO.SendBotOptionSetMessageAsync(update.Message.Chat.Id, update.Message.MessageId, command);

                    break;
                }
            }
            catch (Exception exception)
            {
                Logger.LogError(exception.Message);

                await _messageBO.SendBotOptionNotSetMessageAsync(update.Message.Chat.Id, update.Message.MessageId);
            }
        }
 private System.Boolean IsUserAdmin(Telegram.Bot.Types.User user, Chat chat = null)
 {
     if (Singleton.GetInstance().Context._users.Any(c => c.ID == user.Id && c.IsAdmin != 0))
     {
         return(true);
     }
     if (chat != null)
     {
         List <ChatMember> Ads = BotClient.GetChatAdministratorsAsync(chat).Result.ToList();
         if (Ads.Any(c => c.User.Id == user.Id))
         {
             return(true);
         }
     }
     return(false);
 }
        private async Task <IEnumerable <string> > FindAllowedTesterUserNames()
        {
            // Try to get user names from test configurations first
            string[] allowedUserNames = ConfigurationProvider.TestConfigurations.AllowedUserNames
                                        .Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                                        .Select(n => n.Trim())
                                        .ToArray();

            if (!allowedUserNames.Any())
            {
                // Assume all chat admins are allowed testers
                ChatMember[] admins = await BotClient.GetChatAdministratorsAsync(SupergroupChat, CancellationToken);

                allowedUserNames = admins
                                   .Where(member => !member.User.IsBot)
                                   .Select(member => member.User.Username)
                                   .ToArray();
            }

            return(allowedUserNames);
        }
Exemple #4
0
        public async void FindHashtags()
        {
            if (string.IsNullOrWhiteSpace(_message.Text))
            {
                return;
            }

            // harus grup chat
            if (!_message.IsGroupChat())
            {
                return;
            }

            _log.Debug("Cari semua hashtag dalam teks : {0}", _message.Text);

            var hashtags = new List <string>();
            var matches  = Regex.Matches(_message.Text, @"#([\w\d]+)");

            foreach (Match match in matches)
            {
                var hashtag = match.Groups[1].Value;
                hashtags.Add(hashtag);
            }

            if (hashtags.Count == 0)
            {
                _log.Ignore("Tidak ada hashtag...");
                return;
            }

            // panggil admin/mimin
            foreach (var hashtag in hashtags)
            {
                if (hashtag != "admin" && hashtag != "mimin")
                {
                    continue;
                }

                _log.Debug("Panggil admins grup!", hashtag);

                var admins = await BotClient.GetChatAdministratorsAsync(_message);

                admins = admins.OrderBy(x => x.User.FirstName).ToArray();
                var respon = "Panggilan kepada :";
                foreach (var x in admins)
                {
                    var user = (x.User.FirstName + " " + x.User.LastName).Trim();
                    if (string.IsNullOrWhiteSpace(user))
                    {
                        user = x.User.Username;
                    }
                    if (string.IsNullOrWhiteSpace(user))
                    {
                        user = x.User.Id.ToString();
                    }
                    respon += $"\n• <a href=\"tg://user?id={x.User.Id}\">{user.Trim()}</a>";
                }

                await BotClient.SendTextAsync(_message.Chat.Id, respon, _message.MessageId, ParseMode.Html);

                // hapus #admin dari list
                hashtags.Remove(hashtag);
                break;
            }

            if (hashtags.Count == 0)
            {
                return;
            }

            var isPrivateGroup = string.IsNullOrWhiteSpace(_message.Chat.Username);

            // forward pesan
            if (isPrivateGroup)
            {
                foreach (var hashtag in hashtags)
                {
                    // forward hashtag
                    await ForwardHashtag(hashtag);
                }
            }
            // bikin pesan link
            else
            {
                var isFound = false;
                var respon  = string.Empty;
                foreach (var hashtag in hashtags)
                {
                    var found = await _db.GetBookmarkByHashtag(_message.Chat.Id, hashtag);

                    if (found == null)
                    {
                        _log.Ignore("Hashtag {0} tidak ada!", hashtag);
                        continue;
                    }

                    isFound = true;
                    respon +=
                        $"» <a href=\"https://t.me/{_message.Chat.Username}/{found.MessageId}\">{found.KeyName}</a>\n";
                }

                if (!isFound)
                {
                    return;
                }

                respon = respon.TrimEnd('\n');
                await BotClient.SendTextAsync(_message.Chat.Id, $"Link Bookmark :\n{respon}",
                                              _message.MessageId, ParseMode.Html, preview : false);
            }
        }