seratch / ChatGPT-in-Slack

Swift demonstration of how to build a Slack app that enables end-users to interact with a ChatGPT bot
MIT License
441 stars 168 forks source link

Unescape Slack control characters before sending to OpenAI #5

Closed c-renton closed 1 year ago

c-renton commented 1 year ago

Slack uses &, < and > as control characters, which are replaced with their HTML equivalents (see https://api.slack.com/reference/surfaces/formatting#escaping). This can problems when asking ChatGPT about code, even when these characters appear within code blocks, e.g., Prompt: Is anything wrong with the angle brackets in the following C code?

#include <stdio.h>
int main(int argc, char *argv[])
{
    printf("Hello, world!\n");
    return 0;
}

Example response without this PR: Yes, there is something wrong with the angle brackets in the code. C code should use the normal angle brackets "<" and ">" for including header files. In the code you shared, "<" and ">" entities have been used instead of the normal angle brackets. This is likely due to HTML encoding, which means that the code has been copied from an HTML document where the angle brackets were replaced with those entities to prevent them from being interpreted as HTML tags. To fix it, replace "<" with "<" and ">" with ">" in the #include line.

Example response with this PR: No, there is nothing wrong with the usage of angle brackets in the code. The angle brackets are used to include standard libraries in C, in this case, stdio.h.

seratch commented 1 year ago

Thanks, the change looks good to me at a glance! I am away from keyboard right now (meaning I am writing this on mobile), so I can verify it works on my end within a few hours.