安卓微信机器人插件开发-2

使用php、asp.net开发安卓微信机器人插件

Web插件已经提供C#源码,如有需要请自行修改编译!

Web接口工作原理

机器人收到消息,把消息内容、发送者Wxid、等数据以HTTP协议POST方法发送给接口地址,php处理后返回数据,机器人处理返回数据,返回数据会调用机器人的各种不同方法
多个方法之间使用<&&>分割,每个方法传递的参数使用<&>分割

例如返回“<&&>SendMessage<&>xxxxxxx<&>您好<&><&>1”表示给xxxxxxx发送消息您好,xxxxxxx是接收者wxid
一次可以返回多条指令,例如:

echo('<&&>SendMessage<&>xxxxxxx<&>您好<&><&>1');  
echo('<&&>SendMessage<&>aaa@chatroom<&>群里说大家好<&>bbb<&>1');bbb表示群里@bbb用户bbb也是用wxid

除此以外,机器人还会定时发送GET请求给接口,以检查接口是否有需要发送的指令,可以用来主动给用户发送消息

如果还不理解,请看:作者手绘流程图 -_-||

注意:

  1. 编码要和php文件编码保持一致,否则可能乱码,推荐使用utf-8
  2. 推荐使用密钥,确保接口安全
  3. 必要时可以查阅机器人日志

调试代码

<?php
if($_POST){
    $ret = print_r($_POST, TRUE);
    echo("<&&>SendMessage<&>你的wxid<&>收到数据:\n$ret<&><&>1");
}
?>

调试效果:

发送“你好”给机器人,机器人回复:
Array
(
    [Robot] => xxxxxxxxxx
    [Key] => 123
    [Event] => ReceiveMessage
    [AtList] => 
    [Sender] => xxxxx
    [SenderNick] => xxxxx
    [Content] => 您好
    ……
)  

我们可以看到,机器人发送了n个参数给Web,分别是:Robot、Key、Event、AtList、Sender、SenderNick、Content等
调试代码调用echo返回结果,结果调用了SendMessage函数,给123456发送收到的数据

更多发送参数,请放上调试代码,运行下机器人、插件,即可在机器人日志窗口中看到发送的参数。
返回的结果可以调用的函数,请参考“开发文档.chm”中的Coco.Framework->QQClient节公共方法

监听端口使用

开启“监听端口”后,机器人可以监听端口,等待指令,例如设置8080,使用管理员身份重启后,打开浏览器,输入:

http://localhost:8080/?key=123&a=<%26%26>SendMessage<%26>123456<%26>test

回车后看到机器人日志窗口出现:

2013/4/25 15:08:28 本地监听接收:<&&>SendMessage<&>123456<&>test

2013/4/25 15:08:28 给【123456】发送消息:test
2013/4/25 15:08:28 本地监听返回:ok

即可以即时执行指令,其中<%26%26>是<&&>的url编码,<%26>是<&>的url编码,这里指令格式同上面的指令是一样的<&&>、<&>分割,注意整个指令都需要进行url编码

Web接口支持的函数:

函数说明格式举例:

函数原型:

    /// <summary>
    /// 转账
    /// </summary>
    /// <param name="to_wxid">接收者wxid</param>
    /// <param name="fee">限额1000分</param>
    /// <param name="desc">转账说明</param>
    /// <param name="password">钱包密码</param>
    /// <returns></returns>
    public string Transfer(string to_wxid, int fee, string desc, string password = "123456")

Web接口调用方法:

<&&>Transfer<&>wxid_xxxxxxxxxxxxx<&>50<&>5毛也是爱<&>123456

web插件收到消息,返回格式如上,后面不再重复,只列出每个函数的名字、参数、功能。

特别说明:调用web插件web服务器,需要url编码传递指令,否则会乱码,get、post均可。

已支持函数原型如下:具体函数签名以软件实际为准

    /// <summary>
    /// 转账
    /// </summary>
    /// <param name="to_wxid">接收者wxid</param>
    /// <param name="fee">限额1000分</param>
    /// <param name="desc">转账说明</param>
    /// <param name="password">钱包密码</param>
    /// <returns></returns>
    public string Transfer(string to_wxid, int fee, string desc, string password = "123456")

    /// <summary>
    /// 提现
    /// </summary>
    /// <param name="total_fee">限额100元(10000)</param>
    /// <param name="card_tail">卡号后4位(可不填)</param>
    /// <param name="password">钱包密码</param>
    /// <returns></returns>
    public string Genprefetch(int total_fee, string card_tail = "", string password = "123456")

    /// <summary>
    /// 发红包
    /// </summary>
    /// <param name="wxid">接收者,可以是群wxid,也可以是好友wxid</param>
    /// <param name="hbType">0:普通红包 1:拼手气红包</param>
    /// <param name="totalAmount">单位分,限额10元</param>
    /// <param name="totalNum">红包个数,群红包必须能分配,不能出现类似1分钱2个红包的情况</param>
    /// <param name="wishing">祝福语</param>
    /// <param name="password">钱包密码</param>
    /// <returns></returns>
    public string SendHB(string wxid, int hbType, int totalAmount, int totalNum = 1, string wishing = "恭喜发财", string password = "123456")

    /// <summary>
    /// 同意入群
    /// </summary>
    /// <param name="url">收到好友验证时附带的</param>
    /// <returns></returns>
    public bool AgreeJoin(string url)

    /// <summary>
    /// 获取我的二维码
    /// </summary>
    /// <returns>二进制二维码图片内容</returns>
    public byte[] GetQRCode()

    /// <summary>
    /// 发送图片
    /// </summary>
    /// <param name="wxid">接收者wxid</param>
    /// <param name="file">本地绝对路径,网络图片请自行下载</param>
    /// <returns></returns>
    public string SendImage(string wxid, string file)

    /// <summary>
    /// 发送消息
    /// </summary>
    /// <param name="wxid">接收者</param>
    /// <param name="msg_content">消息内容,支持XML格式,不要问我有哪些,请自行收集</param>
    /// <param name="at_user_list">如果是群消息,这里可以填写@对象wxid</param>
    /// <param name="msg_type">消息类型,我只测试了1的情况。默认填写1即可</param>
    /// <returns></returns>
    public string SendMessage(string wxid, string msg_content, string at_user_list = null, int msg_type = 1)

    /// <summary>
    /// 下载图片
    /// </summary>
    /// <param name="fileid">收到的cdnthumburl</param>
    /// <param name="aeskey">收到消息带的</param>
    /// <param name="filetype">1:大图cdnbigimgurl 2:中图cdnmidimgurl 3:小图cdnthumburl</param>
    /// <returns>返回图片二进制内容</returns>
    public byte[] DownloadImage(string fileid, string aeskey, int filetype = 2)

    /// <summary>
    /// 下载图片并保存到文件
    /// </summary>
    /// <param name="fileid">收到的cdnthumburl</param>
    /// <param name="aeskey">收到消息带的</param>
    /// <param name="filetype">1:大图cdnbigimgurl 2:中图cdnmidimgurl 3:小图cdnthumburl</param>
    /// <param name="filename">文件保存的本地路径</param>
    /// <returns></returns>
    public bool DownloadImage(string fileid, string aeskey, int filetype, string filename)

    /// <summary>
    /// 发送名片
    /// </summary>
    /// <param name="to_wxid">接收者wxid</param>
    /// <param name="wxid">名片的wxid</param>
    /// <returns></returns>
    public string SendCard(string to_wxid, string wxid)

    /// <summary>
    /// 同意加好友请求
    /// </summary>
    /// <param name="opcode">传3即可</param>
    /// <param name="user_wxid">收到的FromUsername</param>
    /// <param name="user_v1_name">收到的EncryptUsername</param>
    /// <param name="user_ticket">收到的Ticket</param>
    /// <param name="user_anti_ticket">空着即可</param>
    /// <param name="send_content">空着即可</param>
    /// <returns></returns>
    public string VerifyUser(int opcode, string user_wxid, string user_v1_name, string user_ticket, string user_anti_ticket = "", string send_content = "")

    /// <summary>
    /// 分享链接
    /// </summary>
    /// <param name="to_wxid">接收者wxid</param>
    /// <param name="title">标题</param>
    /// <param name="des">描述</param>
    /// <param name="link_url">链接地址http格式</param>
    /// <param name="thumb_url">缩略图地址http格式</param>
    /// <returns></returns>
    public long SendAppMsg(string to_wxid, string title, string des, string link_url, string thumb_url = "")

    /// <summary>
    /// 发送原始appmsg,可以实现点歌、发小程序、发链接等
    /// </summary>
    /// <param name="to_wxid">接收者wxid</param>
    /// <param name="content">自己构造XML,不要问我XML怎么构造,我也没有,自己想办法</param>
    /// <param name="type">没研究过,不知道啥意思,大家自己黑盒测试</param>
    /// <returns></returns>
    public long SendAppMsgRaw(string to_wxid, string content, int type = 5)

    /// <summary>
    /// 收红包
    /// </summary>
    /// <param name="msg">这是一个收到红包时的对象,web调用需要json序列化成字符串传给机器人,机器人反序列化成对象</param>
    /// <returns></returns>
    public HBInfo ReceiveHB(HBMsg msg)

    /// <summary>
    /// 查询红包
    /// </summary>
    /// <param name="nativeUrl">收到红包时有</param>
    /// <param name="limit">数量?</param>
    /// <param name="offset">偏移量?获取红包如果有几十个领取人时需要用,没测试过</param>
    /// <param name="ver">传v1.0</param>
    /// <returns></returns>
    public string QueryHB(string nativeUrl, int limit = 11, int offset = 0, string ver = "v1.0")

    /// <summary>
    /// 获取收款二维码
    /// </summary>
    /// <param name="fee">金额,单位分</param>
    /// <param name="desc">收钱说明</param>
    /// <param name="fee_type">传1</param>
    /// <returns></returns>
    public string GetPayUrl(int fee, string desc, string fee_type = "1")

    /// <summary>
    /// 发送emoji表情
    /// </summary>
    /// <param name="wxid"></param>
    /// <param name="file_name">emoji加密文件名</param>
    /// <param name="game_type">game_type=0直接发送emoji; game_type=1无视file_name参数,接收方播放石头剪刀布动画;其余game_type值均为投骰子动画</param>
    /// <param name="content">在game_type不为0即发送游戏表情时有效;content取1-3代表剪刀、石头、布;content取4-9代表投骰子1-6点;</param>
    /// <returns>消息id,撤回消息时可用用到,其它地方没啥用</returns>
    public long SendEmoji(string wxid, string file_name, string game_type, string content)


    /// <summary>
    /// 收款
    /// </summary>
    /// <param name="msg">收到转账时带的,web调用需要JSON序列化成字符串传递</param>
    /// <returns></returns>
    public bool ReceiveTransfer(TransferMsg msg)

    /// <summary>
    /// 建群
    /// </summary>
    /// <param name="wxids">成员wxid,web调用多个wxid使用,连接</param>
    /// <returns></returns>
    public string CreateChatRoom(IEnumerable<string> wxids)

    /// <summary>
    /// 面对面建群,少用
    /// </summary>
    /// <param name="pwd">密码</param>
    /// <param name="lon">经度</param>
    /// <param name="lat">纬度</param>
    /// <returns></returns>
    public string MMFacingCreateChatRoom(string pwd = "8888", float lon = 116.39f, float lat = 38.90f)

    /// <summary>
    /// 群聊拉人
    /// </summary>
    /// <param name="chatroom_wxid">群wxid</param>
    /// <param name="member_list">成员列表,web调用用,分割</param>
    /// <returns></returns>
    public int AddChatRoomMember(string chatroom_wxid, IEnumerable<string> member_list)

    /// <summary>
    /// 群踢人
    /// </summary>
    /// <param name="chatroom_wxid">群wxid</param>
    /// <param name="member_list">成员列表</param>
    /// <returns></returns>
    public int DelChatRoomMember(string chatroom_wxid, IEnumerable<string> member_list)

    /// <summary>
    /// 设置机器人在群内的昵称
    /// </summary>
    /// <param name="chatroom_wxid">群wxid</param>
    /// <param name="nick_name">昵称</param>
    /// <returns></returns>
    public bool SetGroupNickName(string chatroom_wxid, string nick_name)

    /// <summary>
    /// 撤回消息
    /// </summary>
    /// <param name="wxid">消息接收者</param>
    /// <param name="svrid">发消息返回的消息id</param>
    /// <returns></returns>
    public int RevokeMsg(string wxid, long svrid)

    /// <summary>
    /// 发布群公告
    /// </summary>
    /// <param name="wxid">群wxid</param>
    /// <param name="text">公告内容</param>
    /// <returns></returns>
    public int SetChatroomAnnouncement(string wxid, string text)

    /// <summary>
    /// 设置好友备注名/群聊名
    /// </summary>
    /// <param name="wxid">wxid</param>
    /// <param name="name">备注名或者群名</param>
    /// <returns></returns>
    public int SetFriendName(string wxid, string name)

    /// <summary>
    /// 拉黑/恢复 好友关系
    /// </summary>
    /// <param name="wxid">wxid</param>
    /// <param name="ban">拉黑还是恢复,自己测试下</param>
    /// <returns></returns>
    public int BanFriend(string wxid, bool ban = true)

    /// <summary>
    /// 从通讯录中删除好友/恢复好友(删除对方后可以用此接口再添加对方)
    /// 群聊使用此接口可以保存到通讯录
    /// </summary>
    /// <param name="wxid">wxid</param>
    /// <param name="delete"></param>
    /// <returns></returns>
    public int DeleteFriend(string wxid, bool delete = true)

    /// <summary>
    /// 设置昵称、个性签名
    /// </summary>
    /// <param name="cmd">1:昵称 2:个性签名</param>
    /// <param name="text"></param>
    /// <returns></returns>
    public int SetUserInfo(int cmd, string text)

    /// <summary>
    /// 其它设备扫码登录
    /// </summary>
    /// <param name="url">二维码内容</param>
    /// <returns></returns>
    public string DeviceLogin(string url)

    /// <summary>
    /// 确认其它设备登录
    /// </summary>
    /// <param name="url">机器人收到的内容</param>
    /// <returns></returns>
    public string ExtDeviceLoginConfirmOK(string url)

    /// <summary>
    /// 从服务器获取单个好友或者群
    /// </summary>
    /// <param name="wxid">如果传空字符串,从本地获取所有成员,否则从服务器获取单个成员</param>
    /// <returns></returns>
    public Contact GetContact(string wxid)

    /// <summary>
    /// 修改群聊邀请确认
    /// </summary>
    /// <param name="wxid">群wxid</param>
    /// <param name="code">0:关闭确认 2:开启确认</param>
    /// <returns></returns>
    public int ChatroomInviteConfirm(string wxid, int code)

    /// <summary>
    /// 转让群主
    /// </summary>
    /// <param name="ChatRoomWxid">群wxid</param>
    /// <param name="OwnerWxid">新群主wxid</param>
    /// <returns></returns>
    public int TransferChatroomOwner(string ChatRoomWxid, string OwnerWxid)

    /// <summary>
    /// 确认群成员邀请
    /// </summary>
    /// <param name="ChatRoomWxid">群wxid</param>
    /// <param name="ticket">收到的令牌</param>
    /// <param name="Inviter">邀请者wxid</param>
    /// <param name="Wxid">被邀请者</param>
    /// <returns></returns>
    public int ApproveAddChatroomMember(string ChatRoomWxid, string ticket, string Inviter, IEnumerable<string> Wxid)

    /// <summary>
    /// 从服务器获取群成员建议不要频繁使用
    /// </summary>
    /// <param name="ChatRoomWxid">群wxid</param>
    public Contact GetChatroomMemberDetail(string ChatRoomWxid)

机器人事件、参数说明:

JoinChatroom:机器人收到进群邀请(邀请机器人进群)

    /// <summary>
    /// 发送者
    /// </summary>
    public string from_id { get; set; }
    /// <summary>
    /// 接收者
    /// </summary>
    public string to_id { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string content { get; set; }
    /// <summary>
    /// 发送者昵称
    /// </summary>
    public string NickName { get; set; }
    /// <summary>
    /// 群名字
    /// </summary>
    public string ChatroomName { get; set; }
    /// <summary>
    /// 通过是需要用的
    /// </summary>
    public string Url { get; set; }
    /// <summary>
    /// 群头像
    /// </summary>
    public string ThumbUrl { get; set; }

PushLoginUrl:其它设备需要登录

    /// <summary>
    /// 确认时需要用的
    /// </summary>
    public string url { get; set; }
    /// <summary>
    /// 设备名
    /// </summary>
    public string Device { get; set; }

ReceiveSysMsg:系统消息,包括谁改了群名、谁加入群了、谁退出群了等

    /// <summary>
    /// 消息类型
    /// </summary>
    public int Type { get; set; }
    /// <summary>
    /// 来源wxid
    /// </summary>
    public string from_id { get; set; }
    /// <summary>
    /// 接收者wxid
    /// </summary>
    public string to_id { get; set; }
    /// <summary>
    /// 消息内容
    /// </summary>
    public string content { get; set; }

ReceiveVerifyMsg:加机器人为好友,需要机器人确认通过

    public long Id { get; set; }
    public string Ticket { get; set; }
    public string FromUsername { get; set; }
    public string EncryptUsername { get; set; }
    public string FromNickname { get; set; }
    public string Content { get; set; }
    public string Sign { get; set; }
    public string Scene { get; set; }
    public string Alias { get; set; }
    public string Bigheadimgurl { get; set; }
    public string Smallheadimgurl { get; set; }
    public string Opcode { get; set; }
    /// <summary>
    /// 来源,可以做邀请统计、营销用
    /// </summary>
    public string SourceUserName { get; set; }
    public string SourceNickName { get; set; }
    /// <summary>
    /// 原始内容,可以自行解析
    /// </summary>
    public string RawContent { get; set; }

    public DateTime AddTime { get; set; }
    public bool Passed { get; set; }

ReceiveTransferMsg:收到转账,需要自己调用方法确认收款

    public long Id { get; set; }
    public string Transferid { get; set; }
    public string Transcationid { get; set; }
    /// <summary>
    /// 收到转账时1,确认后变为3,只有这里为3才表示已经收款
    /// </summary>
    public int Paysubtype { get; set; }
    public DateTime Invalidtime { get; set; }
    /// <summary>
    /// 发送者
    /// </summary>
    public string FromId { get; set; }
    /// <summary>
    /// 接收者,务必判断是否与机器人wxid相同,因为机器人给别人转账时也会发生这个事件
    /// </summary>
    public string ToId { get; set; }
    public int Amount { get; set; }
    public string PayerMemo { get; set; }

ReceiveQRPayMsg:收到二维码付款

    public long Id { get; set; }
    public string Outtradeno { get; set; }
    public string UserName { get; set; }
    public string Displayname { get; set; }
    public DateTime TimeStamp { get; set; }
    public int Amount { get; set; }
    public string Memo { get; set; }
    public string PayerMemo { get; set; }

ReceiveHBMsg:收到红包消息,需要自己调用收红包才能收到

    public long Id { get; set; }
    public string Paymsgid { get; set; }
    public string Title { get; set; }
    public string Nativeurl { get; set; }
    public DateTime Invalidtime { get; set; }
    public string UserName { get; set; }
    public string FromId { get; set; }
    /// <summary>
    /// 抢到的金额
    /// </summary>
    public int Amount { get; set; }
    /// <summary>
    /// 抢过
    /// </summary>
    public bool Finished { get; set; }

ReceiveMessage:收到消息,最复杂的一个事件,微信很多事情都用这个来通知,框架解析了一些,如果有需要解析的,可以再反馈。

    /// <summary>
    /// 发言人(群里和私聊通用,框架定义的)
    /// </summary>
    public string Sender { get; set; }
    /// <summary>
    /// 发言人昵称(群里和私聊通用,框架定义的)
    /// </summary>
    public string SenderNick { get; set; }
    /// <summary>
    /// 群聊时表示群名,否则空
    /// </summary>
    public string FromNick { get; internal set; }
    /// <summary>
    /// at列表,群聊时用
    /// </summary>
    public List<string> AtList { get; set; }
    public DateTime CreateTime { get; set; }
    /// <summary>
    /// 群消息和私聊消息都是同一个事件,因此FromId如果是@chatroom结尾就是群消息,否则为私聊。其他公众号等特殊id请自己过滤
    /// </summary>
    public string FromId { get; set; }
    public int msgKey { get; set; }
    /// <summary>
    /// 消息内容,可能包含XML消息,公众号消息等,请自行处理
    /// </summary>
    public string Content { get; set; }
    public string ExInfo { get; set; }
    public long ServerId { get; set; }
    public int Status { get; set; }
    public long SvrId { get; set; }
    /// <summary>
    /// 接收者
    /// </summary>
    public string ToId { get; set; }

    public int Type { get; set; }

    public string XmlContent { get; set; }

results matching ""

    No results matching ""