C# development of WeChat portals and applications - messaging for WeChat enterprise accounts (text, pictures, files, voice, video, graphic messages, etc.)

高洛峰
Release: 2017-02-18 09:36:28
Original
2064 people have browsed it

We know that the enterprise account is mainly designed to meet the needs of enterprises, so the exchange of internal messages is very important, and the number of messages sent and replied should be considerable, especially for large enterprises, so internal messages can be implemented in combination with the enterprise account of communication. The enterprise account has the characteristics of focusing on security and unlimited messaging, which is very suitable for the internal environment of the enterprise. This article mainly introduces how to use the enterprise account to send text, pictures, files, voice, video, graphic messages and other messages.

1. Features of enterprise account

For enterprise account, there are the following features:

1) Focus on more security

– only enterprise communication Only registered members can follow the enterprise account, and various features such as hierarchical administrators and confidential messages ensure the security of internal information of the enterprise.

Enterprises can set up self-verification of follower identities and perform secondary security verification to ensure the security of the use and transmission of corporate information.

If an employee resigns, the company administrator can delete the member in the address book, and the member will automatically unfollow the company account, and the history of the company account in WeChat will also be cleared.

2) Application configurability

– Enterprises can configure multiple service accounts in their enterprise accounts, and can connect to different enterprise application systems. Only authorized enterprise members can use the corresponding services. Number.

3) Unlimited messages

– There are no restrictions on sending messages, and it provides a complete management interface and WeChat native capabilities to adapt to complex and personalized application scenarios of enterprises.

Enterprises can proactively send messages to employees, the volume of messages is not limited.

4) More convenient to use

– Enterprise accounts have a unified message entrance in WeChat, and users can manage enterprise account messages more conveniently. WeChat address books can also directly access applications in the enterprise account.

2. Enterprise account management interface content

The current content of the enterprise account can be displayed using the following hierarchical diagram, including material management, passive response messages, For details on address book management, custom menus, etc., please see the diagram below.

C#开发微信门户及应用-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)

3. Enterprise account message and event processing

Enterprise account, like the official account, can be divided into message processing and event processing Processing, the following are their two types of processing operations, that is, the messages sent include text messages, picture messages, file messages, video messages, voice messages, geographical text messages, graphic and multimedia messages, etc.

Event processing mainly includes follow and unfollow events, as well as menu click type and view type operations, as well as geographical location reporting events, etc.

The two types of processing diagrams are shown below.

C#开发微信门户及应用-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)

4. Enterprise account message management

In the enterprise management background, just like the official account, you can see the corresponding information exchange Records, including text, pictures, geographical location, etc., are as follows.

C#开发微信门户及应用-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)

Because messages are divided into several types, including text (Text), picture (Image), file (File), voice (Voice), video (Video), picture News, MpNews, etc.

So we need to define and encapsulate them respectively. The following is their information object design diagram.

C#开发微信门户及应用-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)

The official definition of messages sent by enterprise accounts is as follows:

Enterprises can proactively send messages to employees, The amount of messages is not Restricted.

When calling the interface, use the HTTPS protocol and JSON data packet format. The data packet does not need to be encrypted.

Currently supports text, picture, voice, video, file, graphics and other message types. In addition to the news type, other types of messages can be sent with confidentiality options. Confidential messages will be watermarked and can only be read by the recipient.

We take the text message sent as an example to illustrate. Its definition is as follows.

  • text message

{
   "touser": "UserID1|UserID2|UserID3",
   "toparty": " PartyID1 | PartyID2 ",
   "totag": " TagID1 | TagID2 ",
   "msgtype": "text",
   "agentid": "1",
   "text": {
       "content": "Holiday Request For Pony(http://www.php.cn/)"
   },
   "safe":"0"
}
Copy after login

Parameters Required Description
touser No UserID list (message recipients, multiple recipients separated by '|') . Special case: Specify @all, then send
toparty No PartyID list to all members who follow the enterprise application, multiple acceptance They are separated by '|'. This parameter is ignored when touser is @all
totag No TagID list, multiple recipients are separated by ‘|’. This parameter is ignored when touser is @all.
msgtype is the message type, which is fixed at this time: text
agentid is the ID of the enterprise application, an integer. You can view it on the app’s settings page
content is message content
safe No indicates whether it is confidential information, 0 means no, 1 means yes, the default is 0

Each message will contain the following messages, which are their common attributes:


    touser": "UserID1|UserID2|UserID3",   "toparty": " PartyID1 | PartyID2 ",   "totag": " TagID1 | TagID2 ",   "msgtype": "text",   "agentid": "1",
Copy after login


So we can define a base class to conveniently carry this common information.


    /// <summary>
    /// 企业号发送消息的基础消息内容    /// </summary>
    public class CorpSendBase
    {      
        /// <summary>
        /// UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送        /// </summary>
        public string touser { get; set; }        /// <summary>
        /// PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数        /// </summary>
        public string toparty { get; set; }        /// <summary>
        /// TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数        /// </summary>
        public string totag { get; set; }        /// <summary>
        /// 消息类型        /// </summary>
        public string msgtype { get; set; }        /// <summary>
        /// 企业应用的id,整型。可在应用的设置页面查看        /// </summary>
        public string agentid { get; set; }        /// <summary>
        /// 表示是否是保密消息,0表示否,1表示是,默认0        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]        public string safe { get; set; }

    }
Copy after login


Then other messages can inherit this base class one by one, as shown below.

C#开发微信门户及应用-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)

will eventually form the inheritance diagram below.

C#开发微信门户及应用-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)

5. Definition and implementation of message interface

After defining the relevant sending objects, we can define its unified sending interface, as follows Show.


    /// <summary>
    /// 企业号消息管理接口定义    /// </summary>
    public interface ICorpMessageApi
    {        
        /// <summary>
        /// 发送消息。        /// 需要管理员对应用有使用权限,对收件人touser、toparty、totag有查看权限,否则本次调用失败。        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        CommonResult SendMessage(string accessToken, CorpSendBase data);
    }
Copy after login


Finally, text and other types of messages will be implemented according to the interface definition, and the implementation code is as follows. Note that the sending process does not require calling the encryption class for encryption .


    /// <summary>
    /// 企业号消息管理实现类    /// </summary>
    public class CorpMessageApi : ICorpMessageApi
    {        /// <summary>
        /// 发送消息。        /// 需要管理员对应用有使用权限,对收件人touser、toparty、totag有查看权限,否则本次调用失败。        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public CommonResult SendMessage(string accessToken, CorpSendBase data)
        {        
            CommonResult result = new CommonResult();            string urlFormat = "http://www.php.cn/{0}";            var url = string.Format(urlFormat, accessToken);            var postData = data.ToJson();            //数据不用加密发送
            CorpSendResult sendResult = CorpJsonHelper<CorpSendResult>.ConvertJson(url, postData);            if (sendResult != null)
            {
                result.Success = (sendResult.errcode == CorpReturnCode.请求成功);
                result.ErrorMessage = string.Format("invaliduser:{0},invalidparty:{1},invalidtag:{2}",
                    sendResult.invaliduser, sendResult.invalidparty, sendResult.invalidtag);
            }            return result;
        }
    }
Copy after login


6. Message sending operation and actual effect

After defining the corresponding sending object, we will Unified message sending operations can be performed, including text, picture, file, voice and other types of messages. Note that some messages need to be uploaded to the server and then sent out according to the mediaId.

The operation code for sending text and pictures is as follows.


        private void btnSendText_Click(object sender, EventArgs e)
        {            //发送文本内容
            ICorpMessageApi bll = new CorpMessageApi();

            CorpSendText text = new CorpSendText("API 中文测试(http://www.php.cn/)");
            text.touser = "wuhuacong";
            text.toparty = "4";//部门ID
            text.totag = "0";

            text.safe = "0";
            text.agentid = "0";

            CommonResult result = bll.SendMessage(token, text);            if (result != null)
            {
                Console.WriteLine("发送消息:{0} {1} {2}", text.text.content, (result.Success ? "成功" : "失败"), result.ErrorMessage);
            }
        }        private void btnSendImage_Click(object sender, EventArgs e)
        {
            btnUpload_Click(sender, e);            if (!string.IsNullOrEmpty(image_mediaId))
            {                //发送图片内容
                ICorpMessageApi bll = new CorpMessageApi();

                CorpSendImage image = new CorpSendImage(image_mediaId);
                CommonResult result = bll.SendMessage(token, image);                if (result != null)
                {
                    Console.WriteLine("发送图片消息:{0} {1} {2}", image_mediaId, (result.Success ? "成功" : "失败"), result.ErrorMessage);
                }
            }
        }
Copy after login


The final screenshot on the WeChat enterprise account is as shown below, including text test, file test, graphic test, and voice test The tests are all normal.

C#开发微信门户及应用-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等) C#开发微信门户及应用-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)

More C# development of WeChat portals and applications - WeChat enterprise account messaging (text, pictures, files, voice, video, graphic messages, etc.) related Please pay attention to the PHP Chinese website for articles!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template