前言

公司项目管理中用到了软件redmine,然鹅redmine的任务消息并不会实时推送给指定人员(支持邮件推送,个人并不是很喜欢邮件感觉比较正式,通过实时的消息会更讨喜一些)。然后萌生了推送到实时聊天工具的想法,钉钉是现阶段比较流行的工具公司内聊天通讯工具(当然也有其他的QQ,微信,Skype等)。找到了一个方式:redmine可以通过webhook的方式向外发送请求,于是乎就能自己搭建一个小服务推送到钉钉了。


redmine的webhook插件安装


redmine推送消息解析

配置完webhook后,创建或修改redmine上的任务会出现以下类似消息

{
  "payload": {
    "action": "opened",
    "issue": {
      "id": 1,
      "subject": "A sample bug",//任务标题
      "description": "Lorem ipsum dolor sic amet.",//描述
      "created_on": "2015-03-06T04:23:42Z",//创建时间
      "updated_on": "2015-03-07T10:00:59Z",//更新时间
      "closed_on": null,
      "root_id": 1,
      "parent_id": null,
      "done_ratio": 0,
      "start_date": "2015-03-02",//计划开始日期
      "due_date": "2015-03-20",//计划完成日期
      "estimated_hours": 15,
      "is_private": false,
      "lock_version": 14,
      "project": {
        "id": 1,
        "identifier": "playground",
        "name": "Playground",//项目名称
        "description": "A sample playground project",//项目描述
        "created_on": "2015-03-06T02:51:48Z",//项目创建时间
        "homepage": ""
      },
      "status": {
        "id": 1,
        "name": "New"//任务状态
      },
      "tracker": {
        "id": 2,
        "name": "Feature"//任务类型
      },
      "priority": {
        "id": 3,
        "name": "High"//优先级
      },
      "author": {//作者、派发者
        "id": 1,
        "login": "admin",
        "mail": "[email protected]",
        "firstname": "Redmine",
        "lastname": "Admin",
        "identity_url": null,
        "icon_url": "http:\/\/www.gravatar.com\/avatar\/cb4f282fed12016bd18a879c1f27ff97?rating=PG&size=50"
      },
      "assignee": {//接任务人
        "id": 5,
        "login": "demo",
        "mail": "[email protected]",
        "firstname": "Demo",
        "lastname": "User",
        "identity_url": null,
        "icon_url": "http:\/\/www.gravatar.com\/avatar\/0e5601057dfe4b0fa94611f1fad4fb95?rating=PG&size=50"
      },
      "watchers": [//跟踪人
        {
          "id": 1,
          "login": "admin",
          "mail": "[email protected]",
          "firstname": "Redmine",
          "lastname": "Admin",
          "identity_url": null,
          "icon_url": "http:\/\/www.gravatar.com\/avatar\/cb4f282fed12016bd18a879c1f27ff97?rating=PG&size=50"
        }
      ]
    },
    "url": "http:\/\/localhost:3000\/issues\/1"//redmine打开网址,这个需要看情况替换成可访问地址
  }
}

钉钉中创建机器人


java写简单接收转发

配置项:
redmine.template=\n > # 任务<b> :{0}</b>\n > ### 项目 :{1}\n > ### 描述 :{2}\n > ### 状态:{3}\n > ### 类型:{4}\n > ### 进度:{5} \n > ### 备注:{6}\n > ### 创建人:{7}\n > ### 优先级:{8}\n > ### 指派给:{9}\n > ### 跟踪人: {10}\n > ### 编辑人:{11}\n > ### 更新时间:{12}\n > [☛详情戳这里☚]({13})
实际目标格式化
String content = MessageFormat.format(template, subject, project, desc, status, tracker, percent, note, author, priority, assignee, watchers, editor, updateTime, url);

更多详见git仓库。


示例

  • 请求参数(将配置参数中的钉钉webhook替换即可测试,项目名称替换成“test项目”,请求形式get/post Content-Type: application/json)

    {"payload":{"journal":{"notes":"","private_notes":false,"created_on":"2019-01-07T05:39:15.653Z","author":{"icon_url":"//www.gravatar.com/avatar/8a2bbeda9b56a3005a1a471a21a32dd6?rating=PG&size=50","firstname":"四","mail":"","id":8,"login":"lisi","lastname":"李"},"details":[{"prop_key":"status_id","property":"attr","old_value":"1","id":608,"value":"2"}],"id":477},"issue":{"updated_on":"2019-01-07T05:39:15.000Z","is_private":false,"lock_version":1,"subject":"测试 发现一个bug","author":{"icon_url":"//www.gravatar.com/avatar/ea548552b5363569ee9a1efd5c46789c?rating=PG&size=50","firstname":"三","mail":"","id":27,"login":"zhangsan","lastname":"张"},"description":"测试描述 xxxx情况下 空指针","project":{"identifier":"test","created_on":"2018-08-31T10:46:42.000Z","name":"test项目","description":"test项目","id":5,"homepage":""},"watchers":[{"icon_url":"//www.gravatar.com/avatar/d9af92dc2dc21e8b6bd7853027b398e3?rating=PG&size=50","firstname":"五","mail":"","id":13,"login":"wangwu","lastname":"王"}],"priority":{"name":"普通","id":2},"done_ratio":0,"created_on":"2019-01-07T03:45:30.000Z","tracker":{"name":"缺陷","id":4},"root_id":234,"id":234,"assignee":{"icon_url":"//www.gravatar.com/avatar/8a2bbeda9b56a3005a1a471a21a32dd6?rating=PG&size=50","firstname":"四","mail":"","id":8,"login":"lisi","lastname":"李"},"start_date":"2019-01-07","status":{"name":"已解决","id":2}},"action":"updated","url":"http://localhost:3000/issues/234"}}
    
  • 效果redmine推送消息至钉钉-LMLPHP

09-12 06:40