简体   繁体   English

如何检查用户是否是电报组的成员?

[英]How can I check whether a user is a member of a Telegram group?

I'd like to use the Telegram API to confirm that a given user is a member of a named group. 我想使用Telegram API确认给定的用户是命名组的成员。 I can successfully authenticate as the user given their phone number and the code that they get sent, but beyond that I'm pretty lost. 给定用户的电话号码和发送的代码,我可以成功地对其进行身份验证,但是除此之外,我还很迷路。 I thought I could start with client.contactsSearch with @groupname , but that returns me an empty vector. 我以为我可以使用@groupnameclient.contactsSearch开始,但这会返回一个空向量。

I'm using the Kotlogram library from Scala, but it's just a thin wrapper around the MT protocol. 我正在使用Scala的Kotlogram库,但这只是MT协议的一个薄包装。

For Bot API, you can use getChatMember method. 对于Bot API,可以使用getChatMember方法。

The result will contain status for member , left , etc. 结果将包含memberleft等的status

This is what I came up with in the end. 这就是我最后想出的。 I don't know whether it's the optimal approach, though. 不过,我不知道这是否是最佳方法。

val PhoneNumber = "+1..."

val app = new TelegramApp(..., "...", "...", "0.0.1", "0.0.1", "en")

class ApiStorage extends TelegramApiStorage {
    ...
}

val client = Kotlogram.getDefaultClient(app, new ApiStorage)

val sentCode = client.authSendCode(false, PhoneNumber, true)
val code = scala.io.StdIn.readLine()
val authorization = client.authSignIn(PhoneNumber, sentCode.getPhoneCodeHash, code)

val self = authorization.getUser.getAsUser

val name = s"${self.getFirstName} ${self.getLastName}"

val userInput = new TLInputUser(self.getId, self.getAccessHash)

client.contactsSearch("@groupname", 100).getChats.toArray.map(_.asInstanceOf[TLChannel]).find(_.getUsername == "groupname") match {
    case Some(channel) =>
        val channelInput = new TLInputChannel(channel.getId, channel.getAccessHash)
        val isMember = Try { client.channelsGetParticipant(channelInput, userInput) } map { _ => true } getOrElse false
        if (isMember) {
            println("Is a member")
        } else {
            println("Is not a member")
        }

    case None =>
        println("Can't find channel @groupname")
}

client.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM