Rails 微信开发多客服遇到的问题

greatbody · 2015年10月16日 · 最后由 greatbody 回复于 2015年11月11日 · 4402 次阅读

首先,在问这个问题前,已经各种 google,百度了,找到的都是 php。 然后,陈述问题:

根据腾讯的解释,每次用户发送消息给公众号的时候,微信会向我们配置的一个路径 post 数据,以便我们进行定制化开发。

为了让传给我们自己后台服务器的消息转发到“多客服”,我们需要回复:

<xml>
    <ToUserName><![CDATA[touser]]></ToUserName>
    <FromUserName><![CDATA[fromuser]]></FromUserName>
    <CreateTime>1399197672</CreateTime>
    <MsgType><![CDATA[transfer_customer_service]]></MsgType>
</xml>

我在一个 controller 里面处理了微信的消息推送

 def weixin_msg
    # 这里面就是消息处理代码,我在最后render了这的xml
   render html: <<-EOS
  <xml>
    <ToUserName><![CDATA[touser]]></ToUserName>
    <FromUserName><![CDATA[fromuser]]></FromUserName>
    <CreateTime>1399197672</CreateTime>
    <MsgType><![CDATA[transfer_customer_service]]></MsgType>
</xml>
   EOS
 end

提交部署后,发现发送消息后,微信提示“该公众号暂时无法提供服务,请稍后再试”

服务器那边出异常。touser、fromuser 也应该有相应的值吧。如果有微信开发相关问题:http://weixin-dev.com/

试试返回一个空字符串作为消息的内容。

#1 楼 @ruby_sky 已经好了。我之前回复消息一直用的是 weixin_authorize 这个 gem 中的方法,本次我就干脆重构了微信接口 route 对应的 method,然后返回 xml 来作为回复消息。现在已经恢复正常了。

#2 楼 @lbp0200 已经好了。我之前回复消息一直用的是 weixin_authorize 这个 gem 中的方法,本次我就干脆重构了微信接口 route 对应的 method,然后返回 xml 来作为回复消息。现在已经恢复正常了。

#3 楼 @greatbody weixin_authorize 并没有将信息转发到多客户服的接口哦(transfer_customer_service),而是在 https://github.com/lanrion/weixin_rails_middleware/blob/master/lib%2Fweixin_rails_middleware%2Fhelpers%2Freply_weixin_message_helper.rb#L117 我一直用得好好的:reply_transfer_customer_service_message

#5 楼 @ruby_sky weixin_authorize 的确没有这个接口。我自己做的,问题已经解决了。 我这么玩的

module Weixin
  class WeixinReply
    def self.reply_text(to_user, from_user, message)
      <<-EOS
      <xml>
      <ToUserName><![CDATA[#{to_user}]]></ToUserName>
      <FromUserName><![CDATA[#{from_user}]]></FromUserName>
      <CreateTime>#{Time.now.to_i}</CreateTime>
      <MsgType><![CDATA[text]]></MsgType>
      <Content><![CDATA[#{message}]]></Content>
      </xml>
      EOS
    end

    def self.transfer_customer_service(to_user, from_user)
      <<-EOS
      <xml>
      <ToUserName><![CDATA[#{to_user}]]></ToUserName>
      <FromUserName><![CDATA[#{from_user}]]></FromUserName>
      <CreateTime>#{Time.now.to_i}</CreateTime>
      <MsgType><![CDATA[transfer_customer_service]]></MsgType>
      </xml>
      EOS
    end
  end
end

然后在代码中,处理微信消息调用的 route 对应的方法中,调用:

# 举例
  def weixin_port
    data = request.body.read
    #get hashtable and make it symbolized with keys
    weixin_hash = Hash.from_xml(data)
    weixin_hash.deep_symbolize_keys! unless weixin_hash.nil?
    openid = weixin_hash[:xml][:FromUserName]
    weixin_hash[:xml][:ToUserName]
    return_msg = Weixin::WeixinReply.transfer_customer_service(openid, businessid)  #转移到客服系统
    render plain: return_msg
  end

#6 楼 @greatbody 多客服转发是在 https://github.com/lanrion/weixin_rails_middleware 这个 gem 的: 如果你使用了 weixin_rails_middleware, 在 pro/app/decorators/controllers/weixin_rails_middleware/weixin_controller_decorator.rb

def response_voice_message(options={})
  reply_transfer_customer_service_message
end

即可。

#7 楼 @ruby_sky 我没有用这个 gem。已经用了 weixin_authorize 这个 gem 了

@greatbody 对于被动消息,如何回复,如下做法为什么会出错? hash=MultiXml.parse(request.body.read)['xml'] render xml: <<-STRING <![CDATA[#{hash['ToUserName']}]]> <![CDATA[#{hash['FromUserName']}]]> #{Time.now.to_i} <![CDATA[text]]> <![CDATA[helloworld]]> STRING

#9 楼 @u1446799422 我先看看你的代码。

#9 楼 @u1446799422

def reply_text(to_user, from_user, message)
  <<-EOS
  <xml>
  <ToUserName><![CDATA[#{to_user}]]></ToUserName>
  <FromUserName><![CDATA[#{from_user}]]></FromUserName>
  <CreateTime>#{Time.now.to_i}</CreateTime>
  <MsgType><![CDATA[text]]></MsgType>
  <Content><![CDATA[#{message}]]></Content>
  </xml>
  EOS
end

然后将这个函数生成的字符串作为返回结果,返回给微信的 post 请求。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号