Sending Unicode and Emoji

241 views
Skip to first unread message

Chicago David

unread,
Jun 25, 2023, 11:52:26 PM6/25/23
to Programmers
There was a previous conversation about sending Unicode (actually Vietnamese text) and Emoji in a string to Paltalk RichText control. There was a bunch of robot advice that I think was probably wrong. Is there any way of doing this? So far the advice by Paltalk seems to end up printing ASCII only. Unicode winds up as "?" question marks rendered into the room suggesting that something isn't handling Unicode correctly.
I'm using 1.29.2.91046 GA, and not a Beta.
I can send unicode using UIAutomation, but that brings the Paltalk room window to the front and I'd rather use SendMessage which won't do that. Using SendMessage means one doesn't have to dedicate a computer to run the bot.

VN

unread,
Jun 28, 2023, 3:57:07 AM6/28/23
to Programmers
Paltalk currently cannot send accented Vietnamese, if you use the SendMessage function. (This function can only display all Latin alphabet characters. but can't see the icon and Vietnamese )
And if you change to the SendMessageW function, then in your text, when sent to the room, it only shows 1 character (line break).
As Chicago David explained, if you want to send Vietnamese text with accents and symbols, you must use UIAutomation.

here is an example command to check the properties of the chat window

def send_text_with_pywinauto(text):
    windows = findwindows.find_elements(class_name='DlgGroupChat Window Class')
    print('Windows with class name "DlgGroupChat Window Class":', windows)
    app = Application().connect(title_re=".*" + room_list[0] + ".*")
    chat_window = app.window(title_re=".*" + room_list[0] + ".*")
    print('Chat window:', chat_window)
    if chat_window.exists():
        properties = chat_window.get_properties()
        for property_name, property_value in properties.items():
            print(f"{property_name}: {property_value}")
        chat_window.set_focus()
        chat_window.send_chars(text)
        chat_window.send_keystrokes('{ENTER}')
    else:
        print('Chat window not found')

_____________________________________________________________

Example in my chat room ( Please look at the bold text to understand the properties of the paltalk chat room )


Rooms: ['Inner peace ♥']
Windows with class name "DlgGroupChat Window Class": []
Chat window: <pywinauto.application.WindowSpecification object at 0x05386288>
class_name: Qt5150QWindowIcon
friendly_class_name: Qt5150QWindowIcon
texts: ['Inner peace ♥']
control_id: 0
rectangle: (L849, T135, R1915, B856)
is_visible: True
is_enabled: True
control_count: 0
style: -1764818944
exstyle: 256
user_data: 0
context_help_id: 0
fonts: [<LOGFONTW 'Segoe UI' -15>]
client_rects: [<RECT L0, T0, R1050, B682>]
is_unicode: True
menu_items: []
automation_id:
isAskingForMicrophone 0

Paltalk uses the Segoe UI font for paltalk. so if you want to send icons, then look for the right Segoe UI Emoji icons. these are web pages (Segoe UI Emoji)
I will attach 2 Python files, one file can only send characters
and the second file can send Vietnamese with accents and symbols.

paltalk_bot_exampleUI.py
paltalk_bot_exampleNormal.py

VN

unread,
Jun 28, 2023, 12:10:22 PM6/28/23
to Programmers
Fix the code in the file paltalk_bot_exampleUI. I use the code paltalk_bot_exampleUI to check if it works well with rooms with lots of special characters?
and when I connect to room ['*•.¸♡ DREAM KSA ♡¸.•*'] , I get error from the code.
In the newly debugged code, I added the re.escape function which is used to escape special characters in the string room_list[0]. This function returns a new string where all special characters are appended with a backslash (\) before them so that they are no longer treated as special characters.

For example, if room_list[0] is a string containing the character . (period), the character is escaped by adding a backslash before it, like this: re.escape(room_list[0]).

Escaping these special characters is necessary when you use the string in a regular expression, because special characters have a special meaning in the regular expression and need to be escaped to avoid confusion. mixed.
paltalk_bot_exampleUI-FIX.py

kiên nguyến

unread,
Jul 5, 2023, 4:35:50 AM7/5/23
to Programmers
Hiện tại tôi đang dùng  UIAutomation để send msg như phía dưới. nhưng nếu send nhiều paltalk bị đóng đột ngột. ai có thể cho tôi biết ý do không?
public static void SendPalTxt(AutomationElement element, string message)
        {
            ((ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern)).SetValue(message);
            Thread.Sleep(10);
            SendKeys.SendWait("{ENTER}");
            Thread.Sleep(10);
        }

Vào lúc 23:10:22 UTC+7 ngày Thứ Tư, 28 tháng 6, 2023, VN đã viết:

KháchQuaĐường

unread,
Jul 5, 2023, 5:47:19 AM7/5/23
to Programmers
Một cách để cải thiện đoạn code này là bỏ qua việc sử dụng `Thread.Sleep(10)` để tạm dừng chương trình. Thay vào đó, bạn có thể sử dụng một phương thức khác để đảm bảo rằng tin nhắn đã được nhập vào trường nhập liệu trước khi nhấn phím Enter để gửi tin nhắn. Ví dụ, bạn có thể sử dụng phương thức `WaitForInputIdle` của lớp `Process` để đợi cho đến khi ứng dụng Paltalk hoàn thành xử lý các sự kiện nhập liệu hiện tại trước khi gửi tin nhắn.

Đây là một ví dụ về cách sử dụng phương thức `WaitForInputIdle` để cải thiện đoạn code của bạn:
bạn cần thêm một dòng khai báo  using System.Diagnostics;

```csharp
using System.Diagnostics;

public static void SendPalTxt(AutomationElement element, string message)
{
    ((ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern)).SetValue(message);
    Process p = Process.GetProcessesByName("paltalk")[0];
    p.WaitForInputIdle();
    SendKeys.SendWait("{ENTER}");
}
```

Trong ví dụ trên, chúng ta sử dụng phương thức `GetProcessesByName` của lớp `Process` để tìm kiếm quá trình chạy ứng dụng Paltalk, sau đó sử dụng phương thức `WaitForInputIdle` để đợi cho đến khi ứng dụng Paltalk hoàn thành xử lý các sự kiện nhập liệu hiện tại. Sau khi ứng dụng Paltalk hoàn thành xử lý các sự kiện nhập liệu, chúng ta mới gửi tin nhắn bằng cách nhấn phím Enter.

Bạn có thể thử sử dụng cách tiếp cận này để cải thiện tốc độ gửi tin nhắn và giải quyết vấn đề ứng dụng Paltalk bị đóng khi gửi nhiều tin nhắn.

Trong đoạn code mới mà tôi vừa cung cấp, dòng Process p = Process.GetProcessesByName("paltalk")[0]; được sử dụng để tìm kiếm quá trình chạy ứng dụng Paltalk. Trong đó, "paltalk" là tên tiến trình của ứng dụng Paltalk.

Nếu bạn muốn sử dụng đoạn code mới này, bạn cần thay thế "paltalk" bằng tên tiến trình chính xác của ứng dụng Paltalk trên máy tính của bạn. Bạn có thể tìm kiếm tên tiến trình của ứng dụng Paltalk bằng cách mở Trình quản lý tác vụ (Task Manager) và tìm kiếm ứng dụng Paltalk trong danh sách các tiến trình đang chạy.

Sau khi tìm thấy tên tiến trình của ứng dụng Paltalk, bạn có thể thay thế "paltalk" bằng tên tiến trình chính xác trong đoạn code mới. Ví dụ, nếu tên tiến trình của ứng dụng Paltalk là "PaltalkApp", thì bạn có thể sửa đổi nó.


Ps:/ Đoạn code mà tôi vừa cung cấp cho bạn thay đổi một chút so với đoạn code ban đầu của bạn. Thay vì sử dụng Thread.Sleep(10) để tạm dừng chương trình, đoạn code mới sử dụng phương thức WaitForInputIdle của lớp Process để đợi cho đến khi ứng dụng Paltalk hoàn thành xử lý các sự kiện nhập liệu hiện tại trước khi gửi tin nhắn.

Đoạn code mới này có thể hoạt động tốt hơn đoạn code ban đầu của bạn, bởi vì nó không sử dụng Thread.Sleep để tạm dừng chương trình

- Sau này khi bạn gặp vấn đề gì thì nên cung cấp nhiều chi tiết càng tốt, và nếu đoạn code đó cần khai báo hoặc định nghĩa thêm bất kỳ....


Vào lúc 15:35:50 UTC+7 ngày Thứ Tư, 5 tháng 7, 2023, kiên nguyến đã viết:

Chicago David

unread,
Jul 5, 2023, 4:58:56 PM7/5/23
to Programmers
This is what I use to send into the room where I've previous set ChatEditBox to the input UIAutomation element and Roomptr function does a FindWindow to find the handle to the Paltalk window.
                Dim etb As ValuePattern = ChatEditBox.GetCurrentPattern(ValuePattern.Pattern)
                etb.SetValue(Line)
                ChatEditBox.SetFocus() 'make sure focus is in box
                Dim paltalkHandle As Integer = RoomPtr()
                PostMessage(paltalkHandle, WM_KEYDOWN, RETURNKEY, IntPtr.Zero) 'send RETURN key. Doing a sendkeys can send outside the window

I had problems with send keys sometimes sending an Enter outside the Paltalk window and so I switched to PostMessage RETURNKEY on the Paltalk window. I still have occasional problems with sending RETURNKEY because the chateditbox can lose focus and the RETURNKEY goes to some other control. I'm thinking of changing this to staying with setvalue for my unicode text and then finding the edit box rich text control as per other discussion and send the return key to that control using SendMessage. That way I'd know the RETURNKEY is going to the right control.

Chicago David

unread,
Jul 5, 2023, 5:00:38 PM7/5/23
to Programmers
Oh, and looking at this I shouldn't have used integer type for my handle. It should have been an IntPtr. Forgive my bad hobbiest programming. :-)

Chicago David

unread,
Jul 5, 2023, 6:15:10 PM7/5/23
to Programmers
Oh, to answer your question on why Paltalk might be crashing if you send too fast: Paltalk will check on whether you are sending too much text too quickly to the Paltalk client. It has always done this (even in Classic) to prevent room spamming. It is a matter of trial and error to find the right "sleep" to proceed or follow a line of printed text. I don't know what the current penalty is for sending too much too quickly but exiting the Paltalk client could be one possible penalty.

I ran into something similar a couple of years ago with new Paltalk with my code that was pressing the control key many times a second by mistake. CTRL is the default on mic key and Paltalk logged me out with some error msg for doing that.

Paltalk will also either eliminate or truncate text you send into the room if you send too much. For example I find I cannot send a lot of Bold looking Unicode text into the room or Paltalk will not show it to others. So for your testing you will need to use a second computer to see whether what you print in the room can be seen by others.

Message has been deleted

phục hoangphuc

unread,
Jul 5, 2023, 6:39:01 PM7/5/23
to Programmers
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

const int WM_KEYDOWN = 0x0100;
const int VK_RETURN = 0x0D;


public static void SendPalTxt(AutomationElement element, string message)
{
    // Đặt giá trị cho chateditbox
    ((ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern)).SetValue(message);

    // Đảm bảo focus đang ở trong chateditbox
    element.SetFocus();

    // Tạm dừng chương trình trong 500ms
    Thread.Sleep(500);

    // hWnd là handle của control mà bạn muốn gửi phím RETURNKEY đến
    SendMessage(hWnd, WM_KEYDOWN, new IntPtr(VK_RETURN), IntPtr.Zero);
}

// element là AutomationElement đại diện cho chateditbox
SendPalTxt(element, "Xin chào 😊");

Đoạn mã mà tôi đã cung cấp ở trên thực hiện các thao tác sau:
Định nghĩa phương thức SendPalTxt, nhận vào hai tham số là element và message. Tham số element là một AutomationElement đại diện cho chateditbox, tham số message là nội dung tin nhắn mà bạn muốn gửi.
Trong phương thức SendPalTxt, chúng ta sử dụng phương thức SetValue của ValuePattern để đặt giá trị cho chateditbox. Điều này cho phép bạn nhập nội dung tin nhắn vào chateditbox.
Tiếp theo, chúng ta sử dụng phương thức SetFocus để đảm bảo rằng focus đang ở trong chateditbox.
Sau đó, chúng ta sử dụng phương thức Sleep của lớp Thread để tạm dừng chương trình trong một khoảng thời gian nhất định.
Cuối cùng, chúng ta sử dụng phương thức SendMessage để gửi phím RETURNKEY đến một control cụ thể. Điều này cho phép bạn gửi tin nhắn đã nhập vào chateditbox.
Sau khi định nghĩa xong phương thức SendPalTxt, chúng ta gọi phương thức này với tham số message có giá trị là "Xin chào 😊". Điều này sẽ đặt giá trị cho chateditbox là "Xin chào 😊" và sau đó gửi phím RETURNKEY để gửi tin nhắn
Vào lúc 05:15:10 UTC+7 ngày Thứ Năm, 6 tháng 7, 2023, Chicago David đã viết:
Reply all
Reply to author
Forward
0 new messages