Пример отправки приватного сообщения в чат на языке Python
Отправляется приватное сообщение от имени встроенного бота (UIN 0) для пользователя с UIN = 1.
# MyChat IntegrationAPI Python 2.7 example
# Created by Network Software Solutions
# Jun 15, 2016
# support@nsoft-s.com
# https://www.nsoft-s.com/forum/viewtopic.php?p=22953#p22953 (russian support)
# --------------------------------------------------------------------------
import socket
import json
import struct
HOST = "127.0.0.1"
PORT = 2004
dict_obj = {
"UserTo": 1,
"UserFrom": 0,
"Msg": "Hello",
"hash": "",
"APIStype": "python",
"ServerKey": "iddqd"
}
CRLF = r"\r\n"; # #13#10
CRLF = struct.pack("BB", 13, 10) # #13#10
MCIAPI_CS_SendPrivateMessage = "0002"
MCIAPI_CS_IsUINOnline = "0003"
MCIAPI_CS_SendChannelMessage = "0004"
MCIAPI_CS_GetUINByNick = "0005"
MCIAPI_CS_GetUINByEmail = "0006"
MCIAPI_CS_IsUINExists = "0007"
MCIAPI_CS_AddBBSMessage = "0008"
MCIAPI_SC_SetChannelTopic = "0009"
MCIAPI_CS_GetChannelNameByUID = "0010"
MCIAPI_CS_IsChannelExists = "0011"
MCIAPI_CS_GetUINByADLogin = "0012"
MCIAPI_CS_GetInfoByUIN = "0013"
MCIAPI_CS_GetServerInfo = "0014"
MagicPacket = struct.pack("BB", 23, 6) # B - unsigned char. 1 byte
cs_integration_api = "0077"
iFlag = "30"
try:
try:
skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as e:
print ("Error creating socket: %s" % e)
try:
skt.connect((HOST, PORT))
except socket.gaierror as e:
print("Address-related error connecting to server: %s" % e)
except socket.error as e:
print("Error connecting to socket: %s" % e)
try:
skt.send(b'mc5.20\r\n')
data = skt.recv(1024)
print(bytes.decode(data))
skt.send(MagicPacket + cs_integration_api + iFlag + MCIAPI_CS_SendPrivateMessage + json.dumps(dict_obj) + CRLF)
data = skt.recv(1024)
print bytes.decode(data)
except socket.error as e:
print("Error connecting to socket: %s" % e)
finally:
skt.close()