โซลูชันสำหรับการส่งข้อความไปยัง WeChat ผ่าน Enterprise WeChat รวม:
ข้อได้เปรียบ:
PS: อินเทอร์เฟซการส่งข้อความสามารถใช้งานได้โดยไม่ต้องมีการตรวจสอบสิทธิ์ บุคคลสามารถลงทะเบียนโดยใช้ WeChat
ใช้คอมพิวเตอร์ของคุณเพื่อเปิดเว็บไซต์ WeChat อย่างเป็นทางการของบริษัทและจดทะเบียนบริษัท
หลังจากการลงทะเบียนสำเร็จ คลิก "จัดการองค์กร" เพื่อเข้าสู่อินเทอร์เฟซการจัดการ เลือก "การจัดการแอปพลิเคชัน" → "สร้างด้วยตนเอง" → "สร้างแอปพลิเคชัน"
กรอกชื่อแอปพลิเคชัน "Server Sauce" ดาวน์โหลดโลโก้แอปพลิเคชันที่นี่ และเลือกชื่อบริษัทสำหรับช่วงที่มองเห็นได้
หลังจากการสร้างเสร็จสมบูรณ์ ให้เข้าสู่หน้ารายละเอียดแอปพลิเคชัน จากนั้นคุณจะได้รับ ID แอปพลิเคชัน ( agentid
)1 และความลับของแอปพลิเคชัน ( secret
)②
หมายเหตุ: เมื่อ secret
ถูกส่งไปยังโทรศัพท์มือถือ จะสามารถดูได้ใน企业微信客户端
เท่านั้น
แอปพลิเคชันที่สร้างหลังวันที่ 20 มิถุนายน 2022 จะต้องกำหนดค่า IP ที่เชื่อถือได้เพิ่มเติม
ที่ด้านล่างของ "หน้ารายละเอียดแอปพลิเคชัน" ในหมวดหมู่อินเทอร์เฟซสำหรับนักพัฒนา ให้ค้นหา "Enterprise Trusted IP" คลิก "กำหนดค่า" และกรอก IP ของเซิร์ฟเวอร์
โปรดทราบว่าหากคุณใช้บริการคลาวด์ IP สาธารณะ เช่น ฟังก์ชันคลาวด์ คุณอาจต้องเปิด "IP สาธารณะแบบคงที่" ในอินเทอร์เฟซการตั้งค่า (ของฟังก์ชันคลาวด์หรือบริการอื่นๆ) เพื่อรับ IP อิสระ มิฉะนั้น อาจมีการรายงานข้อผิดพลาด "Third Party Service IP"
เข้าสู่หน้า "ธุรกิจของฉัน" เลื่อนลงไปด้านล่าง คุณจะเห็นรหัสธุรกิจ 3 คัดลอกและกรอกไปด้านบน
หากต้องการพุช UID ให้กรอก @all
โดยตรงและพุชไปยังพนักงานทุกคนในบริษัท
ป้อน "ธุรกิจของฉัน" → "ปลั๊กอิน WeChat" เลื่อนลงเพื่อสแกนโค้ด QR และปฏิบัติตามเพื่อรับข้อความพุช
PS: หาก接口请求正常,企业微信接受消息正常,个人微信无法收到消息
:
ไปที่ "ธุรกิจของฉัน" → "ปลั๊กอิน WeChat" เลื่อนลงไปด้านล่างและทำเครื่องหมายที่ "อนุญาตให้สมาชิกรับและตอบกลับข้อความแชทในปลั๊กอิน WeChat"
ปิดข้อจำกัด "ยอมรับเฉพาะข้อความใน Business WeChat" ในไคลเอนต์ Business WeChat "ฉัน" → "การตั้งค่า" → "การแจ้งเตือนข้อความใหม่"
PS: เพื่อความสะดวกในการใช้งาน ฟังก์ชั่นต่อไปนี้จะไม่แคช access_token
ก็เพียงพอแล้วสำหรับการโทรความถี่ต่ำส่วนบุคคล สำหรับการใช้งานที่มีการแคช โปรดดูโค้ดตัวอย่างใน index.php
(ขึ้นอยู่กับการใช้งาน Redis)
เวอร์ชัน 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 ;
}
ตัวอย่างการใช้งาน:
$ ret = send_to_wecom ( "推送测试rn测试换行" , "企业ID③ " , "应用ID① " , "应用secret② " );
print_r ( $ ret );
เวอร์ชันหลาม:
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
ตัวอย่างการใช้งาน:
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 );
เวอร์ชันของสคริปต์:
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
}
ตัวอย่างการใช้งาน:
sendToWecom ( {
text : '推送测试rn测试换行' ,
wecomAgentId : '应用ID①' ,
wecomSecret : '应用secret②' ,
wecomCId : '企业ID③' ,
} )
. then ( ( res ) => {
console . log ( res )
} )
. catch ( ( err ) => {
console . log ( err )
} )
เวอร์ชัน .NET Core:
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" ;
}
}
ตัวอย่างการใช้งาน:
static void Main ( string [ ] args )
{ // 测试
Console . Write ( new WeCom ( ) . SendToWeCom (
"msginfo" ,
"企业Id①"
, "应用secret②" ,
"应用ID③"
) ) ;
}
}
ฟังก์ชันเวอร์ชันอื่นๆ สามารถเขียนได้โดยอ้างอิงถึงตรรกะด้านบน
สำหรับการใช้งานขั้นสูงในการส่งรูปภาพ การ์ด ไฟล์ หรือข้อความ Markdown โปรดดู Enterprise WeChat API