• 【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');
  • 相关阅读:
    P1726 上白泽慧音
    P1993 小k的农场
    P1983 车站分级
    P1525 关押罪犯【二分+二分图】
    P1268 树的重量【构造】
    P1113 杂务
    F.Three pahs on a tree
    P1522 牛的旅行
    两个约束下的dp问题
    dp 最大正方形
  • 原文地址:https://www.cnblogs.com/jiangxu67/p/10768300.html
Copyright © 2020-2023  润新知