|
|
@@ -15,10 +15,11 @@ type
|
|
|
|
TGetGiftEvent = procedure(s: TGiftEvent) of object;
|
|
|
|
TGetGiftEvent = procedure(s: TGiftEvent) of object;
|
|
|
|
TGetSubEvent = procedure(s: TSubEvent) of object;
|
|
|
|
TGetSubEvent = procedure(s: TSubEvent) of object;
|
|
|
|
TGetRaidEvent = procedure(s: TRaidEvent) of object;
|
|
|
|
TGetRaidEvent = procedure(s: TRaidEvent) of object;
|
|
|
|
TOnLog = procedure(aModul: string; aMethod: string; aMessage: string; aLevel: integer) of object;
|
|
|
|
TOnLog = procedure(aModul: string; aMethod: string; aMessage: string;
|
|
|
|
|
|
|
|
aLevel: integer) of object;
|
|
|
|
|
|
|
|
|
|
|
|
TOnStatus = procedure(Sender: TObject; const ConnectionEvent: String;
|
|
|
|
TOnStatus = procedure(Sender: TObject; const ConnectionEvent: String;
|
|
|
|
StatusCode: Integer; const Description: String) of Object;
|
|
|
|
StatusCode: integer; const Description: String) of Object;
|
|
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
type
|
|
|
|
TTTW_ES = class(TObject)
|
|
|
|
TTTW_ES = class(TObject)
|
|
|
@@ -43,22 +44,23 @@ type
|
|
|
|
FOnStatus: TOnStatus;
|
|
|
|
FOnStatus: TOnStatus;
|
|
|
|
SW: TWelcomMessage;
|
|
|
|
SW: TWelcomMessage;
|
|
|
|
procedure HandleTimer(Sender: TObject);
|
|
|
|
procedure HandleTimer(Sender: TObject);
|
|
|
|
procedure ipwWSClient1DataIn(Sender: TObject; DataFormat: Integer;
|
|
|
|
procedure ipwWSClient1DataIn(Sender: TObject; DataFormat: integer;
|
|
|
|
const Text: string; const TextB: TBytes; EOM, EOL: Boolean);
|
|
|
|
const Text: string; const TextB: TBytes; EOM, EOL: Boolean);
|
|
|
|
procedure ipwWSPing(Sender: TObject; const Payload: String;
|
|
|
|
procedure ipwWSPing(Sender: TObject; const Payload: String;
|
|
|
|
const PayloadB: TBytes; Response: Boolean);
|
|
|
|
const PayloadB: TBytes; Response: Boolean);
|
|
|
|
procedure ipwWSClient1ConnectionStatus(Sender: TObject;
|
|
|
|
procedure ipwWSClient1ConnectionStatus(Sender: TObject;
|
|
|
|
const ConnectionEvent: String; StatusCode: Integer;
|
|
|
|
const ConnectionEvent: String; StatusCode: integer;
|
|
|
|
const Description: String);
|
|
|
|
const Description: String);
|
|
|
|
procedure ipwWSClientError(Sender: TObject; ErrorCode: Integer;
|
|
|
|
procedure ipwWSClientError(Sender: TObject; ErrorCode: integer;
|
|
|
|
const Description: string);
|
|
|
|
const Description: string);
|
|
|
|
procedure ipwWSClientDisconnected(Sender: TObject; StatusCode: Integer;
|
|
|
|
procedure ipwWSClientDisconnected(Sender: TObject; StatusCode: integer;
|
|
|
|
const Description: String);
|
|
|
|
const Description: String);
|
|
|
|
procedure ipwWSClientHeader(Sender: TObject; const Field: String;
|
|
|
|
procedure ipwWSClientHeader(Sender: TObject; const Field: String;
|
|
|
|
const Value: String);
|
|
|
|
const Value: String);
|
|
|
|
procedure ipwWSClientLog(Sender: TObject; LogLevel: Integer;
|
|
|
|
procedure ipwWSClientLog(Sender: TObject; LogLevel: integer;
|
|
|
|
const aMessage, aLog: string);
|
|
|
|
const aMessage, aLog: string);
|
|
|
|
function subscribeTo(const EventType, Version: string; const Condition: string): Boolean;
|
|
|
|
function subscribeTo(const EventType, Version: string;
|
|
|
|
|
|
|
|
const Condition: string): Boolean;
|
|
|
|
procedure subscribe();
|
|
|
|
procedure subscribe();
|
|
|
|
// function ParseRewardRedeemed(const AJsonString: string): TRewardRedeemed;
|
|
|
|
// function ParseRewardRedeemed(const AJsonString: string): TRewardRedeemed;
|
|
|
|
procedure EventMSG(const AText: string);
|
|
|
|
procedure EventMSG(const AText: string);
|
|
|
@@ -104,18 +106,14 @@ begin
|
|
|
|
raise Exception.CreateFmt('JSON object "%s" not found', [Name]);
|
|
|
|
raise Exception.CreateFmt('JSON object "%s" not found', [Name]);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function SafeGetStr(Parent: TJSONObject; const Name: string): string;
|
|
|
|
function GetStrDef(Obj: TJSONObject; const Name: string;
|
|
|
|
var
|
|
|
|
Default: string = ''): string;
|
|
|
|
V: TJSONValue;
|
|
|
|
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
V := Parent.GetValue(Name);
|
|
|
|
if not Obj.TryGetValue(Name, Result) then
|
|
|
|
if Assigned(V) then
|
|
|
|
Result := Default;
|
|
|
|
Result := V.Value
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
Result := '';
|
|
|
|
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function SafeGetInt(Parent: TJSONObject; const Name: string): Integer;
|
|
|
|
function SafeGetInt(Parent: TJSONObject; const Name: string): integer;
|
|
|
|
var
|
|
|
|
var
|
|
|
|
V: TJSONValue;
|
|
|
|
V: TJSONValue;
|
|
|
|
begin
|
|
|
|
begin
|
|
|
@@ -143,8 +141,6 @@ begin
|
|
|
|
FOnLog('uTTWEvenSub', aMethod, aMessage, aLevel);
|
|
|
|
FOnLog('uTTWEvenSub', aMethod, aMessage, aLevel);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
procedure TTTW_ES.Connect;
|
|
|
|
procedure TTTW_ES.Connect;
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
|
|
|
|
|
|
|
@@ -152,7 +148,8 @@ begin
|
|
|
|
wss.Disconnect;
|
|
|
|
wss.Disconnect;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
try
|
|
|
|
wss.ConnectTo('wss://eventsub.wss.twitch.tv/ws?keepalive_timeout_seconds=60');
|
|
|
|
wss.ConnectTo
|
|
|
|
|
|
|
|
('wss://eventsub.wss.twitch.tv/ws?keepalive_timeout_seconds=60');
|
|
|
|
toLog(0, 'Connect', 'Ïîäêëþ÷åíèå ê WebSocket âûïîëíåíî');
|
|
|
|
toLog(0, 'Connect', 'Ïîäêëþ÷åíèå ê WebSocket âûïîëíåíî');
|
|
|
|
FTimer.Enabled := True;
|
|
|
|
FTimer.Enabled := True;
|
|
|
|
except
|
|
|
|
except
|
|
|
@@ -178,7 +175,7 @@ begin
|
|
|
|
wss.OnDisconnected := ipwWSClientDisconnected;
|
|
|
|
wss.OnDisconnected := ipwWSClientDisconnected;
|
|
|
|
wss.OnHeader := ipwWSClientHeader;
|
|
|
|
wss.OnHeader := ipwWSClientHeader;
|
|
|
|
|
|
|
|
|
|
|
|
FTimer := TTimer.Create(nil);
|
|
|
|
FTimer := ttimer.Create(nil);
|
|
|
|
FTimer.Interval := 9000;
|
|
|
|
FTimer.Interval := 9000;
|
|
|
|
FTimer.OnTimer := HandleTimer;
|
|
|
|
FTimer.OnTimer := HandleTimer;
|
|
|
|
FTimer.Enabled := False;
|
|
|
|
FTimer.Enabled := False;
|
|
|
@@ -210,6 +207,7 @@ begin
|
|
|
|
try
|
|
|
|
try
|
|
|
|
if wss.Connected then
|
|
|
|
if wss.Connected then
|
|
|
|
wss.Disconnect;
|
|
|
|
wss.Disconnect;
|
|
|
|
|
|
|
|
FTimer.Enabled := False;
|
|
|
|
except
|
|
|
|
except
|
|
|
|
on E: Exception do
|
|
|
|
on E: Exception do
|
|
|
|
toLog(2, 'Disconnect', E.ClassName + ': ' + E.Message);
|
|
|
|
toLog(2, 'Disconnect', E.ClassName + ': ' + E.Message);
|
|
|
@@ -220,11 +218,16 @@ procedure TTTW_ES.EventMSG(const AText: string);
|
|
|
|
var
|
|
|
|
var
|
|
|
|
md: TMetadata;
|
|
|
|
md: TMetadata;
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
|
|
|
|
TThread.Queue(nil,
|
|
|
|
|
|
|
|
procedure
|
|
|
|
|
|
|
|
begin
|
|
|
|
|
|
|
|
|
|
|
|
if Assigned(FOnRAW) then
|
|
|
|
if Assigned(FOnRAW) then
|
|
|
|
FOnRAW(AText);
|
|
|
|
FOnRAW(AText);
|
|
|
|
|
|
|
|
|
|
|
|
md := ParseMetadata(AText);
|
|
|
|
md := ParseMetadata(AText);
|
|
|
|
toLog(0, 'EventMSG', 'Òèï ñîîáùåíèÿ: ' + md.message_type + ', Òèï ïîäïèñêè: ' + md.subscription_type);
|
|
|
|
toLog(0, 'EventMSG', 'Òèï ñîîáùåíèÿ: ' + md.message_type +
|
|
|
|
|
|
|
|
', Òèï ïîäïèñêè: ' + md.subscription_type);
|
|
|
|
|
|
|
|
|
|
|
|
if md.message_type = 'session_welcome' then
|
|
|
|
if md.message_type = 'session_welcome' then
|
|
|
|
begin
|
|
|
|
begin
|
|
|
@@ -236,29 +239,29 @@ begin
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else if md.message_type = 'notification' then
|
|
|
|
else if md.message_type = 'notification' then
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
if md.subscription_type = 'channel.channel_points_custom_reward_redemption.add' then
|
|
|
|
if Assigned(FOnGetCustomReward) and
|
|
|
|
if Assigned(FOnGetCustomReward) then
|
|
|
|
(md.subscription_type = 'channel.channel_points_custom_reward_redemption.add')
|
|
|
|
|
|
|
|
then
|
|
|
|
FOnGetCustomReward(ParseCustomRewardEvent(AText));
|
|
|
|
FOnGetCustomReward(ParseCustomRewardEvent(AText));
|
|
|
|
|
|
|
|
|
|
|
|
if md.subscription_type = 'channel.follow' then
|
|
|
|
if Assigned(FOnFollow) and (md.subscription_type = 'channel.follow')
|
|
|
|
if Assigned(FOnFollow) then
|
|
|
|
then
|
|
|
|
FOnFollow(ParseFollowEvent(AText));
|
|
|
|
FOnFollow(ParseFollowEvent(AText));
|
|
|
|
|
|
|
|
|
|
|
|
if md.subscription_type = 'channel.subscribe' then
|
|
|
|
if Assigned(FOnSub) and (md.subscription_type = 'channel.subscribe')
|
|
|
|
if Assigned(FOnSub) then
|
|
|
|
then
|
|
|
|
FOnSub(ParseSubEvent(AText));
|
|
|
|
FOnSub(ParseSubEvent(AText));
|
|
|
|
|
|
|
|
|
|
|
|
if md.subscription_type = 'channel.subscription.gift' then
|
|
|
|
if Assigned(FOnGift) and
|
|
|
|
if Assigned(FOnGift) then
|
|
|
|
(md.subscription_type = 'channel.subscription.gift') then
|
|
|
|
FOnGift(ParseGiftEvent(AText));
|
|
|
|
FOnGift(ParseGiftEvent(AText));
|
|
|
|
|
|
|
|
|
|
|
|
if md.subscription_type = 'channel.raid' then
|
|
|
|
if Assigned(FOnRaid) and (md.subscription_type = 'channel.raid') then
|
|
|
|
if Assigned(FOnRaid) then
|
|
|
|
|
|
|
|
FOnRaid(ParseRaidEvent(AText));
|
|
|
|
FOnRaid(ParseRaidEvent(AText));
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else if md.message_type = 'session_keepalive' then
|
|
|
|
else if md.message_type = 'session_keepalive' then
|
|
|
|
toLog(3, 'EventMSG', 'Ïîëó÷åí keepalive');
|
|
|
|
toLog(3, 'EventMSG', 'Ïîëó÷åí keepalive');
|
|
|
|
|
|
|
|
end);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TTTW_ES.HandleTimer(Sender: TObject);
|
|
|
|
procedure TTTW_ES.HandleTimer(Sender: TObject);
|
|
|
@@ -271,32 +274,32 @@ if wss.Connected then
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TTTW_ES.ipwWSClient1ConnectionStatus(Sender: TObject;
|
|
|
|
procedure TTTW_ES.ipwWSClient1ConnectionStatus(Sender: TObject;
|
|
|
|
const ConnectionEvent: String; StatusCode: Integer;
|
|
|
|
const ConnectionEvent: String; StatusCode: integer; const Description: String);
|
|
|
|
const Description: String);
|
|
|
|
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
toLog(0, 'ConnectionStatus',
|
|
|
|
toLog(0, 'ConnectionStatus', Format('%s | %d | %s', [ConnectionEvent,
|
|
|
|
Format('%s | %d | %s', [ConnectionEvent, StatusCode, Description]));
|
|
|
|
StatusCode, Description]));
|
|
|
|
if Assigned(FOnStatus) then
|
|
|
|
if Assigned(FOnStatus) then
|
|
|
|
FOnStatus(Sender, ConnectionEvent, StatusCode, Description);
|
|
|
|
FOnStatus(Sender, ConnectionEvent, StatusCode, Description);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TTTW_ES.ipwWSClient1DataIn(Sender: TObject; DataFormat: Integer;
|
|
|
|
procedure TTTW_ES.ipwWSClient1DataIn(Sender: TObject; DataFormat: integer;
|
|
|
|
const Text: string; const TextB: TBytes; EOM, EOL: Boolean);
|
|
|
|
const Text: string; const TextB: TBytes; EOM, EOL: Boolean);
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
toLog(3, 'ipwWSClient1DataIn', Text);
|
|
|
|
toLog(3, 'ipwWSClient1DataIn', Text);
|
|
|
|
EventMSG(Text);
|
|
|
|
EventMSG(Text);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TTTW_ES.ipwWSClientDisconnected(Sender: TObject; StatusCode: Integer;
|
|
|
|
procedure TTTW_ES.ipwWSClientDisconnected(Sender: TObject; StatusCode: integer;
|
|
|
|
const Description: String);
|
|
|
|
const Description: String);
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
toLog(1, 'ipwWSClientDisconnected', Description);
|
|
|
|
toLog(1, 'ipwWSClientDisconnected', Description);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TTTW_ES.ipwWSClientError(Sender: TObject; ErrorCode: Integer;
|
|
|
|
procedure TTTW_ES.ipwWSClientError(Sender: TObject; ErrorCode: integer;
|
|
|
|
const Description: string);
|
|
|
|
const Description: string);
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
toLog(2, 'ipwWSClientError', Format('Êîä: %d | %s', [ErrorCode, Description]));
|
|
|
|
toLog(2, 'ipwWSClientError', Format('Êîä: %d | %s',
|
|
|
|
|
|
|
|
[ErrorCode, Description]));
|
|
|
|
if Assigned(FOnError) then
|
|
|
|
if Assigned(FOnError) then
|
|
|
|
FOnError(Description);
|
|
|
|
FOnError(Description);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
@@ -309,7 +312,7 @@ begin
|
|
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TTTW_ES.ipwWSClientLog(Sender: TObject; LogLevel: Integer;
|
|
|
|
procedure TTTW_ES.ipwWSClientLog(Sender: TObject; LogLevel: integer;
|
|
|
|
const aMessage, aLog: string);
|
|
|
|
const aMessage, aLog: string);
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
// toLog(3, 'ipwWSClientLog', 'Level: ' + IntToStr(LogLevel)
|
|
|
|
// toLog(3, 'ipwWSClientLog', 'Level: ' + IntToStr(LogLevel)
|
|
|
@@ -334,10 +337,10 @@ begin
|
|
|
|
raise Exception.Create('Invalid JSON');
|
|
|
|
raise Exception.Create('Invalid JSON');
|
|
|
|
try
|
|
|
|
try
|
|
|
|
Metadata := SafeGetObj(Root, 'metadata');
|
|
|
|
Metadata := SafeGetObj(Root, 'metadata');
|
|
|
|
Result.message_id := SafeGetStr(Metadata, 'message_id');
|
|
|
|
Result.message_id := GetStrDef(Metadata, 'message_id');
|
|
|
|
Result.message_type := SafeGetStr(Metadata, 'message_type');
|
|
|
|
Result.message_type := GetStrDef(Metadata, 'message_type');
|
|
|
|
Result.message_timestamp := SafeGetStr(Metadata, 'message_timestamp');
|
|
|
|
Result.message_timestamp := GetStrDef(Metadata, 'message_timestamp');
|
|
|
|
Result.subscription_type := SafeGetStr(Metadata, 'subscription_type');
|
|
|
|
Result.subscription_type := GetStrDef(Metadata, 'subscription_type');
|
|
|
|
finally
|
|
|
|
finally
|
|
|
|
Root.Free;
|
|
|
|
Root.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
@@ -353,11 +356,12 @@ begin
|
|
|
|
try
|
|
|
|
try
|
|
|
|
Payload := SafeGetObj(Root, 'payload');
|
|
|
|
Payload := SafeGetObj(Root, 'payload');
|
|
|
|
Session := SafeGetObj(Payload, 'session');
|
|
|
|
Session := SafeGetObj(Payload, 'session');
|
|
|
|
Result.Payload.session.id := SafeGetStr(Session, 'id');
|
|
|
|
Result.Payload.Session.id := GetStrDef(Session, 'id');
|
|
|
|
Result.Payload.session.status := SafeGetStr(Session, 'status');
|
|
|
|
Result.Payload.Session.status := GetStrDef(Session, 'status');
|
|
|
|
Result.Payload.session.connected_at := SafeGetStr(Session, 'connected_at');
|
|
|
|
Result.Payload.Session.connected_at := GetStrDef(Session, 'connected_at');
|
|
|
|
Result.Payload.session.keepalive_timeout_seconds := SafeGetInt(Session, 'keepalive_timeout_seconds');
|
|
|
|
Result.Payload.Session.keepalive_timeout_seconds :=
|
|
|
|
Result.Payload.session.reconnect_url := SafeGetStr(Session, 'reconnect_url');
|
|
|
|
SafeGetInt(Session, 'keepalive_timeout_seconds');
|
|
|
|
|
|
|
|
Result.Payload.Session.reconnect_url := GetStrDef(Session, 'reconnect_url');
|
|
|
|
finally
|
|
|
|
finally
|
|
|
|
Root.Free;
|
|
|
|
Root.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
@@ -366,7 +370,8 @@ end;
|
|
|
|
function TTTW_ES.ParseCustomRewardEvent(const JSONString: string)
|
|
|
|
function TTTW_ES.ParseCustomRewardEvent(const JSONString: string)
|
|
|
|
: TCustomRewardEvent;
|
|
|
|
: TCustomRewardEvent;
|
|
|
|
var
|
|
|
|
var
|
|
|
|
Root, Payload, Subscription, mCondition, mTransport, Event, mReward: TJSONObject;
|
|
|
|
Root, Payload, Subscription, mCondition, mTransport, Event,
|
|
|
|
|
|
|
|
mReward: TJSONObject;
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
toLog(3, 'ParseCustomRewardEvent', 'Íà÷àëî ïàðñèíãà íàãðàäû');
|
|
|
|
toLog(3, 'ParseCustomRewardEvent', 'Íà÷àëî ïàðñèíãà íàãðàäû');
|
|
|
|
Root := TJSONObject.ParseJSONValue(JSONString) as TJSONObject;
|
|
|
|
Root := TJSONObject.ParseJSONValue(JSONString) as TJSONObject;
|
|
|
@@ -377,35 +382,36 @@ begin
|
|
|
|
Subscription := SafeGetObj(Payload, 'subscription');
|
|
|
|
Subscription := SafeGetObj(Payload, 'subscription');
|
|
|
|
with Result.Subscription do
|
|
|
|
with Result.Subscription do
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
id := SafeGetStr(Subscription, 'id');
|
|
|
|
id := GetStrDef(Subscription, 'id');
|
|
|
|
subscription_type := SafeGetStr(Subscription, 'type');
|
|
|
|
subscription_type := GetStrDef(Subscription, 'type');
|
|
|
|
version := SafeGetStr(Subscription, 'version');
|
|
|
|
Version := GetStrDef(Subscription, 'version');
|
|
|
|
status := SafeGetStr(Subscription, 'status');
|
|
|
|
status := GetStrDef(Subscription, 'status');
|
|
|
|
cost := SafeGetInt(Subscription, 'cost');
|
|
|
|
cost := SafeGetInt(Subscription, 'cost');
|
|
|
|
created_at := SafeGetStr(Subscription, 'created_at');
|
|
|
|
created_at := GetStrDef(Subscription, 'created_at');
|
|
|
|
mCondition := SafeGetObj(Subscription, 'condition');
|
|
|
|
mCondition := SafeGetObj(Subscription, 'condition');
|
|
|
|
condition.broadcaster_user_id := SafeGetStr(mCondition, 'broadcaster_user_id');
|
|
|
|
Condition.broadcaster_user_id :=
|
|
|
|
condition.reward_id := SafeGetStr(mCondition, 'reward_id');
|
|
|
|
GetStrDef(mCondition, 'broadcaster_user_id');
|
|
|
|
|
|
|
|
Condition.reward_id := GetStrDef(mCondition, 'reward_id');
|
|
|
|
mTransport := SafeGetObj(Subscription, 'transport');
|
|
|
|
mTransport := SafeGetObj(Subscription, 'transport');
|
|
|
|
transport.method := SafeGetStr(mTransport, 'method');
|
|
|
|
transport.method := GetStrDef(mTransport, 'method');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
Event := SafeGetObj(Payload, 'event');
|
|
|
|
Event := SafeGetObj(Payload, 'event');
|
|
|
|
with Result.Event do
|
|
|
|
with Result.Event do
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
id := SafeGetStr(Event, 'id');
|
|
|
|
id := GetStrDef(Event, 'id');
|
|
|
|
broadcaster_user_id := SafeGetStr(Event, 'broadcaster_user_id');
|
|
|
|
broadcaster_user_id := GetStrDef(Event, 'broadcaster_user_id');
|
|
|
|
broadcaster_user_login := SafeGetStr(Event, 'broadcaster_user_login');
|
|
|
|
broadcaster_user_login := GetStrDef(Event, 'broadcaster_user_login');
|
|
|
|
broadcaster_user_name := SafeGetStr(Event, 'broadcaster_user_name');
|
|
|
|
broadcaster_user_name := GetStrDef(Event, 'broadcaster_user_name');
|
|
|
|
user_id := SafeGetStr(Event, 'user_id');
|
|
|
|
user_id := GetStrDef(Event, 'user_id');
|
|
|
|
user_login := SafeGetStr(Event, 'user_login');
|
|
|
|
user_login := GetStrDef(Event, 'user_login');
|
|
|
|
user_name := SafeGetStr(Event, 'user_name');
|
|
|
|
user_name := GetStrDef(Event, 'user_name');
|
|
|
|
user_input := SafeGetStr(Event, 'user_input');
|
|
|
|
user_input := GetStrDef(Event, 'user_input');
|
|
|
|
mReward := SafeGetObj(Event, 'reward');
|
|
|
|
mReward := SafeGetObj(Event, 'reward');
|
|
|
|
revard.id := SafeGetStr(mReward, 'id');
|
|
|
|
revard.id := GetStrDef(mReward, 'id');
|
|
|
|
revard.title := SafeGetStr(mReward, 'title');
|
|
|
|
revard.title := GetStrDef(mReward, 'title');
|
|
|
|
revard.cost := SafeGetInt(mReward, 'cost');
|
|
|
|
revard.cost := SafeGetInt(mReward, 'cost');
|
|
|
|
revard.prompt := SafeGetStr(mReward, 'prompt');
|
|
|
|
revard.prompt := GetStrDef(mReward, 'prompt');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
finally
|
|
|
|
Root.Free;
|
|
|
|
Root.Free;
|
|
|
@@ -425,28 +431,29 @@ begin
|
|
|
|
Subscription := SafeGetObj(Payload, 'subscription');
|
|
|
|
Subscription := SafeGetObj(Payload, 'subscription');
|
|
|
|
with Result.Subscription do
|
|
|
|
with Result.Subscription do
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
id := SafeGetStr(Subscription, 'id');
|
|
|
|
id := GetStrDef(Subscription, 'id');
|
|
|
|
subscription_type := SafeGetStr(Subscription, 'type');
|
|
|
|
subscription_type := GetStrDef(Subscription, 'type');
|
|
|
|
version := SafeGetStr(Subscription, 'version');
|
|
|
|
Version := GetStrDef(Subscription, 'version');
|
|
|
|
status := SafeGetStr(Subscription, 'status');
|
|
|
|
status := GetStrDef(Subscription, 'status');
|
|
|
|
cost := SafeGetInt(Subscription, 'cost');
|
|
|
|
cost := SafeGetInt(Subscription, 'cost');
|
|
|
|
created_at := SafeGetStr(Subscription, 'created_at');
|
|
|
|
created_at := GetStrDef(Subscription, 'created_at');
|
|
|
|
mCondition := SafeGetObj(Subscription, 'condition');
|
|
|
|
mCondition := SafeGetObj(Subscription, 'condition');
|
|
|
|
condition.broadcaster_user_id := SafeGetStr(mCondition, 'broadcaster_user_id');
|
|
|
|
Condition.broadcaster_user_id :=
|
|
|
|
|
|
|
|
GetStrDef(mCondition, 'broadcaster_user_id');
|
|
|
|
mTransport := SafeGetObj(Subscription, 'transport');
|
|
|
|
mTransport := SafeGetObj(Subscription, 'transport');
|
|
|
|
transport.method := SafeGetStr(mTransport, 'method');
|
|
|
|
transport.method := GetStrDef(mTransport, 'method');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
Event := SafeGetObj(Payload, 'event');
|
|
|
|
Event := SafeGetObj(Payload, 'event');
|
|
|
|
with Result.Event do
|
|
|
|
with Result.Event do
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
broadcaster_user_id := SafeGetStr(Event, 'broadcaster_user_id');
|
|
|
|
broadcaster_user_id := GetStrDef(Event, 'broadcaster_user_id');
|
|
|
|
broadcaster_user_login := SafeGetStr(Event, 'broadcaster_user_login');
|
|
|
|
broadcaster_user_login := GetStrDef(Event, 'broadcaster_user_login');
|
|
|
|
broadcaster_user_name := SafeGetStr(Event, 'broadcaster_user_name');
|
|
|
|
broadcaster_user_name := GetStrDef(Event, 'broadcaster_user_name');
|
|
|
|
user_id := SafeGetStr(Event, 'user_id');
|
|
|
|
user_id := GetStrDef(Event, 'user_id');
|
|
|
|
user_login := SafeGetStr(Event, 'user_login');
|
|
|
|
user_login := GetStrDef(Event, 'user_login');
|
|
|
|
user_name := SafeGetStr(Event, 'user_name');
|
|
|
|
user_name := GetStrDef(Event, 'user_name');
|
|
|
|
followed_at := SafeGetStr(Event, 'followed_at');
|
|
|
|
followed_at := GetStrDef(Event, 'followed_at');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
finally
|
|
|
|
Root.Free;
|
|
|
|
Root.Free;
|
|
|
@@ -466,29 +473,30 @@ begin
|
|
|
|
Subscription := SafeGetObj(Payload, 'subscription');
|
|
|
|
Subscription := SafeGetObj(Payload, 'subscription');
|
|
|
|
with Result.Subscription do
|
|
|
|
with Result.Subscription do
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
id := SafeGetStr(Subscription, 'id');
|
|
|
|
id := GetStrDef(Subscription, 'id');
|
|
|
|
subscription_type := SafeGetStr(Subscription, 'type');
|
|
|
|
subscription_type := GetStrDef(Subscription, 'type');
|
|
|
|
version := SafeGetStr(Subscription, 'version');
|
|
|
|
Version := GetStrDef(Subscription, 'version');
|
|
|
|
status := SafeGetStr(Subscription, 'status');
|
|
|
|
status := GetStrDef(Subscription, 'status');
|
|
|
|
cost := SafeGetInt(Subscription, 'cost');
|
|
|
|
cost := SafeGetInt(Subscription, 'cost');
|
|
|
|
created_at := SafeGetStr(Subscription, 'created_at');
|
|
|
|
created_at := GetStrDef(Subscription, 'created_at');
|
|
|
|
mCondition := SafeGetObj(Subscription, 'condition');
|
|
|
|
mCondition := SafeGetObj(Subscription, 'condition');
|
|
|
|
condition.broadcaster_user_id := SafeGetStr(mCondition, 'broadcaster_user_id');
|
|
|
|
Condition.broadcaster_user_id :=
|
|
|
|
|
|
|
|
GetStrDef(mCondition, 'broadcaster_user_id');
|
|
|
|
mTransport := SafeGetObj(Subscription, 'transport');
|
|
|
|
mTransport := SafeGetObj(Subscription, 'transport');
|
|
|
|
transport.method := SafeGetStr(mTransport, 'method');
|
|
|
|
transport.method := GetStrDef(mTransport, 'method');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
Event := SafeGetObj(Payload, 'event');
|
|
|
|
Event := SafeGetObj(Payload, 'event');
|
|
|
|
with Result.Event do
|
|
|
|
with Result.Event do
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
broadcaster_user_id := SafeGetStr(Event, 'broadcaster_user_id');
|
|
|
|
broadcaster_user_id := GetStrDef(Event, 'broadcaster_user_id');
|
|
|
|
broadcaster_user_login := SafeGetStr(Event, 'broadcaster_user_login');
|
|
|
|
broadcaster_user_login := GetStrDef(Event, 'broadcaster_user_login');
|
|
|
|
broadcaster_user_name := SafeGetStr(Event, 'broadcaster_user_name');
|
|
|
|
broadcaster_user_name := GetStrDef(Event, 'broadcaster_user_name');
|
|
|
|
user_id := SafeGetStr(Event, 'user_id');
|
|
|
|
user_id := GetStrDef(Event, 'user_id');
|
|
|
|
user_login := SafeGetStr(Event, 'user_login');
|
|
|
|
user_login := GetStrDef(Event, 'user_login');
|
|
|
|
user_name := SafeGetStr(Event, 'user_name');
|
|
|
|
user_name := GetStrDef(Event, 'user_name');
|
|
|
|
total := SafeGetInt(Event, 'total');
|
|
|
|
total := SafeGetInt(Event, 'total');
|
|
|
|
tier := SafeGetStr(Event, 'tier');
|
|
|
|
tier := GetStrDef(Event, 'tier');
|
|
|
|
cumulative_total := SafeGetInt(Event, 'cumulative_total');
|
|
|
|
cumulative_total := SafeGetInt(Event, 'cumulative_total');
|
|
|
|
is_anonymous := SafeGetBool(Event, 'anonymous');
|
|
|
|
is_anonymous := SafeGetBool(Event, 'anonymous');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
@@ -497,7 +505,6 @@ begin
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function TTTW_ES.ParseRaidEvent(const JSONString: string): TRaidEvent;
|
|
|
|
function TTTW_ES.ParseRaidEvent(const JSONString: string): TRaidEvent;
|
|
|
|
var
|
|
|
|
var
|
|
|
|
Root, Payload, Subscription, mCondition, mTransport, Event: TJSONObject;
|
|
|
|
Root, Payload, Subscription, mCondition, mTransport, Event: TJSONObject;
|
|
|
@@ -511,27 +518,31 @@ begin
|
|
|
|
Subscription := SafeGetObj(Payload, 'subscription');
|
|
|
|
Subscription := SafeGetObj(Payload, 'subscription');
|
|
|
|
with Result.Subscription do
|
|
|
|
with Result.Subscription do
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
id := SafeGetStr(Subscription, 'id');
|
|
|
|
id := GetStrDef(Subscription, 'id');
|
|
|
|
subscription_type := SafeGetStr(Subscription, 'type');
|
|
|
|
subscription_type := GetStrDef(Subscription, 'type');
|
|
|
|
version := SafeGetStr(Subscription, 'version');
|
|
|
|
Version := GetStrDef(Subscription, 'version');
|
|
|
|
status := SafeGetStr(Subscription, 'status');
|
|
|
|
status := GetStrDef(Subscription, 'status');
|
|
|
|
cost := SafeGetInt(Subscription, 'cost');
|
|
|
|
cost := SafeGetInt(Subscription, 'cost');
|
|
|
|
created_at := SafeGetStr(Subscription, 'created_at');
|
|
|
|
created_at := GetStrDef(Subscription, 'created_at');
|
|
|
|
mCondition := SafeGetObj(Subscription, 'condition');
|
|
|
|
mCondition := SafeGetObj(Subscription, 'condition');
|
|
|
|
condition.broadcaster_user_id := SafeGetStr(mCondition, 'to_broadcaster_user_id');
|
|
|
|
Condition.broadcaster_user_id :=
|
|
|
|
|
|
|
|
GetStrDef(mCondition, 'to_broadcaster_user_id');
|
|
|
|
mTransport := SafeGetObj(Subscription, 'transport');
|
|
|
|
mTransport := SafeGetObj(Subscription, 'transport');
|
|
|
|
transport.method := SafeGetStr(mTransport, 'method');
|
|
|
|
transport.method := GetStrDef(mTransport, 'method');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
Event := SafeGetObj(Payload, 'event');
|
|
|
|
Event := SafeGetObj(Payload, 'event');
|
|
|
|
with Result.Event do
|
|
|
|
with Result.Event do
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
from_broadcaster_user_id := SafeGetStr(Event, 'from_broadcaster_user_id');
|
|
|
|
from_broadcaster_user_id := GetStrDef(Event, 'from_broadcaster_user_id');
|
|
|
|
from_broadcaster_user_login := SafeGetStr(Event, 'from_broadcaster_user_login');
|
|
|
|
from_broadcaster_user_login :=
|
|
|
|
from_broadcaster_user_name := SafeGetStr(Event, 'from_broadcaster_user_name');
|
|
|
|
GetStrDef(Event, 'from_broadcaster_user_login');
|
|
|
|
to_broadcaster_user_id := SafeGetStr(Event, 'to_broadcaster_user_id');
|
|
|
|
from_broadcaster_user_name :=
|
|
|
|
to_broadcaster_user_login := SafeGetStr(Event, 'to_broadcaster_user_login');
|
|
|
|
GetStrDef(Event, 'from_broadcaster_user_name');
|
|
|
|
to_broadcaster_user_name := SafeGetStr(Event, 'to_broadcaster_user_name');
|
|
|
|
to_broadcaster_user_id := GetStrDef(Event, 'to_broadcaster_user_id');
|
|
|
|
|
|
|
|
to_broadcaster_user_login :=
|
|
|
|
|
|
|
|
GetStrDef(Event, 'to_broadcaster_user_login');
|
|
|
|
|
|
|
|
to_broadcaster_user_name := GetStrDef(Event, 'to_broadcaster_user_name');
|
|
|
|
viewers := SafeGetInt(Event, 'viewers');
|
|
|
|
viewers := SafeGetInt(Event, 'viewers');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
finally
|
|
|
@@ -552,28 +563,29 @@ begin
|
|
|
|
Subscription := SafeGetObj(Payload, 'subscription');
|
|
|
|
Subscription := SafeGetObj(Payload, 'subscription');
|
|
|
|
with Result.Subscription do
|
|
|
|
with Result.Subscription do
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
id := SafeGetStr(Subscription, 'id');
|
|
|
|
id := GetStrDef(Subscription, 'id');
|
|
|
|
subscription_type := SafeGetStr(Subscription, 'type');
|
|
|
|
subscription_type := GetStrDef(Subscription, 'type');
|
|
|
|
version := SafeGetStr(Subscription, 'version');
|
|
|
|
Version := GetStrDef(Subscription, 'version');
|
|
|
|
status := SafeGetStr(Subscription, 'status');
|
|
|
|
status := GetStrDef(Subscription, 'status');
|
|
|
|
cost := SafeGetInt(Subscription, 'cost');
|
|
|
|
cost := SafeGetInt(Subscription, 'cost');
|
|
|
|
created_at := SafeGetStr(Subscription, 'created_at');
|
|
|
|
created_at := GetStrDef(Subscription, 'created_at');
|
|
|
|
mCondition := SafeGetObj(Subscription, 'condition');
|
|
|
|
mCondition := SafeGetObj(Subscription, 'condition');
|
|
|
|
condition.broadcaster_user_id := SafeGetStr(mCondition, 'broadcaster_user_id');
|
|
|
|
Condition.broadcaster_user_id :=
|
|
|
|
|
|
|
|
GetStrDef(mCondition, 'broadcaster_user_id');
|
|
|
|
mTransport := SafeGetObj(Subscription, 'transport');
|
|
|
|
mTransport := SafeGetObj(Subscription, 'transport');
|
|
|
|
transport.method := SafeGetStr(mTransport, 'method');
|
|
|
|
transport.method := GetStrDef(mTransport, 'method');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
Event := SafeGetObj(Payload, 'event');
|
|
|
|
Event := SafeGetObj(Payload, 'event');
|
|
|
|
with Result.Event do
|
|
|
|
with Result.Event do
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
broadcaster_user_id := SafeGetStr(Event, 'broadcaster_user_id');
|
|
|
|
broadcaster_user_id := GetStrDef(Event, 'broadcaster_user_id');
|
|
|
|
broadcaster_user_login := SafeGetStr(Event, 'broadcaster_user_login');
|
|
|
|
broadcaster_user_login := GetStrDef(Event, 'broadcaster_user_login');
|
|
|
|
broadcaster_user_name := SafeGetStr(Event, 'broadcaster_user_name');
|
|
|
|
broadcaster_user_name := GetStrDef(Event, 'broadcaster_user_name');
|
|
|
|
user_id := SafeGetStr(Event, 'user_id');
|
|
|
|
user_id := GetStrDef(Event, 'user_id');
|
|
|
|
user_login := SafeGetStr(Event, 'user_login');
|
|
|
|
user_login := GetStrDef(Event, 'user_login');
|
|
|
|
user_name := SafeGetStr(Event, 'user_name');
|
|
|
|
user_name := GetStrDef(Event, 'user_name');
|
|
|
|
tier := SafeGetStr(Event, 'tier');
|
|
|
|
tier := GetStrDef(Event, 'tier');
|
|
|
|
is_gift := SafeGetBool(Event, 'is_gift');
|
|
|
|
is_gift := SafeGetBool(Event, 'is_gift');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
finally
|
|
|
@@ -581,34 +593,30 @@ begin
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TTTW_ES.subscribeTo(const EventType, Version: string; const Condition: string): Boolean;
|
|
|
|
function TTTW_ES.subscribeTo(const EventType, Version: string;
|
|
|
|
|
|
|
|
const Condition: string): Boolean;
|
|
|
|
var
|
|
|
|
var
|
|
|
|
Json: TStringStream;
|
|
|
|
JSON: TStringStream;
|
|
|
|
Resp: string;
|
|
|
|
Resp: string;
|
|
|
|
HTTP: TNetHTTPClient;
|
|
|
|
HTTP: TNetHTTPClient;
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
Result := False;
|
|
|
|
toLog(0, 'subscribeTo', 'Ïîäïèñêà íà ' + EventType);
|
|
|
|
toLog(0, 'subscribeTo', 'Ïîäïèñêà íà ' + EventType);
|
|
|
|
|
|
|
|
try
|
|
|
|
HTTP := TNetHTTPClient.Create(nil);
|
|
|
|
HTTP := TNetHTTPClient.Create(nil);
|
|
|
|
try
|
|
|
|
try
|
|
|
|
HTTP.ContentType := 'application/json';
|
|
|
|
HTTP.ContentType := 'application/json';
|
|
|
|
HTTP.CustomHeaders['Authorization'] := 'Bearer ' + FAccessToken;
|
|
|
|
HTTP.CustomHeaders['Authorization'] := 'Bearer ' + FAccessToken;
|
|
|
|
HTTP.CustomHeaders['Client-Id'] := FClientID;
|
|
|
|
HTTP.CustomHeaders['Client-Id'] := FClientID;
|
|
|
|
|
|
|
|
|
|
|
|
Json := TStringStream.Create(
|
|
|
|
JSON := TStringStream.Create(TJSONObject.Create.AddPair('type', EventType)
|
|
|
|
TJSONObject.Create
|
|
|
|
.AddPair('version', Version).AddPair('condition',
|
|
|
|
.AddPair('type', EventType)
|
|
|
|
TJSONObject.ParseJSONValue(Condition) as TJSONObject)
|
|
|
|
.AddPair('version', Version)
|
|
|
|
.AddPair('transport', TJSONObject.Create.AddPair('method', 'websocket')
|
|
|
|
.AddPair('condition', TJSONObject.ParseJSONValue(Condition) as TJSONObject)
|
|
|
|
.AddPair('session_id', SW.Payload.Session.id)).ToJSON, TEncoding.UTF8);
|
|
|
|
.AddPair('transport',
|
|
|
|
|
|
|
|
TJSONObject.Create
|
|
|
|
|
|
|
|
.AddPair('method', 'websocket')
|
|
|
|
|
|
|
|
.AddPair('session_id', SW.Payload.session.id)
|
|
|
|
|
|
|
|
).ToJSON, TEncoding.UTF8
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
try
|
|
|
|
try
|
|
|
|
Resp := HTTP.Post('https://api.twitch.tv/helix/eventsub/subscriptions', Json).ContentAsString();
|
|
|
|
Resp := HTTP.Post('https://api.twitch.tv/helix/eventsub/subscriptions',
|
|
|
|
|
|
|
|
JSON).ContentAsString();
|
|
|
|
toLog(3, 'subscribeTo', 'Îòâåò Twitch: ' + Resp);
|
|
|
|
toLog(3, 'subscribeTo', 'Îòâåò Twitch: ' + Resp);
|
|
|
|
|
|
|
|
|
|
|
|
if Pos('"status":"enabled"', Resp) > 0 then
|
|
|
|
if Pos('"status":"enabled"', Resp) > 0 then
|
|
|
@@ -619,7 +627,11 @@ begin
|
|
|
|
else
|
|
|
|
else
|
|
|
|
toLog(1, 'subscribeTo', 'Ïîäïèñêà íå ïîäòâåðæäåíà: ' + Resp);
|
|
|
|
toLog(1, 'subscribeTo', 'Ïîäïèñêà íå ïîäòâåðæäåíà: ' + Resp);
|
|
|
|
finally
|
|
|
|
finally
|
|
|
|
Json.Free;
|
|
|
|
JSON.Free;
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
|
|
|
HTTP.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
except
|
|
|
|
on E: Exception do
|
|
|
|
on E: Exception do
|
|
|
|