到目前为止,我已经能够完全实现电报授权,但不能使用您请求的语言 - 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
这种方法将为您提供更好的理解和介绍整个 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 地址
<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)
<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 SelectEnd 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</p>
以上是如何在 VB.Net 中生成 Telegram 授权密钥?的详细内容。更多信息请关注PHP中文网其他相关文章!