到目前為止,我已經能夠完全實現電報授權,但不能使用您請求的語言 - PHP,我使用了 vb.Net。不過,我相信同樣的邏輯應該也適用。
建立 Telegram 授權金鑰
Telegram API 並非易事在公園裡。研究現有的 src 程式碼可能非常艱鉅(恕我直言)。因此,我的方法是研究線上 API 文件並實作連結中概述的範例 auth_key下面。
https://core.telegram.org/mtproto/auth_key
https://core.telegram.org/mtproto/ Samples-auth_key p>
這種方法將為您提供更好的理解和介紹整個Telegram API 中使用的原語,可能會幫助您開始組織自己的一組函數和例程來處理後續步驟所需的內容- 實作 API 的其他功能,因為產生 AuthKey 只是一個開始。 /p>
第1 步
所有通訊均通過TCP - 一旦您已獲得唯一的api_id (https://core.telegram. org/api/obtaining_api_id#obtaining-api-id),您將發現以下廣告用於測試的IP: 149.154.167.40:433此時產生api_id 不是必要的AuthKey
設定發送/接收TCP 處理循環的首選方法
我擁有的是一個私有SendData,它只是將位元組傳送到活動套接字連接到上面給定的IP位址
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">If Not IsConnected() Then Log("Connection Closed!", ConsoleColor.DarkRed) RaiseEvent Disconneted() Exit Sub End If b = TCPPack(b) Dim arg = New SocketAsyncEventArgs With {.RemoteEndPoint = ep} AddHandler arg.Completed, AddressOf IO_Handler arg.SetBuffer(b, 0, b.Length) Try If Not soc.SendAsync(arg) Then IO_Handler(soc, arg) End If If read Then ReadData() End If Catch ex As Exception Log("SendData: " & ex.ToString, ConsoleColor.Red) End Try
End Sub
Private Sub ReadData(可選wait As Integer = 0)
If Not IsConnected() Then Log("Connection Closed!", ConsoleColor.DarkRed) RaiseEvent Disconneted() Exit Sub End If Dim arg = New SocketAsyncEventArgs With {.RemoteEndPoint = ep} AddHandler arg.Completed, AddressOf IO_Handler Dim b(BUFFER_SIZE - 1) As Byte arg.SetBuffer(b, 0, BUFFER_SIZE) Try If Not soc.ReceiveAsync(arg) Then IO_Handler(soc, arg) End If Catch ex As Exception Log("ReadMessages: " & ex.ToString, ConsoleColor.Red) End Try
End Sub
Private Sub IO_Handler(sender 作為對象,e 作為SocketAsyncEventArgs)
Log($"{e.LastOperation}:{e.SocketError}:{e.BytesTransferred}", ConsoleColor.Cyan) Select Case e.SocketError Case SocketError.Success Select Case e.LastOperation Case SocketAsyncOperation.Connect 'A socket Connect operation. Log("Connected to " & e.ConnectSocket.RemoteEndPoint.ToString, ConsoleColor.Green) are.Set() Case SocketAsyncOperation.Disconnect, SocketAsyncOperation.Connect RaiseEvent Disconneted() Case SocketAsyncOperation.Receive 'A socket Receive operation. HandleData(e) End Select Case SocketError.ConnectionAborted RaiseEvent Disconneted() End Select
結束Sub
私人 Sub HandleData(e As SocketAsyncEventArgs)
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">If Not IsConnected() Then Log("Connection Closed!", ConsoleColor.DarkRed) RaiseEvent Disconneted() Exit Sub End If b = TCPPack(b) Dim arg = New SocketAsyncEventArgs With {.RemoteEndPoint = ep} AddHandler arg.Completed, AddressOf IO_Handler arg.SetBuffer(b, 0, b.Length) Try If Not soc.SendAsync(arg) Then IO_Handler(soc, arg) End If If read Then ReadData() End If Catch ex As Exception Log("SendData: " & ex.ToString, ConsoleColor.Red) End TryEnd Sub
最後,對於這一步,我們需要一個 TcpPack() 方法,它可以幫助我們以 Telegram 期望的格式填充資料 -請參考下面的程式碼有註解
If Not IsConnected() Then Log("Connection Closed!", ConsoleColor.DarkRed) RaiseEvent Disconneted() Exit Sub End If Dim arg = New SocketAsyncEventArgs With {.RemoteEndPoint = ep} AddHandler arg.Completed, AddressOf IO_Handler Dim b(BUFFER_SIZE - 1) As Byte arg.SetBuffer(b, 0, BUFFER_SIZE) Try If Not soc.ReceiveAsync(arg) Then IO_Handler(soc, arg) End If Catch ex As Exception Log("ReadMessages: " & ex.ToString, ConsoleColor.Red) End Try登入後複製完函數
第2 步驟
有了基本的TCP 發送/接收例程設置,我們就可以開始準備了發送到電報的數據包並具有用於處理收到的特定響應的子例程 - ProcessResponse(data)
接下來我們需要了解的是,Telegram 處理2 大類訊息-
這些是純文字訊息,其auth_key_id =0 產生AuthKey 自始至終都使用這種類型的訊息
> ;已加密 - https://core.telegram.org/mtproto/description#encrypted-message-encrypted-data
與Telegram 伺服器的所有進一步通訊都將透過加密訊息進行
我選擇使用兩個類別來封裝這兩種訊息類型。然後我可以有兩個Send(m) 方法來處理每種類型
Log($"{e.LastOperation}:{e.SocketError}:{e.BytesTransferred}", ConsoleColor.Cyan) Select Case e.SocketError Case SocketError.Success Select Case e.LastOperation Case SocketAsyncOperation.Connect 'A socket Connect operation. Log("Connected to " & e.ConnectSocket.RemoteEndPoint.ToString, ConsoleColor.Green) are.Set() Case SocketAsyncOperation.Disconnect, SocketAsyncOperation.Connect RaiseEvent Disconneted() Case SocketAsyncOperation.Receive 'A socket Receive operation. HandleData(e) End Select Case SocketError.ConnectionAborted RaiseEvent Disconneted() End Select登入後複製End Sub
Private Sub Send(m As EncryptedMessage)
If e.BytesTransferred = 0 Then --no pending data Log("The remote end has closed the connection.") Exit Sub End If Dim len As Integer = e.Buffer(0) Dim start = 1 If len = &H7F Then len = e.Buffer(1) len += e.Buffer(2) << 8 len += e.Buffer(3) << 16 start = 4 End If len = 4 * len Dim data(len - 1) As Byte Array.Copy(e.Buffer, start, data, 0, len) ProcessResponse(data) ReadData()登入後複製結束Sub
目前只需要UnencryptedMessage
<pre class="brush:php;toolbar:false">Dim a = New List(Of Byte) Dim len = CByte(b.Length / 4) If efSent = False Then --TCP abridged version efSent = True a.Add(&HEF) End If If len >= &H7F Then a.Add(&H7F) a.AddRange(BitConverter.GetBytes(len)) Else a.Add(len) End If a.AddRange(b) --only data, no sequence number, no CRC32 Return a.ToArray
以上是如何在 VB.Net 中產生 Telegram 授權金鑰?的詳細內容。更多資訊請關注PHP中文網其他相關文章!