n3d1117 / chatgpt-telegram-bot

🤖 A Telegram bot that integrates with OpenAI's official ChatGPT APIs to provide answers, written in Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add typing effect

deanxizian opened this issue · comments

Is it possible to add a parameter stream=true to achieve stream outputs like typing effect?

Definitely. I don't have much time, but I added to the README in case someone wants to give it a try

@deanxizian i've never seen a telegram message come in as a live stream. I'm old enough to remember unix ytalk .. it gives a "full duplex" capability to chats and interesting dynamics. This is not yet picked up by modern chat clients which are all half duplex. Standby until someone "invents" this again.

@k3it there's a trick I had implemented in the old version of this repo to kind of achieve the typing effect @deanxizian was talking about:

  • Read chatgpt response stream line by line
  • Send initial telegram message as soon as the first line is received
  • Keep editing the same message with the updated content as it comes

The tricky part is to make sure to balance the amount of edit requests to avoid exceeding errors.

Not a big fan of this approach but it worked last time i tried. Some pseudocode:

id = None
for (index, line) in stream_response:
    if index % N == 0:
        if id is None:
             id = (await send_message(text=line)).id
        else:
             await edit_message(id=id, text=line)

with e.g. N=10 meaning we update the message every 10 lines received (you could also make it time based e.g. every 0.5s)

@n3d1117 very clever use of the editing feature! not sure if it was intended for this purpose but i guess it could work.