Sinatra Sinatra 微信机器人演示

metal · 2013年03月16日 · 最后由 lxzhh 回复于 2013年12月25日 · 5837 次阅读

刚用 sinatra 写了简单的微信机器人。很简单。

https://github.com/kennx/weixin_robot

boolean 方法前面加 "is_" 很奇怪

#1 楼 @qhwa 我也不知道加什么,英文基础不行,还有一般命名有什么可以指导一下的文章吗?

#2 楼 @metal 什么都不加就最好了,问号就有is_的意思 text?, image?

#3 楼 @qhwa 搞定。谢谢。

简单介绍下功能?

#5 楼 @loveky 现在是个 gem https://rubygems.org/gems/sinatra-weixin-robot

https://github.com/kennx/weixin_robot/blob/master/lib/sinatra/weixin-robot.rb

简单的演示: https://github.com/kennx/yaoyaohuahuapangpang

纯新手作业,有问题多提,有砖就砸,不用客气。自己一个人学挺苦逼的,也没人告诉我那里做的不对。哪里又做的对。


require 'sinatra/base'
require 'sinatra/weixin-robot'

class App < Sinatra::Base
   register Sinatra::WeiXinRobot

   configure do
      enable  :logging
      set     :weixin_token, "yourtoken" # 微信的token
      set     :weixin_uri,   "http://yaoyaohuahuapangpang.cloudfoundry.com/" # 你机器人的地址 https://github.com/kennx/weixin_robot/blob/master/lib/sinatra/weixin-robot.rb#L182
    end
   get "#{settings.weixin_path}" do # 用URI这个东西获取到weixin_uri的path
      "#{params[:echostr}" # 用来通过微信第一次的验证
   end
   post "#{settings.weixin_path}" do
      if generate_signature == params[:signature]
         receiver = message_receiver(request.body)  # 得到用户向机器人发送的信息.  receiver.content 可以得到具体内容。
          xml = receiver.sender do |r|
             r.msg_type = "text"  #指定发送的信息类型.
             r.content = "你好。我是机器人。"
             r.complete! # 最后返回Reply这个对象,用来转换xml
          end
          xml.to_xml # 创建返回xml,结束。
      end
   end
end

还有一些其他的方法,假设给用户发送新闻


post "#{settings.weixin_path}" do
  if generate_signature == params[:signature]
    receiver = message_receiver(request.body)
    receiver.sender(:msg_type => "news") do |r|
       @model.each do |model|
           r.articles = {
                            :title => model.title, 
                            :description => model.description
                            : pic_url => model.pic_url,
                            :url => "http://www.xxx.com/news/#{model.id}"
             }
          r.complete!
          r.to_xml
       end
    end  
  end
end

还有音乐这些。。具体看微信的开发文档和我的源代码 https://github.com/kennx/weixin_robot/blob/master/lib/sinatra/weixin-robot.rb#L136

我写完以后挺不负责的。也没搞什么文档。

#6 楼 @metal 你的演示 repo 里为啥还有 .DS_Store 这货。。。这个跟 ruby 没关系吧。。。

我打开http://yaoyaohuahuapangpang.cloudfoundry.com/啥也没看见啊

#8 楼 @loveky .DS_Store 是 mac 自动产生的。网址是没有任何东西的,需要 post 请求,具体你看微信的开发文档就明白了,http://mp.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E6%8C%87%E5%8D%97。实际这个机器人我也没有写什么。之前写个一些判断,比如你和它说你好,它回复你一些相关的话题。

#9 楼 @metal 明白你的意思了,基于微信的 api 应该可以做一些比较 cool 的东西,回头也折腾下

写得好复杂!!!

来点简单的

require 'sinatra'
module Weixin
  class App < Sinatra::Base
    before do
      timestamp, nonce = params[:timestamp].to_s, params[:nonce].to_s
      codes = ["bluelion", timestamp, nonce].sort.join("")
      halt(401, 'Go away!') unless Digest::SHA1.hexdigest(codes) == params[:signature]
    end

    get "/robot" do
      params[:echostr]
    end

    post "/robot" do
      // 在这里添加你的逻辑
    end

  end
end

```ruby

@metal 请问微信公众频道可以直接给用户推数据吗。我在 github 上找到的例子都是需要以回复方式给用户发的。但是招行的可以直接推给用户的。是有隐藏的办法吗?

顶起 自己一个人学习的确很苦逼的

学习了。@alvin2ye,招行的是服务号,有限制也有优势的,一般的是订阅号,每天好像只能群发一次。

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