-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelegramStatus.cs
101 lines (86 loc) · 3.14 KB
/
TelegramStatus.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
Works with /status command sent to the bot.
Takes some time to respond.
*/
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System;
using System.IO;
using System.Text;
using System.IO.Compression;
using Oxide.Core.Plugins;
using Oxide.Core.Configuration;
using Oxide.Core.Libraries;
using Facepunch;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Oxide.Plugins
{
[Info("Telegram Status", "crester", "1.0.0")]
[Description("Telegram Status Plugin")]
public class TelegramStatus : RustPlugin
{
public static TelegramStatus _plugin;
void Init()
{
_plugin = this;
}
string BotID = "<Bot ID>";
string BotToken = "<Bot Token>";
private void SendMessage(string id, string message)
{
webrequest.Enqueue("https://api.telegram.org/bot" + BotID + ":" + BotToken + "/sendMessage?chat_id=" + id + "&text=" + message, null, (code, response) =>
{
Puts("#" + id + " Message sent");
}, this, RequestMethod.GET);
}
string sleepers = "?";
private void RefreshSleepers()
{
sleepers = BasePlayer.sleepingPlayerList.Count.ToString();
}
string online = "?/?";
private void RefreshOnlinePlayers()
{
online = $"{BasePlayer.activePlayerList.Count}/{ConVar.Server.maxplayers}";
}
void OnPlayerDisconnected(BasePlayer pl)
{
timer.Once(2, RefreshOnlinePlayers);
timer.Once(2, RefreshSleepers);
}
void OnPlayerConnected(BasePlayer player)
{
timer.Once(2, RefreshOnlinePlayers);
timer.Once(2, RefreshSleepers);
}
string last = "0";
void OnTick()
{
webrequest.Enqueue("https://api.telegram.org/bot" + BotID + ":" + BotToken + "/getUpdates?offset=-1", null, (code, response) =>
{
if (code == 200)
{
JObject msgs = null;
try
{
msgs = JObject.Parse(response);
string msg_id = (string)msgs.SelectToken("result[0].message.message_id");
string chat_id = (string)msgs.SelectToken("result[0].message.chat.id");
string message = (string)msgs.SelectToken("result[0].message.text");
if (msg_id != last & message == "/status")
{
last = msg_id;
SendMessage(chat_id, "🌎 Current server statistic: " + online + " players online and " + sleepers + " sleepers.");
}
}
catch (Exception ex)
{
Puts("Error: " + ex.Message);
}
};
}, this, RequestMethod.GET);
}
}
}