-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPyChatModule.cpp
760 lines (630 loc) · 21.9 KB
/
PyChatModule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
#include "PyChatModule.h"
#include <TVAgentAPI/ChatModuleStringify.h>
#include <TVAgentAPI/IChatModule.h>
#include <TVAgentAPI/IAgentAPI.h>
#include <TVAgentAPI/IAgentConnection.h>
#include "PyAgentConnection.h"
#include "PyTVAgentAPI.h"
#include "PythonHelpers.h"
#include "Typesystem.h"
#include <assert.h>
#include <iostream>
using namespace tvagentapipy;
namespace
{
// Methods
PyObject* CreateMessageDict(const tvagentapi::IChatModule::ChatMessage* message)
{
if (!message)
{
return nullptr;
}
PyObject* pyChatID = PyUnicode_FromString(message->chatID);
PyObject* pyMessageID = PyUnicode_FromString(message->messageID);
PyObject* pyContent = PyUnicode_FromString(message->content);
PyObject* pyTimeStamp = PyLong_FromLong(message->timeStamp);
PyObject* pySender = PyUnicode_FromString(message->sender);
PyObject* pyMessage = PyDict_New();
assert(pyMessage);
PyDict_SetItemString(pyMessage, "chatID", pyChatID);
PyDict_SetItemString(pyMessage, "messageID", pyMessageID);
PyDict_SetItemString(pyMessage, "content", pyContent);
PyDict_SetItemString(pyMessage, "timeStamp", pyTimeStamp);
PyDict_SetItemString(pyMessage, "sender", pySender);
Py_DECREF(pyChatID);
Py_DECREF(pyMessageID);
Py_DECREF(pyContent);
Py_DECREF(pyTimeStamp);
Py_DECREF(pySender);
return pyMessage;
}
PyObject* PyChatModule_setCallbacks(
PyChatModule* self,
PyObject* args,
PyObject* kwargs)
{
char chatCreatedCallbackArgName[] = "chatCreatedCallback";
char chatsRemovedCallbackArgName[] = "chatsRemovedCallback";
char chatClosedCallbackArgName[] = "chatClosedCallback";
char receivedMessagesCallbackArgName[] = "receivedMessagesCallback";
char sendMessageFinishedCallbackArgName[] = "sendMessageFinishedCallback";
char messagesLoadedCallbackArgName[] = "messagesLoadedCallback";
char historyDeletedCallbackArgName[] = "historyDeletedCallback";
PyObject* chatCreatedCallback = Py_None;
PyObject* chatsRemovedCallback = Py_None;
PyObject* chatClosedCallback = Py_None;
PyObject* receivedMessagesCallback = Py_None;
PyObject* sendMessageFinishedCallback = Py_None;
PyObject* messagesLoadedCallback = Py_None;
PyObject* historyDeletedCallback = Py_None;
char* kwargList[] = {
chatCreatedCallbackArgName,
chatsRemovedCallbackArgName,
chatClosedCallbackArgName,
receivedMessagesCallbackArgName,
sendMessageFinishedCallbackArgName,
messagesLoadedCallbackArgName,
historyDeletedCallbackArgName,
{}};
if (!PyArg_ParseTupleAndKeywords(
args,
kwargs,
"|OOOOOOO:setCallbacks",
kwargList,
&chatCreatedCallback,
&chatsRemovedCallback,
&chatClosedCallback,
&receivedMessagesCallback,
&sendMessageFinishedCallback,
&messagesLoadedCallback,
&historyDeletedCallback))
{
return nullptr;
}
chatCreatedCallback = chatCreatedCallback == Py_None ? nullptr : chatCreatedCallback;
chatsRemovedCallback = chatsRemovedCallback == Py_None ? nullptr : chatsRemovedCallback;
chatClosedCallback = chatClosedCallback == Py_None ? nullptr : chatClosedCallback;
receivedMessagesCallback = receivedMessagesCallback == Py_None ? nullptr : receivedMessagesCallback;
sendMessageFinishedCallback = sendMessageFinishedCallback == Py_None ? nullptr : sendMessageFinishedCallback;
messagesLoadedCallback = messagesLoadedCallback == Py_None ? nullptr : messagesLoadedCallback;
historyDeletedCallback = historyDeletedCallback == Py_None ? nullptr : historyDeletedCallback;
if (chatCreatedCallback && !PyCallable_Check(chatCreatedCallback))
{
PyErr_Format(PyExc_TypeError, "%R is not callable", chatCreatedCallback);
return nullptr;
}
if (chatsRemovedCallback && !PyCallable_Check(chatsRemovedCallback))
{
PyErr_Format(PyExc_TypeError, "%R is not callable", chatsRemovedCallback);
return nullptr;
}
if (chatClosedCallback && !PyCallable_Check(chatClosedCallback))
{
PyErr_Format(PyExc_TypeError, "%R is not callable", chatClosedCallback);
return nullptr;
}
if (receivedMessagesCallback && !PyCallable_Check(receivedMessagesCallback))
{
PyErr_Format(PyExc_TypeError, "%R is not callable", receivedMessagesCallback);
return nullptr;
}
if (sendMessageFinishedCallback && !PyCallable_Check(sendMessageFinishedCallback))
{
PyErr_Format(PyExc_TypeError, "%R is not callable", sendMessageFinishedCallback);
return nullptr;
}
if (messagesLoadedCallback && !PyCallable_Check(messagesLoadedCallback))
{
PyErr_Format(PyExc_TypeError, "%R is not callable", messagesLoadedCallback);
return nullptr;
}
if (historyDeletedCallback && !PyCallable_Check(historyDeletedCallback))
{
PyErr_Format(PyExc_TypeError, "%R is not callable", historyDeletedCallback);
return nullptr;
}
Py_XINCREF(chatCreatedCallback);
Py_XDECREF(self->m_pyChatCreatedCallback);
self->m_pyChatCreatedCallback = chatCreatedCallback;
Py_XINCREF(chatsRemovedCallback);
Py_XDECREF(self->m_pyChatsRemovedCallback);
self->m_pyChatsRemovedCallback = chatsRemovedCallback;
Py_XINCREF(chatClosedCallback);
Py_XDECREF(self->m_pyChatClosedCallback);
self->m_pyChatClosedCallback = chatClosedCallback;
Py_XINCREF(receivedMessagesCallback);
Py_XDECREF(self->m_pyReceivedMessagesCallback);
self->m_pyReceivedMessagesCallback = receivedMessagesCallback;
Py_XINCREF(sendMessageFinishedCallback);
Py_XDECREF(self->m_pySendMessageFinishedCallback);
self->m_pySendMessageFinishedCallback = sendMessageFinishedCallback;
Py_XINCREF(messagesLoadedCallback);
Py_XDECREF(self->m_pyMessagesLoadedCallback);
self->m_pyMessagesLoadedCallback = messagesLoadedCallback;
Py_XINCREF(historyDeletedCallback);
Py_XDECREF(self->m_pyHistoryDeletedCallback);
self->m_pyHistoryDeletedCallback = historyDeletedCallback;
auto chatCreated = [](
const char* chatId,
const char* title,
tvagentapi::IChatModule::ChatEndpointType chatEndpointType,
uint64_t chatTypeId,
void* userdata)
{
auto pyChatCreatedCallback = static_cast<PyObject*>(userdata);
PyObject* chatEndpointTypeEnumVal = PyEnumValue(
GetPyTypeChatModule_ChatEndpointType(),
tvagentapi::toCString(chatEndpointType));
PyObject* args = Py_BuildValue("(ssOl)", chatId, title, chatEndpointTypeEnumVal, chatTypeId);
PyObject* result = PyObject_CallObject(pyChatCreatedCallback, args);
Py_DECREF(args);
Py_XDECREF(result);
Py_DECREF(chatEndpointTypeEnumVal);
};
auto chatsRemoved = [](
const char** chatIDs,
size_t chatIDsCount,
void* userdata)
{
auto pyChatRemovedCallback = static_cast<PyObject*>(userdata);
PyObject* chadIDsList = PyList_New(chatIDsCount);
assert(chadIDsList);
for (size_t i = 0; i < chatIDsCount; i++)
{
PyObject* chatID = PyUnicode_FromString(chatIDs[i]);
assert(chatID);
PyList_SET_ITEM(chadIDsList, i, chatID);
}
PyObject* args = Py_BuildValue("(O)", chadIDsList);
PyObject* result = PyObject_CallObject(pyChatRemovedCallback, args);
Py_DECREF(args);
Py_XDECREF(result);
};
auto chatClosed = [](
const char* chatID,
void* userdata)
{
auto pyChatClosedCallback = static_cast<PyObject*>(userdata);
PyObject* args = Py_BuildValue("(s)", chatID);
PyObject* result = PyObject_CallObject(pyChatClosedCallback, args);
Py_DECREF(args);
Py_XDECREF(result);
};
auto receivedMessages = [](
const tvagentapi::IChatModule::ChatMessage* messages,
size_t messagesCount,
void* userdata)
{
auto receivedMessagesCallback = static_cast<PyObject*>(userdata);
PyObject* messagesList = PyList_New(messagesCount);
assert(messagesList);
for (size_t i = 0; i < messagesCount; i++)
{
PyObject* message = CreateMessageDict(&messages[i]);
PyList_SET_ITEM(messagesList, i, message);
}
PyObject* args = Py_BuildValue("(O)", messagesList);
PyObject* result = PyObject_CallObject(receivedMessagesCallback, args);
Py_DECREF(args);
Py_XDECREF(result);
Py_DECREF(messagesList);
};
auto sendMessageFinished = [](
tvagentapi::IChatModule::RequestID requestID,
tvagentapi::IChatModule::SendMessageResult sendMessageResult,
const char* messageID,
tvagentapi::IChatModule::TimeStamp timeStamp,
void* userdata)
{
auto pySendMessageFinishedCallback = static_cast<PyObject*>(userdata);
PyObject* sendMessageResultEnumVal = PyEnumValue(
GetPyTypeChatModule_SendMessageResult(),
tvagentapi::toCString(sendMessageResult));
PyObject* args = Py_BuildValue("(IOsk)", requestID, sendMessageResultEnumVal, messageID, timeStamp);
PyObject* result = PyObject_CallObject(pySendMessageFinishedCallback, args);
Py_DECREF(args);
Py_XDECREF(result);
Py_XDECREF(sendMessageResultEnumVal);
};
auto messagesLoaded = [](
const tvagentapi::IChatModule::ChatMessage* messages,
size_t messagesCount,
bool hasMore,
void* userdata)
{
auto messagesLoadedCallback = static_cast<PyObject*>(userdata);
PyObject* messagesList = PyList_New(messagesCount);
assert(messagesList);
for (size_t i = 0; i < messagesCount; i++)
{
PyObject* message = CreateMessageDict(&messages[i]);
PyList_SET_ITEM(messagesList, i, message);
}
PyObject* args = Py_BuildValue("(OO)", messagesList, hasMore ? Py_True : Py_False);
PyObject* result = PyObject_CallObject(messagesLoadedCallback, args);
Py_DECREF(args);
Py_XDECREF(result);
Py_DECREF(messagesList);
};
auto historyDeleted = [](
const char* chatID,
void* userdata)
{
auto historyDeletedCallback = static_cast<PyObject*>(userdata);
PyObject* args = Py_BuildValue("(s)", chatID);
PyObject* result = PyObject_CallObject(historyDeletedCallback, args);
Py_DECREF(args);
Py_XDECREF(result);
};
tvagentapi::IChatModule::ChatCreatedCallback chatCreatedCb{};
if (self->m_pyChatCreatedCallback)
{
chatCreatedCb = {chatCreated, self->m_pyChatCreatedCallback};
}
tvagentapi::IChatModule::ChatsRemovedCallback chatsRemovedCb{};
if (self->m_pyChatsRemovedCallback)
{
chatsRemovedCb = {chatsRemoved, self->m_pyChatsRemovedCallback};
}
tvagentapi::IChatModule::ChatClosedCallback chatClosedCb{};
if (self->m_pyChatClosedCallback)
{
chatClosedCb = {chatClosed, self->m_pyChatClosedCallback};
}
tvagentapi::IChatModule::ReceivedMessagesCallback receivedMessagesCb{};
if (self->m_pyReceivedMessagesCallback)
{
receivedMessagesCb = {receivedMessages, self->m_pyReceivedMessagesCallback};
}
tvagentapi::IChatModule::SendMessageFinishedCallback sendMessageFinishedCb{};
if (self->m_pySendMessageFinishedCallback)
{
sendMessageFinishedCb = {sendMessageFinished, self->m_pySendMessageFinishedCallback};
}
tvagentapi::IChatModule::MessagesLoadedCallback messagesLoadedCb{};
if (self->m_pyMessagesLoadedCallback)
{
messagesLoadedCb = {messagesLoaded, self->m_pyMessagesLoadedCallback};
}
tvagentapi::IChatModule::HistoryDeletedCallback historyDeletedCb{};
if (self->m_pyHistoryDeletedCallback)
{
historyDeletedCb = {historyDeleted, self->m_pyHistoryDeletedCallback};
}
self->m_module->setCallbacks({
chatCreatedCb,
chatsRemovedCb,
chatClosedCb,
receivedMessagesCb,
sendMessageFinishedCb,
messagesLoadedCb,
historyDeletedCb
});
Py_RETURN_NONE;
}
PyObject* PyChatModule_obtainChats(PyChatModule* self, PyObject* args)
{
(void)args;
auto enumerator = [](
const char* chatID,
const char* title,
tvagentapi::IChatModule::ChatEndpointType chatEndpointType,
uint64_t chatEndpointID,
tvagentapi::IChatModule::ChatState chatState,
void* userdata) -> bool
{
auto pyList = static_cast<PyObject*>(userdata);
PyObject* pyChatID = PyUnicode_FromString(chatID);
PyObject* pyTitle = PyUnicode_FromString(title);
PyObject* pyChatEndpointType = PyEnumValue(GetPyTypeChatModule_ChatEndpointType(), toCString(chatEndpointType));
PyObject* pyChatEndpointID = PyLong_FromUnsignedLongLong(chatEndpointID);
PyObject* pyChatState = PyEnumValue(GetPyTypeChatModule_ChatState(), toCString(chatState));
PyObject* pyChatInfo = PyDict_New();
PyDict_SetItemString(pyChatInfo, "chatID", pyChatID);
PyDict_SetItemString(pyChatInfo, "title", pyTitle);
PyDict_SetItemString(pyChatInfo, "chatEndpointType", pyChatEndpointType);
PyDict_SetItemString(pyChatInfo, "chatEndpointID", pyChatEndpointID);
PyDict_SetItemString(pyChatInfo, "chatState", pyChatState);
Py_DECREF(pyChatID);
Py_DECREF(pyTitle);
Py_DECREF(pyChatEndpointType);
Py_DECREF(pyChatEndpointID);
Py_DECREF(pyChatState);
PyList_Append(pyList, pyChatInfo);
Py_DECREF(pyChatInfo);
return true;
};
PyObject* pyList = PyList_New(/*size=*/0);
if (!self->m_module->obtainChats({enumerator, pyList}))
{
Py_DECREF(pyList);
PyErr_SetString(PyExc_RuntimeError, "Failed to obtain chats");
return nullptr;
}
return pyList;
}
PyObject* PyChatModule_sendMessage(PyChatModule* self, PyObject* args)
{
const char* chatID = nullptr;
const char* content = nullptr;
if (!PyArg_ParseTuple(args, "ss", &chatID, &content))
{
return nullptr;
}
const tvagentapi::IChatModule::RequestID requestID = self->m_module->sendMessage(
chatID,
content);
if (requestID == tvagentapi::IChatModule::InvalidRequestID)
{
PyErr_SetString(PyExc_RuntimeError, "Internal call has failed");
return nullptr;
}
return PyLong_FromUnsignedLong(requestID);
}
PyObject* PyChatModule_loadMessages(PyChatModule* self, PyObject* args)
{
const char* chatID = nullptr;
PyObject* pyMessageCount = nullptr;
const char* messagesBefore = nullptr;
if (!PyArg_ParseTuple(args, "sO|z", &chatID, &pyMessageCount, &messagesBefore))
{
return nullptr;
}
const size_t messageCount = PyLong_AsSize_t(pyMessageCount);
if (PyErr_Occurred())
{
return nullptr;
}
return NoneOrInternalError(self->m_module->loadMessages(
chatID,
messageCount,
messagesBefore));
}
PyObject* PyChatModule_deleteHistory(PyChatModule* self, PyObject* arg)
{
if (!PyUnicode_Check(arg))
{
PyErr_Format(PyExc_TypeError, "expected string instance, got %R", arg);
return nullptr;
}
return NoneOrInternalError(self->m_module->deleteHistory(PyUnicode_AsUTF8(arg)));
}
PyObject* PyChatModule_deleteChat(PyChatModule* self, PyObject* arg)
{
if (!PyUnicode_Check(arg))
{
PyErr_Format(PyExc_TypeError, "expected string instance, got %R", arg);
return nullptr;
}
return NoneOrInternalError(self->m_module->deleteChat(PyUnicode_AsUTF8(arg)));
}
PyObject* PyChatModule_isSupported(PyChatModule* self, PyObject* args)
{
(void)args;
if (self->m_module->isSupported())
{
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}
namespace DocStrings
{
PyDoc_STRVAR(isSupported,
R"__(isSupported($self)
--
Returns whether the current module is supported by the SDK and the IoT Agent counterpart.
:return True if the current module is supported, False otherwise.
)__");
PyDoc_STRVAR(setCallbacks,
R"__(setCallbacks($self, /,
chatCreatedCallback=None,
chatsRemovedCallback=None,
chatClosedCallback=None,
receivedMessagesCallback=None,
sendMessageFinishedCallback=None,
messagesLoadedCallback=None,
historyDeletedCallback=None)
--
Sets callbacks to handle various events like chat room and message updates.
:param chatCreatedCallback: called when a new chat room has been created
:type chatCreatedCallback: callback(str chatId, str title, tvagentapi.ChatModule.ChatEndpointType chatEndpointType, int chatTypeId)
:param chatsRemovedCallback: called when a chat room has been deleted
:type chatsRemovedCallback: callback(list chadIDsList)
:param chatClosedCallback: called when a chat room has been closed
:type chatClosedCallback: callback(str chatID)
:param receivedMessagesCallback: called when messages have been received. The given list consists of dictionaries - one for each message - with the keys 'chatID', 'message', 'content', 'timeStamp', 'sender'.
:type receivedMessagesCallback: callback(list messageList)
:param sendMessageFinishedCallback: called when the result for a send request from sendMessage() is available
:type sendMessageFinishedCallback: callback(int requestID, tvagentapi.ChatModule.SendMessageResult sendMessageResult, str messageID, int timeStamp)
:param messagesLoadedCallback: called as response of a loadMessages() call. The given list of messages consists of dictionaries - one for each message - with the keys 'chatID', 'message', 'content', 'timeStamp', 'sender'.
:type messagesLoadedCallback: callback(list messagesList, bool hasMore)
:param historyDeletedCallback: called when the history of a chat room has been deleted
:type historyDeletedCallback: callback(str chatID)
)__");
PyDoc_STRVAR(obtainChats,
R"__(obtainChats($self)
--
Requests the list of active chats.
:return list of dictionaries containing data for "chatID", "title", "chatEndpointType", "chatEndpointID", "chatState".
)__");
PyDoc_STRVAR(sendMessage,
R"__(sendMessage($self, chatID, content)
--
Requests to send the given content as a message to the given chat.
The returned request ID can be used to match a call of sendMessageFinishedCallback (see setCallbacks).
:param str chatID: identifier of the chat the message should be sent to.
:param str content: the message to be sent.
:return An int identifier for the current request.
)__");
PyDoc_STRVAR(loadMessages,
R"__(loadMessages($self, chatID, messageCount, messagesBefore=None)
--
Requests to load messages from history for the given chat ID.
:param str chatID: identifier of the chat messages should be loaded from.
:param int messageCount: maximum number of messages to load.
:param str messagesBefore: load messages older than (but not including) message with given message ID.
:return True if this function call is successful, False otherwise.
)__");
PyDoc_STRVAR(deleteHistory,
R"__(deleteHistory($self, chatID)
--
Requests to delete the history for given chat.
:param str chatID: identifier of the chat to be deleted.
)__");
PyDoc_STRVAR(deleteChat,
R"__(deleteChat($self, chatID)
--
Requests to delete the chat by given chat ID.
:param str chatID: identifier of the chat to be deleted.
)__");
} // namespace DocStrings
PyMethodDef PyChatModule_methods[] =
{
{
"isSupported",
WeakConnectionCall<PyChatModule, PyChatModule_isSupported>,
METH_NOARGS,
DocStrings::isSupported
},
{
"setCallbacks",
PyCFunctionCast(WeakConnectionCall<PyChatModule, PyChatModule_setCallbacks>),
METH_VARARGS | METH_KEYWORDS,
DocStrings::setCallbacks
},
{
"obtainChats",
WeakConnectionCall<PyChatModule, PyChatModule_obtainChats>,
METH_NOARGS,
DocStrings::obtainChats
},
{
"sendMessage",
WeakConnectionCall<PyChatModule, PyChatModule_sendMessage>,
METH_VARARGS,
DocStrings::sendMessage
},
{
"loadMessages",
WeakConnectionCall<PyChatModule, PyChatModule_loadMessages>,
METH_VARARGS,
DocStrings::loadMessages
},
{
"deleteHistory",
WeakConnectionCall<PyChatModule, PyChatModule_deleteHistory>,
METH_O,
DocStrings::deleteHistory
},
{
"deleteChat",
WeakConnectionCall<PyChatModule, PyChatModule_deleteChat>,
METH_O,
DocStrings::deleteChat
},
{} // Sentinel
};
} // namespace
namespace tvagentapipy
{
PyChatModule::PyChatModule(PyAgentConnection* pyAgentConnection)
: m_pyWeakAgentConnection{reinterpret_cast<PyObject*>(pyAgentConnection)}
{
m_module = static_cast<tvagentapi::IChatModule*>(
pyAgentConnection->m_connection->getModule(
tvagentapi::IModule::Type::Chat
));
}
PyChatModule::~PyChatModule()
{
Py_XDECREF(m_pyChatCreatedCallback);
Py_XDECREF(m_pyChatsRemovedCallback);
Py_XDECREF(m_pyChatClosedCallback);
Py_XDECREF(m_pyReceivedMessagesCallback);
Py_XDECREF(m_pySendMessageFinishedCallback);
Py_XDECREF(m_pyMessagesLoadedCallback);
Py_XDECREF(m_pyHistoryDeletedCallback);
}
bool PyChatModule::IsReady() const
{
return !!m_module;
}
PyTypeObject* GetPyTypeChatModule()
{
static PyTypeObject* pyChatModuleType = []() -> PyTypeObject*
{
static PyTypeObject result = PyTypeObjectInitialized();
result.tp_name = "tvagentapi.ChatModule";
result.tp_basicsize = sizeof(PyChatModule);
result.tp_dealloc =
reinterpret_cast<destructor>(DeallocWrapperObject<PyChatModule>);
result.tp_flags = Py_TPFLAGS_DEFAULT;
result.tp_doc = "Chat module class";
result.tp_methods = PyChatModule_methods;
result.tp_dict = PyDict_New();
PyDict_SetItemString(
result.tp_dict,
"ChatEndpointType",
reinterpret_cast<PyObject*>(GetPyTypeChatModule_ChatEndpointType()));
PyDict_SetItemString(
result.tp_dict,
"ChatState",
reinterpret_cast<PyObject*>(GetPyTypeChatModule_ChatState()));
PyDict_SetItemString(
result.tp_dict,
"SendMessageResult",
reinterpret_cast<PyObject*>(GetPyTypeChatModule_SendMessageResult()));
if (PyType_Ready(&result) < 0)
{
return nullptr;
}
return &result;
}();
return pyChatModuleType;
}
PyTypeObject* GetPyTypeChatModule_ChatEndpointType()
{
static PyTypeObject* pyTypeChatModule_ChatEndpointType = []() -> PyTypeObject*
{
using ChatEndpointType = tvagentapi::IChatModule::ChatEndpointType;
using tvagentapi::toCString;
PyTypeObject* result =
CreateEnumType(
"tvagentapi.ChatModule.ChatEndpointType",
{{toCString(ChatEndpointType::Machine), static_cast<long>(ChatEndpointType::Machine)},
{toCString(ChatEndpointType::InstantSupportSession), static_cast<long>(ChatEndpointType::InstantSupportSession)}});
return result;
}();
return pyTypeChatModule_ChatEndpointType;
}
PyTypeObject* GetPyTypeChatModule_ChatState()
{
static PyTypeObject* pyTypeChatModule_ChatState = []() -> PyTypeObject*
{
using ChatState = tvagentapi::IChatModule::ChatState;
using tvagentapi::toCString;
PyTypeObject* result =
CreateEnumType(
"tvagentapi.ChatModule.ChatState",
{{toCString(ChatState::Open), static_cast<long>(ChatState::Open)},
{toCString(ChatState::Closed), static_cast<long>(ChatState::Closed)}});
return result;
}();
return pyTypeChatModule_ChatState;
}
PyTypeObject* GetPyTypeChatModule_SendMessageResult()
{
static PyTypeObject* pyTypeChatModule_SendMessageResult = []() -> PyTypeObject*
{
using SendMessageResult = tvagentapi::IChatModule::SendMessageResult;
using tvagentapi::toCString;
PyTypeObject* result =
CreateEnumType(
"tvagentapi.ChatModule.SendMessageResult",
{{toCString(SendMessageResult::Succeeded), static_cast<long>(SendMessageResult::Succeeded)},
{toCString(SendMessageResult::Failed), static_cast<long>(SendMessageResult::Failed)}});
return result;
}();
return pyTypeChatModule_SendMessageResult;
}
} // namespace tvagentapipy