• 【perl】企业微信发消息


    https://open.work.weixin.qq.com/api/doc#90000/90135/90236

    #!/usr/bin/env perl
    
    use strict;
    use warnings FATAL => 'all';
    use Encode qw(encode_utf8);
    use JSON::MaybeXS qw(encode_json decode_json);
    use LWP::UserAgent;
    use HTTP::Request ();
    use HTTP::Response;
    use HTTP::Request::Common;
    
    my $tokenurl    = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?';
    my $sendurl     = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?';
    my $corpid      = '';
    my $corpsecret  = '';
    my $agentid     =;
    
    sub get_response($) {
      my $req = shift;
    
      my $ua  = LWP::UserAgent->new(timeout=>10);
      my $res = $ua->request($req);
      # $res->decoded_content / $res->content
      if ( $res->is_success ) {
        my $res_data = decode_json($res->content);
        return (1, $res_data);
      }
      else {
        return (0, $res->status_line);
      }
    }
    
    sub get_token {
      my $token_url = $tokenurl . 'corpid=' .$corpid . '&corpsecret=' . $corpsecret;
      my $req = HTTP::Request->new('GET', $token_url);
    
      my ($r, $res_data) = get_response($req);
      if ( $r ) {
        return ($r, $res_data->{access_token});
      }
      else {
        return ($r, $res_data);
      }
    
    }
    
    sub send_data($$$) {
      my ($token, $user, $content) = @_;
      my $send_url = $sendurl . 'access_token=' . $token;
      my $send_data = {
        'touser'  => $user,
        'msgtype' => 'text',
        'agentid' => $agentid,
        'text'    => {'content' => $content},
        'safe'    => '0'
      };
      my $json = encode_utf8(encode_json($send_data));
      my $req = HTTP::Request->new('POST', $send_url);
      $req->content($json);
    
      my ($r, $res_data) = get_response($req);
      if ( $r ) {
        return ($r, $res_data->{errmsg});
      }
      else {
        return ($r, $res_data);
      }
    
    }
    
    my ($r, $send_token) = get_token();
    print send_data($send_token, 'username', 'hello world');
  • 相关阅读:
    vbscript 语言通过序列和ADODB实现取号不重复
    arcgisserver成功发布服务后,浏览服务,无地图显示
    GUID的获取
    EasyUi 表格自适应宽度
    接口隔离原则
    依赖倒置原则
    开放封闭原则
    单一职责原则
    python-函数基础
    python -流程控制
  • 原文地址:https://www.cnblogs.com/jiangxu67/p/10768300.html
Copyright © 2020-2023  润新知