简体   繁体   English

电报中用户ID的交换电话号码

[英]Exchange telephone number for user id in Telegram

I wrote a Telegram Bot in groovy and it was a piece of cake. 我在groovy写了一个Telegram Bot,这是一块蛋糕。

Now in order to register propper webhooks I need to get a hold of user's id. 现在,为了注册propper webhooks,我需要获得用户的id。

I read, that I should call auth.sendCode method to start that process. 我读过,我应该调用auth.sendCode方法来启动该进程。

Are there any simpler alternatives to that? 有没有更简单的替代方案?

If not, how can I invoke the sendCode with the smallest effort and possibly w/o any additional dependencies? 如果没有,我如何以最小的努力调用sendCode ,并且可能没有任何其他依赖项? Any examples or pointers using plain java or curl would be good. 使用普通java或curl的任何示例或指针都会很好。

After some research I ended up with a simple solution. 经过一些研究,我得到了一个简单的解决方案。

Instead of authenticating against the Telegram API over MTProto, I reversed the process. 我没有通过MTProto对Telegram API进行身份验证,而是颠倒了这个过程。 I implemented a new bot-command: 我实现了一个新的bot命令:

/login {my-user-id}

so that the user sends his id (can be some generated token later) in Telegram bot chat and the bot sends this message - along with Telegram user id! 这样用户就可以在Telegram bot聊天中发送他的id(稍后可以是一些生成的令牌),并且bot会发送此消息 - 以及Telegram用户ID! - over webhook to my server, where I do the matching and saving. - 通过webhook到我的服务器,我在那里进行匹配和保存。

The implementation looks like this: 实现如下:

switch( json.message.text ){
  case ~/\/login \w+/: 
    String userId
    text.toLowerCase().eachMatch( /\/login (\w+)/ ){ userId = it[ 1 ] }
    String telegramUserId = json.message.from.id
    saveJoin userId, telegramUserId
    break
 }

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

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