Example of message sending in MyChat in Delphi XE3
Frankly speaking, the program can be written in anything, because it uses the standard mechanism of dynamic libraries for all Windows programs DLL. But the author of this article is related to Delphi programming environment, so the example deals exactly with it.
Our application will be console without user interface. Run the programming environment and choose in the main menu "File" "New" "Other":
In the window, choose"Console Application":
As we do not need any graphic interface for our program, the console application is more suitable.
We need a 32-bit DLL library of MyChat Integration API (C:\Program Files (x86)\MyChat Server\doc\IntegrationAPI\MyChatIntegrationAPIDLLs\x32\mychat.dll) to connect to MyChat Server and send a text message to a specified user.
A simple example shows how to send a text message that consists of two lines on behalf of built-in bot Elisa.
Program text:
// MyChat Integration API sample
// Copyright (c) 2012-2016 by Alexey Pikurov / Network Software Solutions
// https://nsoft-s.com/aboutmychat.html
// [email protected]
//
// Tested on Delphi XE3
// Support, questions, comments: https://nsoft-s.com/mychatforum
//
// 22.02.2016
program TestMyChatDLL;
{$APPTYPE CONSOLE}
uses Windows;
const
MCIAPI_CS_SendPrivateMessage = 2;
MCIAPI_CS_IsUINOnline = 3;
MCIAPI_CS_SendChannelMessage = 4;
MCIAPI_CS_GetUINByNick = 5;
MCIAPI_CS_GetUINByEmail = 6;
MCIAPI_CS_IsUINExists = 7;
MCIAPI_CS_AddBBSMessage = 8;
MCIAPI_SC_SetChannelTopic = 9;
MCIAPI_CS_GetChannelNameByUID = 10;
MCIAPI_CS_IsChannelExists = 11;
MCIAPI_CS_GetUINByADLogin = 12;
MCIAPI_CS_GetInfoByUIN = 13;
MCIAPI_CS_GetServerInfo = 14;
var
mychatGetLibVersion: function:WideString;safecall;
mychatGetLibReleaseDate: function:WideString;safecall;
mychatSendData: function (sIP: WideString; iPort: DWORD; sServerKey: WideString; iCmd: DWORD; sJSONData: WideString): WideString;safecall;
LibHandle: THandle;
sData: WideString;
begin
// clean variables from trash
@mychatGetLibVersion := nil;
@mychatGetLibReleaseDate := nil;
@mychatSendData := nil;
// trying to load the library
LibHandle := LoadLibrary('mychat.dll');
// if everything is ??
if LibHandle >= 32 then begin
// ...then trying to get an address of the function in the library
mychatGetLibVersion := GetProcAddress(LibHandle, 'mychatGetLibVersion');
@mychatGetLibReleaseDate := GetProcAddress(LibHandle, 'mychatGetLibReleaseDate');
@mychatSendData := GetProcAddress(LibHandle, 'mychatSendData');
Writeln(mychatGetLibVersion + ' / ' + mychatGetLibReleaseDate);
// if everything is ??
sData := '{"UserTo":"6","UserFrom":"0","Msg":"Hello, how are you?'+ #13#10#13#10 + 'Call me back after work"}';
if @mychatSendData <> nil then
// ...then call this function and show the result
writeln(mychatSendData('mychat-server.com', 2004, 'iddqd' , MCIAPI_CS_SendPrivateMessage, sData));
end;
// ...do not forget to free a memory and unload DLL
FreeLibrary(LibHandle);
end.
A received message in a client looks like this: