【问题标题】:Telegram bot parse user's phone number using Ruby?Telegram bot 使用 Ruby 解析用户的电话号码?
【发布时间】:2018-01-02 20:09:31
【问题描述】:

我正在尝试让 Telegram 机器人向用户请求他/她的号码并对其执行操作。

Telegram::Bot::Client.run(token) do |bot|
  bot.listen do |message|
    case message
    when Telegram::Bot::Types::CallbackQuery
    when Telegram::Bot::Types::Message
      case message.text
      when '/help'
        bot.api.send_message(
          chat_id: message.chat.id, 
          text: 'Display help.' 
          )
      end
      if message.contact.phone_number == "1111111" #undefined method
        bot.api.send_message(
          chat_id: message.chat.id, 
          text: 'Number received.' 
          )
      end
    else
      bot.api.send_message(
        chat_id: message.chat.id, 
        text: 'Unknown command.' 
        )
    end
  end
end

我可以获取 Contact 对象,但如何访问该对象的 phone_number 字段?为 Telegram 机器人使用 Ruby 包装器。

【问题讨论】:

  • 您检查过您的联系人对象吗?它提供了哪些方法?你试过在 irb 中玩它吗?
  • 根据 Telegram Bot API,如果我删除了 'phone_number == '11111111' 部分,我可以获得文本回复。

标签: ruby telegram


【解决方案1】:

我通过在查询其phone_number 字段之前为Contact 对象设置一个时间变量来解决此问题。不确定这是否是预期的交互,但它有效。

Telegram::Bot::Client.run(token) do |bot|
  bot.listen do |message|
    if sender = message.contact #Set a variable to the Contact object.
      if sender.phone_number == "11111111"
        bot.api.send_message(
          chat_id: message.chat.id, 
          text: 'Phone number received.' 
          )
      end
    end
  end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 2016-03-27
    • 2017-11-20
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多