Solusi untuk mengirim pesan ke WeChat melalui Enterprise WeChat. termasuk:
keuntungan:
PS: Antarmuka perpesanan dapat digunakan tanpa otentikasi.
Gunakan komputer Anda untuk membuka situs web resmi WeChat perusahaan dan mendaftarkan perusahaan
Setelah registrasi berhasil, klik "Kelola Perusahaan" untuk masuk ke antarmuka manajemen, pilih "Manajemen Aplikasi" → "Bangun Mandiri" → "Buat Aplikasi"
Isikan "Server Sauce" untuk nama aplikasi, unduh logo aplikasi di sini, dan pilih nama perusahaan untuk rentang yang terlihat.
Setelah pembuatan selesai, masuk ke halaman detail aplikasi dan Anda bisa mendapatkan ID aplikasi ( agentid
)① dan Rahasia aplikasi ( secret
)②.
Catatan: Saat secret
dimasukkan ke ponsel, rahasia tersebut hanya dapat dilihat di企业微信客户端
.
Aplikasi yang dibuat setelah 20 Juni 2022 perlu mengonfigurasi IP tepercaya tambahan.
Di bagian bawah "Halaman Detail Aplikasi", di kategori Antarmuka Pengembang, temukan "IP Tepercaya Perusahaan", klik "Konfigurasi" dan isi IP server.
Perhatikan bahwa jika Anda menggunakan layanan cloud IP publik seperti Cloud Function, Anda mungkin perlu mengaktifkan "IP Publik Tetap" di antarmuka pengaturan (Cloud Function atau layanan lainnya) untuk mendapatkan IP independen. Jika tidak, kesalahan "IP Layanan Pihak Ketiga" mungkin dilaporkan.
Masuk ke halaman "Bisnisku", gulir ke bawah, Anda dapat melihat ID bisnis ③, salin dan isi ke atas.
Untuk mendorong UID, isi @all
langsung dan kirimkan ke seluruh karyawan di perusahaan.
Masuk ke "Bisnisku" → "Plug-in WeChat", gulir ke bawah dan pindai kode QR, lalu ikuti untuk menerima pesan push.
PS: Jika接口请求正常,企业微信接受消息正常,个人微信无法收到消息
:
Buka "Bisnisku" → "Plug-in WeChat", gulir ke bawah, dan centang "Izinkan anggota menerima dan membalas pesan obrolan di plugin WeChat"
Nonaktifkan batasan "Hanya terima pesan di WeChat Bisnis" di klien WeChat Bisnis "Saya" → "Pengaturan" → "Pemberitahuan Pesan Baru"
PS: Untuk kemudahan penggunaan, fungsi berikut tidak melakukan cache access_token
. Cukup untuk panggilan pribadi frekuensi rendah. Untuk implementasi dengan caching, lihat contoh kode di index.php
(tergantung implementasi Redis).
Versi PHP:
function send_to_wecom ( $ text , $ wecom_cid , $ wecom_aid , $ wecom_secret , $ wecom_touid = ' @all ' )
{
$ info = @ json_decode ( file_get_contents ( " https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid= " . urlencode ( $ wecom_cid ). " &corpsecret= " . urlencode ( $ wecom_secret )), true );
if ( $ info && isset ( $ info [ ' access_token ' ]) && strlen ( $ info [ ' access_token ' ]) > 0 ) {
$ access_token = $ info [ ' access_token ' ];
$ url = ' https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token= ' . urlencode ( $ access_token );
$ data = new stdClass ();
$ data -> touser = $ wecom_touid ;
$ data -> agentid = $ wecom_aid ;
$ data -> msgtype = " text " ;
$ data -> text = [ " content " => $ text ];
$ data -> duplicate_check_interval = 600 ;
$ data_json = json_encode ( $ data );
$ ch = curl_init ();
curl_setopt ( $ ch , CURLOPT_HTTPHEADER , [ ' Content-Type: application/json ' ]);
curl_setopt ( $ ch , CURLOPT_URL , $ url );
curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true );
@ curl_setopt ( $ ch , CURLOPT_FOLLOWLOCATION , true );
curl_setopt ( $ ch , CURLOPT_POST , true );
curl_setopt ( $ ch , CURLOPT_TIMEOUT , 5 );
curl_setopt ( $ ch , CURLOPT_POSTFIELDS , $ data_json );
curl_setopt ( $ ch , CURLOPT_SSL_VERIFYHOST , false );
curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , false );
$ response = curl_exec ( $ ch );
return $ response ;
}
return false ;
}
Contoh penggunaan:
$ ret = send_to_wecom ( "推送测试rn测试换行" , "企业ID③ " , "应用ID① " , "应用secret② " );
print_r ( $ ret );
Versi PYTHON:
import json , requests , base64
def send_to_wecom ( text , wecom_cid , wecom_aid , wecom_secret , wecom_touid = '@all' ):
get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid= { wecom_cid } &corpsecret= { wecom_secret } "
response = requests . get ( get_token_url ). content
access_token = json . loads ( response ). get ( 'access_token' )
if access_token and len ( access_token ) > 0 :
send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token= { access_token } '
data = {
"touser" : wecom_touid ,
"agentid" : wecom_aid ,
"msgtype" : "text" ,
"text" :{
"content" : text
},
"duplicate_check_interval" : 600
}
response = requests . post ( send_msg_url , data = json . dumps ( data )). content
return response
else :
return False
def send_to_wecom_image ( base64_content , wecom_cid , wecom_aid , wecom_secret , wecom_touid = '@all' ):
get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid= { wecom_cid } &corpsecret= { wecom_secret } "
response = requests . get ( get_token_url ). content
access_token = json . loads ( response ). get ( 'access_token' )
if access_token and len ( access_token ) > 0 :
upload_url = f'https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token= { access_token } &type=image'
upload_response = requests . post ( upload_url , files = {
"picture" : base64 . b64decode ( base64_content )
}). json ()
if "media_id" in upload_response :
media_id = upload_response [ 'media_id' ]
else :
return False
send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token= { access_token } '
data = {
"touser" : wecom_touid ,
"agentid" : wecom_aid ,
"msgtype" : "image" ,
"image" :{
"media_id" : media_id
},
"duplicate_check_interval" : 600
}
response = requests . post ( send_msg_url , data = json . dumps ( data )). content
return response
else :
return False
def send_to_wecom_markdown ( text , wecom_cid , wecom_aid , wecom_secret , wecom_touid = '@all' ):
get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid= { wecom_cid } &corpsecret= { wecom_secret } "
response = requests . get ( get_token_url ). content
access_token = json . loads ( response ). get ( 'access_token' )
if access_token and len ( access_token ) > 0 :
send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token= { access_token } '
data = {
"touser" : wecom_touid ,
"agentid" : wecom_aid ,
"msgtype" : "markdown" ,
"markdown" :{
"content" : text
},
"duplicate_check_interval" : 600
}
response = requests . post ( send_msg_url , data = json . dumps ( data )). content
return response
else :
return False
Contoh penggunaan:
ret = send_to_wecom ( "推送测试r n测试换行" , "企业ID③" , "应用ID①" , "应用secret②" );
print ( ret );
ret = send_to_wecom ( '<a href="https://www.github.com/">文本中支持超链接</a>' , "企业ID③" , "应用ID①" , "应用secret②" );
print ( ret );
ret = send_to_wecom_image ( "此处填写图片Base64" , "企业ID③" , "应用ID①" , "应用secret②" );
print ( ret );
ret = send_to_wecom_markdown ( "**Markdown 内容**" , "企业ID③" , "应用ID①" , "应用secret②" );
print ( ret );
Versi skrip:
import request from 'superagent'
async function sendToWecom ( body : {
text : string
wecomCId : string
wecomSecret : string
wecomAgentId : string
wecomTouid ?: string
} ) : Promise < { errcode : number ; errmsg : string ; invaliduser : string } > {
body . wecomTouid = body . wecomTouid ?? '@all'
const getTokenUrl = `https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid= ${ body . wecomCId } &corpsecret= ${ body . wecomSecret } `
const getTokenRes = await request . get ( getTokenUrl )
const accessToken = getTokenRes . body . access_token
if ( accessToken ?. length <= 0 ) {
throw new Error ( '获取 accessToken 失败' )
}
const sendMsgUrl = `https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token= ${ accessToken } `
const sendMsgRes = await request . post ( sendMsgUrl ) . send ( {
touser : body . wecomTouid ,
agentid : body . wecomAgentId ,
msgtype : 'text' ,
text : {
content : body . text ,
} ,
duplicate_check_interval : 600 ,
} )
return sendMsgRes . body
}
Contoh penggunaan:
sendToWecom ( {
text : '推送测试rn测试换行' ,
wecomAgentId : '应用ID①' ,
wecomSecret : '应用secret②' ,
wecomCId : '企业ID③' ,
} )
. then ( ( res ) => {
console . log ( res )
} )
. catch ( ( err ) => {
console . log ( err )
} )
Versi .NET Inti:
using System ;
using RestSharp ;
using Newtonsoft . Json ;
namespace WeCom . Demo
{
class WeCom
{
public string SendToWeCom (
string text , // 推送消息
string weComCId , // 企业Id①
string weComSecret , // 应用secret②
string weComAId , // 应用ID③
string weComTouId = "@all" )
{
// 获取Token
string getTokenUrl = $ "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid= { weComCId } &corpsecret= { weComSecret } " ;
string token = JsonConvert
. DeserializeObject < dynamic > ( new RestClient ( getTokenUrl )
. Get ( new RestRequest ( ) ) . Content ) . access_token ;
System . Console . WriteLine ( token ) ;
if ( ! String . IsNullOrWhiteSpace ( token ) )
{
var request = new RestRequest ( ) ;
var client = new RestClient ( $ "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token= { token } " ) ;
var data = new
{
touser = weComTouId ,
agentid = weComAId ,
msgtype = "text" ,
text = new
{
content = text
} ,
duplicate_check_interval = 600
} ;
string serJson = JsonConvert . SerializeObject ( data ) ;
System . Console . WriteLine ( serJson ) ;
request . Method = Method . POST ;
request . AddHeader ( "Accept" , "application/json" ) ;
request . Parameters . Clear ( ) ;
request . AddParameter ( "application/json" , serJson , ParameterType . RequestBody ) ;
return client . Execute ( request ) . Content ;
}
return "-1" ;
}
}
Contoh penggunaan:
static void Main ( string [ ] args )
{ // 测试
Console . Write ( new WeCom ( ) . SendToWeCom (
"msginfo" ,
"企业Id①"
, "应用secret②" ,
"应用ID③"
) ) ;
}
}
Versi fungsi lainnya dapat ditulis dengan mengacu pada logika PR di atas.
Untuk penggunaan lanjutan dalam mengirim gambar, kartu, file, atau pesan penurunan harga, lihat API WeChat Perusahaan.