How to create Messaging End User using Apex in Salesforce?

Sample Code:
MessagingEndUser objMsgUser = new MessagingEndUser();
objMsgUser.ContactId = '<ContactId>';
objMsgUser.MessagingChannelId = '<Messaging Channel Id>';
objMsgUser.MessagingConsentStatus = 'ImplicitlyOptedIn';
objMsgUser.MessageType = 'Text';//For SMS Channel
/*The phone number or Facebook page ID associated with this Messaging end user.*/
objMsgUser.MessagingPlatformKey = '<Platform Key>';
objMsgUser.Name = '<Name>';
insert objMsgUser; 
To get the Messaging Channel Id, you can use the below SOQL.
SELECT Id, DeveloperName FROM MessagingChannel