274 lines
8.8 KiB
C#
274 lines
8.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using NeoSmart.Unicode;
|
|
using static TdLib.TdApi;
|
|
using static telegram.tgcli;
|
|
|
|
namespace telegram
|
|
{
|
|
public class Util
|
|
{
|
|
public static class Ansi
|
|
{
|
|
public const string ResetAll = "\x1B[0m";
|
|
public const string Red = "\x1b[31m";
|
|
public const string Green = "\x1b[32m";
|
|
public const string Yellow = "\x1b[33m";
|
|
public const string Blue = "\x1b[34m";
|
|
public const string Magenta = "\x1b[35m";
|
|
public const string Cyan = "\x1b[36m";
|
|
public const string Bold = "\x1b[1m";
|
|
public const string BoldOff = "\x1b[22m";
|
|
}
|
|
|
|
public static User getUser(int uid)
|
|
{
|
|
try
|
|
{
|
|
var uinfo = client.ExecuteAsync(new GetUser
|
|
{
|
|
UserId = uid
|
|
}).Result;
|
|
return uinfo;
|
|
}
|
|
catch
|
|
{
|
|
var user = new User();
|
|
user.FirstName = "null";
|
|
user.LastName = "null";
|
|
return user;
|
|
}
|
|
}
|
|
|
|
public static Chat getChat(long chatId)
|
|
{
|
|
return client.ExecuteAsync(new GetChat
|
|
{
|
|
ChatId = chatId
|
|
}).Result;
|
|
}
|
|
|
|
public static User getMe()
|
|
{
|
|
return client.ExecuteAsync(new GetMe()).Result;
|
|
}
|
|
|
|
public static Message getMessage(long chatId, long messageId)
|
|
{
|
|
return client.ExecuteAsync(new GetMessage
|
|
{
|
|
ChatId = chatId,
|
|
MessageId = messageId
|
|
}).Result;
|
|
}
|
|
|
|
public static List<Message> getHistory(long chatId, int limit = 5, long fromMessageId = 0, int offset = 0)
|
|
{
|
|
var history = new List<Message>();
|
|
if (limit <= 0)
|
|
{
|
|
lock (_lock)
|
|
messageQueue.Add($"{Ansi.Red}[tgcli] " +
|
|
$"Limit cannot be less than one. Usage: /history <count>");
|
|
return history;
|
|
}
|
|
|
|
var response = client.ExecuteAsync(new GetChatHistory
|
|
{
|
|
ChatId = chatId,
|
|
FromMessageId = fromMessageId,
|
|
Limit = limit,
|
|
Offset = offset,
|
|
OnlyLocal = false
|
|
}).Result;
|
|
history.AddRange(response.Messages_);
|
|
history.Reverse();
|
|
return history;
|
|
}
|
|
|
|
public static List<Chat> getUnreadChats(bool all = false)
|
|
{
|
|
var response = client.ExecuteAsync(new GetChats
|
|
{
|
|
OffsetOrder = long.MaxValue,
|
|
Limit = int.MaxValue
|
|
}).Result;
|
|
return all
|
|
? response.ChatIds.Select(getChat).Where(c => c.UnreadCount > 0 || c.IsMarkedAsUnread).ToList()
|
|
: response.ChatIds.Select(getChat).Where(c => (c.UnreadCount > 0 || c.IsMarkedAsUnread)
|
|
&& c.NotificationSettings.MuteFor == 0)
|
|
.ToList();
|
|
}
|
|
|
|
public static void ClearCurrentConsoleLine()
|
|
{
|
|
Console.SetCursorPosition(0, Console.WindowHeight);
|
|
Console.Write(new string(' ', Console.WindowWidth));
|
|
Console.SetCursorPosition(0, Console.WindowHeight);
|
|
}
|
|
|
|
public static string ReadConsolePassword()
|
|
{
|
|
string pass = "";
|
|
do
|
|
{
|
|
ConsoleKeyInfo key = Console.ReadKey(true);
|
|
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
|
|
{
|
|
pass += key.KeyChar;
|
|
Console.Write("*");
|
|
}
|
|
else
|
|
{
|
|
if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
|
|
{
|
|
pass = pass.Substring(0, (pass.Length - 1));
|
|
Console.Write("\b \b");
|
|
}
|
|
else if (key.Key == ConsoleKey.Enter)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
} while (true);
|
|
|
|
Console.WriteLine();
|
|
return pass;
|
|
}
|
|
|
|
public static void sendMessage(string message, long chatId)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(message))
|
|
return;
|
|
message = message.Replace("⏎", "\n")
|
|
.Replace(":xd:", Emoji.FaceWithTearsOfJoy.Sequence.AsString)
|
|
.Replace(":heart:", Emoji.RedHeart.Sequence.AsString)
|
|
.Replace(":shrug:", Emoji.PersonShrugging.Sequence.AsString)
|
|
.Replace(":shrugf:", Emoji.WomanShrugging.Sequence.AsString)
|
|
.Replace(":shrugm:", Emoji.ManShrugging.Sequence.AsString);
|
|
client.ExecuteAsync(new SendMessage
|
|
{
|
|
ChatId = chatId,
|
|
InputMessageContent = new InputMessageContent.InputMessageText
|
|
{
|
|
Text = new FormattedText()
|
|
{
|
|
Text = message
|
|
}
|
|
}
|
|
});
|
|
currentUserRead = false;
|
|
}
|
|
|
|
public static Message editMessage(string newText, Message message)
|
|
{
|
|
var msg = client.ExecuteAsync(new EditMessageText
|
|
{
|
|
ChatId = message.ChatId,
|
|
MessageId = message.Id,
|
|
InputMessageContent = new InputMessageContent.InputMessageText
|
|
{
|
|
Text = new FormattedText()
|
|
{
|
|
Text = newText
|
|
}
|
|
}
|
|
}).Result;
|
|
|
|
return msg;
|
|
}
|
|
|
|
public static void markRead(long chatId, long messageId)
|
|
{
|
|
client.ExecuteAsync(new ViewMessages
|
|
{
|
|
ChatId = chatId,
|
|
MessageIds = new[]
|
|
{
|
|
messageId
|
|
},
|
|
ForceRead = true
|
|
});
|
|
}
|
|
|
|
public static void markUnread(long chatId)
|
|
{
|
|
client.ExecuteAsync(new ToggleChatIsMarkedAsUnread
|
|
{
|
|
ChatId = chatId,
|
|
IsMarkedAsUnread = true,
|
|
});
|
|
}
|
|
|
|
public static long searchChatId(string query)
|
|
{
|
|
try
|
|
{
|
|
var results = client.ExecuteAsync(new SearchChatsOnServer
|
|
{
|
|
Query = query,
|
|
Limit = 5
|
|
}).Result;
|
|
|
|
return results.ChatIds.First();
|
|
}
|
|
catch
|
|
{
|
|
lock (_lock)
|
|
messageQueue.Add($"{Ansi.Red}[tgcli] No results found.");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public static object getFormattedUsername(User sender)
|
|
{
|
|
var username = sender.Username;
|
|
if (string.IsNullOrWhiteSpace(username))
|
|
username = sender.FirstName + " " +
|
|
sender.LastName;
|
|
else
|
|
username = "@" + username;
|
|
|
|
return username;
|
|
}
|
|
|
|
public static string formatTime(long unix)
|
|
{
|
|
var time = DateTimeOffset.FromUnixTimeSeconds(unix).DateTime.ToLocalTime();
|
|
var currentTime = DateTime.Now.ToLocalTime();
|
|
return time.ToString(time.Date.Ticks == currentTime.Date.Ticks ? "HH:mm" : "yyyy-MM-dd HH:mm");
|
|
}
|
|
|
|
public static bool isMessageRead(long chatId, long messageId)
|
|
{
|
|
var chat = getChat(chatId);
|
|
return chat.LastReadOutboxMessageId <= messageId;
|
|
}
|
|
|
|
public static int getActualStringWidth(string input)
|
|
{
|
|
input = input.Replace(Ansi.Blue, "");
|
|
input = input.Replace(Ansi.Bold, "");
|
|
input = input.Replace(Ansi.Cyan, "");
|
|
input = input.Replace(Ansi.Green, "");
|
|
input = input.Replace(Ansi.Magenta, "");
|
|
input = input.Replace(Ansi.Red, "");
|
|
input = input.Replace(Ansi.Yellow, "");
|
|
input = input.Replace(Ansi.BoldOff, "");
|
|
input = input.Replace(Ansi.ResetAll, "");
|
|
return input.Length;
|
|
}
|
|
|
|
public static string getFormattedStatus(bool isOnline, bool isRead, bool isTyping)
|
|
{
|
|
var output = " ";
|
|
output += (isOnline ? Ansi.Green : Ansi.Red) + "o";
|
|
output += (isRead ? Ansi.Green : Ansi.Red) + "r";
|
|
output += (isTyping ? Ansi.Green : Ansi.Red) + "t";
|
|
return output + $"{Ansi.ResetAll}]";
|
|
}
|
|
}
|
|
} |