VkMessagercs: New Methods and VK API version update (#1319)

* New Methods and VK API version update
* Update VkMessager.cs
* Update VkMessager.cs
* Fix code indenting
Co-authored-by: ORelio <ORelio@users.noreply.github.com>
This commit is contained in:
Рома Данилов 2020-11-09 03:49:19 +05:00 committed by GitHub
parent 28f47cc532
commit 2eba2149b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -47,6 +47,7 @@ internal class VkLongPoolClient
Init(); Init();
StartLongPoolAsync(); StartLongPoolAsync();
} }
private WebClient ReceiverWebClient { get; set; } private WebClient ReceiverWebClient { get; set; }
private WebClient SenderWebClient { get; set; } private WebClient SenderWebClient { get; set; }
private string Token { get; set; } private string Token { get; set; }
@ -65,11 +66,15 @@ internal class VkLongPoolClient
string json = CallVkMethod("utils.getShortLink", "url=" + url); string json = CallVkMethod("utils.getShortLink", "url=" + url);
var j = JsonConvert.DeserializeObject(json) as JObject; var j = JsonConvert.DeserializeObject(json) as JObject;
var link = j["response"]["short_url"].ToString(); var link = j["response"]["short_url"].ToString();
if (link == "") return Utils_GetShortLink(url); if (link == "")
else return link; return Utils_GetShortLink(url);
else
return link;
} }
else return "Invalid link format"; else
return "Invalid link format";
} }
/* Docs */ /* Docs */
public string Docs_GetMessagesUploadServer(string peer_id, string type, string file) public string Docs_GetMessagesUploadServer(string peer_id, string type, string file)
{ {
@ -79,8 +84,10 @@ internal class VkLongPoolClient
string uploadurl = Regex.Match(string1, "\"upload_url\":\"(.*)\"").Groups[1].Value.Replace(@"\/", "/"); string uploadurl = Regex.Match(string1, "\"upload_url\":\"(.*)\"").Groups[1].Value.Replace(@"\/", "/");
return uploadurl; return uploadurl;
} }
else return Docs_GetMessagesUploadServer(peer_id, type, file); else
return Docs_GetMessagesUploadServer(peer_id, type, file);
} }
public string Docs_Upload(string url, string file) public string Docs_Upload(string url, string file)
{ {
var c = new WebClient(); var c = new WebClient();
@ -88,32 +95,36 @@ internal class VkLongPoolClient
if (r2 != "") return r2; if (r2 != "") return r2;
else return Docs_Upload(url, file); else return Docs_Upload(url, file);
} }
public string Docs_Save(string file, string title) public string Docs_Save(string file, string title)
{ {
var j2 = JsonConvert.DeserializeObject(file) as JObject; var j2 = JsonConvert.DeserializeObject(file) as JObject;
if (j2 != null)
{
string json = CallVkMethod("docs.save", "&file=" + j2["file"].ToString() + "&title=" + title); string json = CallVkMethod("docs.save", "&file=" + j2["file"].ToString() + "&title=" + title);
if (json != "") if (json != "")
return json; return json;
else return Docs_Save(file, title); else return "";
} }
else return "";
}
public string Docs_Get_Send_Attachment(string file) public string Docs_Get_Send_Attachment(string file)
{ {
var j3 = JsonConvert.DeserializeObject(file) as JObject; var j3 = JsonConvert.DeserializeObject(file) as JObject;
var at = "doc" + j3["response"]["doc"]["owner_id"].ToString() + "_" + j3["response"]["doc"]["id"].ToString(); var at = "doc" + j3["response"]["doc"]["owner_id"].ToString() + "_" + j3["response"]["doc"]["id"].ToString();
return at; return at;
} }
/* Groups */ /* Groups */
public void Groups_Online(bool enable = true) public void Groups_Online(bool enable = true)
{ {
if (enable) if (enable)
string json = CallVkMethod("groups.enableOnline", "group_id=" + BotCommunityId); CallVkMethod("groups.enableOnline", "group_id=" + BotCommunityId);
if (json == "")
Groups_Online(enable);
else else
string json = CallVkMethod("groups.disableOnline", "group_id=" + BotCommunityId); CallVkMethod("groups.disableOnline", "group_id=" + BotCommunityId);
if (json == "")
Groups_Online(enable);
} }
public string Groups_GetById_GetName(string group_id) public string Groups_GetById_GetName(string group_id)
{ {
try try
@ -125,37 +136,25 @@ internal class VkLongPoolClient
string name = j3["response"][0]["name"].ToString(); string name = j3["response"][0]["name"].ToString();
return name; return name;
} }
else return Groups_GetById_GetName(group_id); else return "";
} catch { return ""; }
} }
/* Users */ catch { return ""; }
public string Users_Get_FirstName(string user_id)
{
string js = CallVkMethod("users.get", "user_ids=" + user_id);
if (js != "")
{
var j3 = JsonConvert.DeserializeObject(js) as JObject;
string name = j3["response"][0]["first_name"].ToString();
return name;
}
else return Users_Get_FirstName(user_id);
} }
/* Messages */ /* Messages */
public void Messages_Kick_Group(string chat_id, string user_id) public void Messages_Kick_Group(string chat_id, string user_id)
{ {
if (user_id != BotCommunityId) if (user_id != BotCommunityId)
{ {
string json = CallVkMethod("messages.removeChatUser", "chat_id=" + chat_id + "&member_id=" + "-" + user_id); string json = CallVkMethod("messages.removeChatUser", "chat_id=" + chat_id + "&member_id=" + "-" + user_id);
if (json == "")
Messages_Kick_Group(chat_id, user_id);
} }
} }
public void Messages_Kick_User(string chat_id, string user_id) public void Messages_Kick_User(string chat_id, string user_id)
{ {
string json = CallVkMethod("messages.removeChatUser", "chat_id=" + chat_id + "&user_id=" + user_id + "&member_id=" + user_id); string json = CallVkMethod("messages.removeChatUser", "chat_id=" + chat_id + "&user_id=" + user_id + "&member_id=" + user_id);
if (json == "")
Messages_Kick_User(chat_id, user_id);
} }
public void Messages_SetActivity(string chatId, string type = "typing") public void Messages_SetActivity(string chatId, string type = "typing")
{ {
string id3 = chatId; string id3 = chatId;
@ -164,6 +163,7 @@ internal class VkLongPoolClient
CallVkMethod("messages.setActivity", "user_id=" + BotCommunityId + "&peer_id=" + chatId + "&group_id=" + "&type=" + type + "&group_id=" + BotCommunityId); CallVkMethod("messages.setActivity", "user_id=" + BotCommunityId + "&peer_id=" + chatId + "&group_id=" + "&type=" + type + "&group_id=" + BotCommunityId);
CallVkMethod("messages.setActivity", "user_id=" + BotCommunityId + "&peer_id=" + ind + "&type=" + type + "&group_id=" + BotCommunityId); CallVkMethod("messages.setActivity", "user_id=" + BotCommunityId + "&peer_id=" + ind + "&type=" + type + "&group_id=" + BotCommunityId);
} }
public string Messages_GetInviteLink(string chatId, bool reset) public string Messages_GetInviteLink(string chatId, bool reset)
{ {
try try
@ -173,40 +173,67 @@ internal class VkLongPoolClient
{ {
var j = JsonConvert.DeserializeObject(json) as JObject; var j = JsonConvert.DeserializeObject(json) as JObject;
var link = j["response"]["link"].ToString(); var link = j["response"]["link"].ToString();
if (link == "") return link;
return Messages_GetInviteLink(chatId, reset);
else return link;
} }
else return Messages_GetInviteLink(chatId, reset); return "";
} catch { return Messages_GetInviteLink(chatId, reset); }
} }
/* Messages Send */ catch { return ""; }
public void Messages_Send_Text(string chatId, string text) }
/* Users */
public string Users_Get_First_Name(string user_id, string name_case = "nom")
{ {
string reply = CallVkMethod("messages.send", "peer_id=" + chatId + "&random_id=" + rnd.Next() + "&message=" + text); try
if (reply == "") {
Messages_Send_Text(chatId, text); string json = CallVkMethod("users.get", "user_ids=" + user_id + "&name_case=" + name_case);
if (json != "")
{
var firstname = Regex.Match(json, "{\"first_name\":\"(.*)\",\"id\":(.*)").Groups[1].Value;
return firstname;
} }
return "";
}
catch { return ""; }
}
public string Users_Get_Last_Name(string user_id)
{
try
{
string json = CallVkMethod("users.get", "user_ids=" + user_id);
if (json != "")
{
var firstname = Regex.Match(json, "\"id\":(.*),\"last_name\":\"(.*)\",\"can_access_closed\":(.*)").Groups[2].Value;
return firstname;
}
return "";
}
catch { return ""; }
}
/* Messages Send */
public void Messages_Send_Text(string chatId, string text, int disable_mentions = 0)
{
string reply = CallVkMethod("messages.send", "peer_id=" + chatId + "&random_id=" + rnd.Next() + "&message=" + text + "&disable_mentions=" + disable_mentions);
}
public void Messages_Send_Keyboard(string chatId, Keyboard keyboard) public void Messages_Send_Keyboard(string chatId, Keyboard keyboard)
{ {
string kb = keyboard.GetKeyboard(); string kb = keyboard.GetKeyboard();
string reply = CallVkMethod("messages.send", "peer_id=" + chatId + "&random_id=" + rnd.Next() + "&keyboard=" + kb); string reply = CallVkMethod("messages.send", "peer_id=" + chatId + "&random_id=" + rnd.Next() + "&keyboard=" + kb);
if (reply == "")
Messages_Send_Keyboard(chatId, keyboard);
} }
public void Messages_Send_TextAndKeyboard(string chatId, string text, Keyboard keyboard) public void Messages_Send_TextAndKeyboard(string chatId, string text, Keyboard keyboard)
{ {
string kb = keyboard.GetKeyboard(); string kb = keyboard.GetKeyboard();
string reply = CallVkMethod("messages.send", "peer_id=" + chatId + "&random_id=" + rnd.Next() + "&message=" + text + "&keyboard=" + kb); string reply = CallVkMethod("messages.send", "peer_id=" + chatId + "&random_id=" + rnd.Next() + "&message=" + text + "&keyboard=" + kb);
if (reply == "")
Messages_Send_TextAndKeyboard(chatId, text, keyboard);
} }
public void Messages_Send_Sticker(string chatId, int sticker_id) public void Messages_Send_Sticker(string chatId, int sticker_id)
{ {
string reply = CallVkMethod("messages.send", "peer_id=" + chatId + "&random_id=" + rnd.Next() + "&sticker_id=" + sticker_id); string reply = CallVkMethod("messages.send", "peer_id=" + chatId + "&random_id=" + rnd.Next() + "&sticker_id=" + sticker_id);
if (reply == "")
Messages_Send_Sticker(chatId, sticker_id);
} }
public void Messages_Send_TextAndDocument(string chatId, string text, string file, string title) public void Messages_Send_TextAndDocument(string chatId, string text, string file, string title)
{ {
string u2 = Docs_GetMessagesUploadServer(chatId, "doc", file); string u2 = Docs_GetMessagesUploadServer(chatId, "doc", file);
@ -214,15 +241,13 @@ internal class VkLongPoolClient
string r3 = Docs_Save(r2, title); string r3 = Docs_Save(r2, title);
string at = Docs_Get_Send_Attachment(r3); string at = Docs_Get_Send_Attachment(r3);
string reply = CallVkMethod("messages.send", "peer_id=" + chatId + "&random_id=" + rnd.Next() + "&message=" + text + "&attachment=" + at); string reply = CallVkMethod("messages.send", "peer_id=" + chatId + "&random_id=" + rnd.Next() + "&message=" + text + "&attachment=" + at);
if (reply == "")
Messages_Send_TextAndDocument(chatId, text, file, title);
} }
public void Messages_Send_Custom(string chatId, string custom) public void Messages_Send_Custom(string chatId, string custom)
{ {
string reply = CallVkMethod("messages.send", "peer_id=" + chatId + "&random_id=" + rnd.Next() + custom); string reply = CallVkMethod("messages.send", "peer_id=" + chatId + "&random_id=" + rnd.Next() + custom);
if (reply == "")
Messages_Send_Custom(chatId, custom);
} }
/* Messages GetConversationMembers*/ /* Messages GetConversationMembers*/
public int Messages_GetConversationMembers_GetCount(string chatId) public int Messages_GetConversationMembers_GetCount(string chatId)
{ {
@ -233,8 +258,9 @@ internal class VkLongPoolClient
int u2 = int.Parse(j["response"]["count"].ToString()); int u2 = int.Parse(j["response"]["count"].ToString());
return u2; return u2;
} }
else return Messages_GetConversationMembers_GetCount(chatId); else return -1;
} }
public string Messages_GetConversationMembers_GetProfiles(string chatId) public string Messages_GetConversationMembers_GetProfiles(string chatId)
{ {
var json = CallVkMethod("messages.getConversationMembers", "peer_id=" + chatId + "&group_id=" + BotCommunityId); var json = CallVkMethod("messages.getConversationMembers", "peer_id=" + chatId + "&group_id=" + BotCommunityId);
@ -251,8 +277,9 @@ internal class VkLongPoolClient
} }
return ids; return ids;
} }
else return Messages_GetConversationMembers_GetProfiles(chatId); return "";
} }
public string Messages_GetConversationMembers_GetItems_member_id(string chatId) public string Messages_GetConversationMembers_GetItems_member_id(string chatId)
{ {
var json = CallVkMethod("messages.getConversationMembers", "peer_id=" + chatId + "&group_id=" + BotCommunityId); var json = CallVkMethod("messages.getConversationMembers", "peer_id=" + chatId + "&group_id=" + BotCommunityId);
@ -269,32 +296,51 @@ internal class VkLongPoolClient
} }
return ids; return ids;
} }
else return Messages_GetConversationMembers_GetItems_member_id(chatId); return "";
} }
public class Keyboard public class Keyboard
{ {
public enum Color
{
Negative,
Positive,
Primary,
Secondary
}
public bool one_time = false; public bool one_time = false;
public List<List<object>> buttons = new List<List<object>>(); public List<List<object>> buttons = new List<List<object>>();
public bool inline = false; public bool inline = false;
public Keyboard(bool one_time2, bool line = false) public Keyboard(bool one_time2, bool line = false)
{ {
if (line == true && one_time2 == true) if (line == true && one_time2 == true)
one_time2 = false; one_time2 = false;
one_time = one_time2; one_time = one_time2;
inline = line; inline = line;
} }
public void AddButton(string label, string payload, string color) public void AddButton(string label, string payload, Color color)
{ {
Buttons button = new Buttons(label, payload, color); string color2 = "";
if (color == Color.Negative)
color2 = "Negative";
else if (color == Color.Positive)
color2 = "Positive";
else if (color == Color.Primary)
color2 = "Primary";
else
color2 = "Secondary";
Buttons button = new Buttons(label, payload, color2);
buttons.Add(new List<object>() { button }); buttons.Add(new List<object>() { button });
} }
public string GetKeyboard() public string GetKeyboard()
{ {
return JsonConvert.SerializeObject(this, Formatting.Indented); ; return JsonConvert.SerializeObject(this, Formatting.Indented); ;
} }
public class Buttons public class Buttons
{ {
public Action action; public Action action;
@ -330,6 +376,7 @@ internal class VkLongPoolClient
Server = data.Properties["response"].Properties["server"].StringValue; Server = data.Properties["response"].Properties["server"].StringValue;
LastTs = Convert.ToInt32(data.Properties["response"].Properties["ts"].StringValue); LastTs = Convert.ToInt32(data.Properties["response"].Properties["ts"].StringValue);
} }
private void StartLongPoolAsync() private void StartLongPoolAsync()
{ {
Task.Factory.StartNew(() => Task.Factory.StartNew(() =>
@ -346,7 +393,6 @@ internal class VkLongPoolClient
{ {
OnMessageReceivedCallback(message.Item1, message.Item2, message.Item3, message.Item4, message.Item5, message.Item6); OnMessageReceivedCallback(message.Item1, message.Item2, message.Item3, message.Item4, message.Item5, message.Item6);
} }
} }
catch { } catch { }
} }
@ -387,8 +433,9 @@ internal class VkLongPoolClient
{ {
try try
{ {
var url = String.Format("https://api.vk.com/method/{0}?v=5.122&access_token={1}&{2}", methodName, Token, data); var url = String.Format("https://api.vk.com/method/{0}?v=5.124&access_token={1}&{2}", methodName, Token, data);
var jsonResult = SenderWebClient.DownloadString(url); var jsonResult = SenderWebClient.DownloadString(url);
return jsonResult; return jsonResult;
} }
catch { return String.Empty; } catch { return String.Empty; }