微信小程序-客服消息配置(自动回复)

    xiaoxiao2022-07-12  145

    微信小程序客服消息分两种,一种是自动回复 需要我们在后台配置 另一种是人工回复 只要我们在小程序后台配置就可以了 今天主要写的是自动回复在小程序后台配置的坑

    def check_miniprogram_msg_signature(token, signature, timestamp, nonce) arr = [token, timestamp, nonce] arr = arr.sort text = '' arr.each {|element| text += element} (Digest::SHA1.hexdigest(text)==signature) ? true : false # 验证消息 end def recieve_miniprogram_msg render :text => params[:echostr] if check_miniprogram_msg_signature(ENV['NAME_CARD_WECHAT_TOKEN'],params[:signature], params[:timestamp], params[:nonce]) end

    读取消息内容:

    content = Nokogiri::XML(request.body.read) username = content.at_css('FromUserName')&.children&.text

    自动回复: 如果是图片 需要mediaid

    res = RestClient.post 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token=' + access_token + '&type=image', {media: file }, {'Content-Type': 'application/octet-stream'} media_id = JSON.parse(res)['media_id'] temp = { "touser": username, "msgtype":"image", "image": { "media_id": media_id } } msg = RestClient.post 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' + a ccess_token, temp.to_json, {content_type: :json, accept: :json}

    最后在小程序后台 – 开发 -开发设置-消息推送中配置 启用并设置服务器配置后,用户发送的消息以及开发者需要的事件推送,都将被微信转发至开发者URL中。

    最新回复(0)