Answers questions asked in a Telegram chat by searching the web, and replies with the answer and the pages it came from.

UsesOpenAISerperTelegram

Answer Telegram questions from the web, with sources

Whenever someone asks a question in your Telegram chat, the web is searched for it, the top results are read, and a written answer comes back in the same chat with the pages it came from listed underneath. Messages too short to be a question are ignored, and so are the workflow's own replies, so it never searches for one of its own answers.

The sources under every answer are written by the workflow from what the search actually returned, never by the AI, so a link in a reply is always a page the search found. The AI is told to cite by number, to say plainly when the sources do not answer the question, and to decline questions outside the subjects you have given it. If the search returns nothing citable, the chat is told so and no answer is written at all. Long answers are shortened so the reply and its sources fit in one Telegram message.

You need a Telegram bot in the chat, and a short description of what this assistant is for.

TelegramTelegramWhen a new message is received. Save it as incomingMessage.When a new message is received. Save it as incomingMessage.
Set subjectsToAnswer to blank.Set subjectsToAnswer to blank.
Set sourcesToUse to “3”.Set sourcesToUse to “3”.
Set minimumQuestionLength to “12”.Set minimumQuestionLength to “12”.
Set sourcesWanted to sourcesToUse multiplied by 1.Set sourcesWanted to sourcesToUse multiplied by 1.
If sourcesWanted is less than 1If sourcesWanted is less than 1
Yes
Stop and report a problem: Set the 'sourcesToUse' step to how many search results to read - a whole number from 1 to 5.Stop and report a problem: Set the 'sourcesToUse' step to how many search results to read - a whole number from 1 to 5.
No
No steps
If sourcesWanted is greater than 5If sourcesWanted is greater than 5
Yes
Stop and report a problem: Set the 'sourcesToUse' step to 5 or fewer, so the reply fits inside one Telegram message.Stop and report a problem: Set the 'sourcesToUse' step to 5 or fewer, so the reply fits inside one Telegram message.
No
No steps
Set chatId to incomingMessage.chat_id.Set chatId to incomingMessage.chat_id.
If chatId is emptyIf chatId is empty
Yes
Stop and report a problem: That message did not say which chat it came from, so there was nowhere to reply.Stop and report a problem: That message did not say which chat it came from, so there was nowhere to reply.
No
No steps
Set question to blank.Set question to blank.
If a step below fails, save the error as messageErrorIf a step below fails, save the error as messageError
Try
Trim incomingMessage.text and save it as question.Trim incomingMessage.text and save it as question.
On error
Finish the run: That message had no text in it, so there was no question to answer.Finish the run: That message had no text in it, so there was no question to answer.
If question starts with “Here is what I found”If question starts with “Here is what I found”
Yes
Finish the run: That message was an answer this workflow already sent, so it was left alone.Finish the run: That message was an answer this workflow already sent, so it was left alone.
No
No steps
Set questionFloor to minimumQuestionLength multiplied by 1.Set questionFloor to minimumQuestionLength multiplied by 1.
If the length of question is less than questionFloorIf the length of question is less than questionFloor
Yes
Finish the run: That message was too short to be a question worth searching for.Finish the run: That message was too short to be a question worth searching for.
No
No steps
SerperSerperSearch Google for '@question truncated to 300 characters using “” as the ellipsis (keeping words whole)'. Save it as searchResult.Search Google for '@question truncated to 300 characters using “” as the ellipsis (keeping words whole)'. Save it as searchResult.
Set searchResults to searchResult.results.Set searchResults to searchResult.results.
Start an empty list and save it as citationLines.Start an empty list and save it as citationLines.
Start an empty list and save it as sourceBlocks.Start an empty list and save it as sourceBlocks.
Set sourceNumber to 0.Set sourceNumber to 0.
For each row in searchResults:For each row in searchResults:
If sourceNumber is greater than or equal to sourcesWantedIf sourceNumber is greater than or equal to sourcesWanted
Yes
Stop repeating.Stop repeating.
No
No steps
Set resultLink to blank.Set resultLink to blank.
Set resultTitle to blank.Set resultTitle to blank.
Set resultSnippet to blank.Set resultSnippet to blank.
If a step below fails, save the error as linkErrorIf a step below fails, save the error as linkError
Try
Trim row.link and save it as resultLink.Trim row.link and save it as resultLink.
On error
Skip to the next item.Skip to the next item.
If resultLink is emptyIf resultLink is empty
Yes
Skip to the next item.Skip to the next item.
No
No steps
If a step below fails, save the error as titleErrorIf a step below fails, save the error as titleError
Try
Trim row.title and save it as resultTitle.Trim row.title and save it as resultTitle.
On error
Change resultTitle to blank.Change resultTitle to blank.
If resultTitle is emptyIf resultTitle is empty
Yes
Change resultTitle to “Untitled page”.Change resultTitle to “Untitled page”.
No
No steps
If a step below fails, save the error as snippetErrorIf a step below fails, save the error as snippetError
Try
Trim row.snippet and save it as resultSnippet.Trim row.snippet and save it as resultSnippet.
On error
Change resultSnippet to blank.Change resultSnippet to blank.
Add 1 to sourceNumber.Add 1 to sourceNumber.
Add [sourceNumber] resultTitle truncated to 110 characters using “...” as the ellipsisAdd [sourceNumber] resultTitle truncated to 110 characters using “...” as the ellipsis resultLink truncated to 200 characters using “” as the ellipsis to the end of citationLines and save it as citationLines.
Add [sourceNumber] resultTitleAdd [sourceNumber] resultTitle resultLink resultSnippet truncated to 700 characters using “...” as the ellipsis to the end of sourceBlocks and save it as sourceBlocks.
If citationLines is emptyIf citationLines is empty
Yes
TelegramTelegramSend a message to chat chatId.Send a message to chat chatId.I searched the web for that but did not get back any result I could cite, so I have not written an answer. Try asking it a different way.
Finish the run: The search returned nothing citable, so no answer was written.Finish the run: The search returned nothing citable, so no answer was written.
No
No steps
Set promptRole to You are a research assistant answering a question someone asked in a Telegram chat.Set promptRole to You are a research assistant answering a question someone asked in a Telegram chat. This assistant answers questions about: subjectsToAnswer .
Set promptRules to Rules you must follow:Set promptRules to Rules you must follow: - Answer only from the numbered sources below. Do not use anything else you know. - Never invent a source, a web address, a statistic or a quotation. - Cite by number in square brackets, like [1], after each claim it supports. - Do not write out any web address. The addresses are added below your answer for you.
Set promptRulesTwo to - If the sources do not answer the question, say plainly that they do not, and say what they do cover.Set promptRulesTwo to - If the sources do not answer the question, say plainly that they do not, and say what they do cover. - If the question is outside the subjects above, say so instead of answering it. - Plain text only, no formatting marks, under 200 words.
Set promptSources to Sources:Set promptSources to Sources: sourceBlocks joined by a blank line .
Set answerPrompt to promptRole@promptRules@promptRulesTwo@promptSourcesQuestion:Set answerPrompt to promptRole@promptRules@promptRulesTwo@promptSourcesQuestion: question .
OpenAIOpenAIAsk OpenAI: answerPrompt. Save it as modelResult.Ask OpenAI: answerPrompt. Save it as modelResult.
Set answerText to blank.Set answerText to blank.
If a step below fails, save the error as answerErrorIf a step below fails, save the error as answerError
Try
Trim modelResult.response_text and save it as answerText.Trim modelResult.response_text and save it as answerText.
On error
Change answerText to blank.Change answerText to blank.
If answerText is emptyIf answerText is empty
Yes
TelegramTelegramSend a message to chat chatId.Send a message to chat chatId.I found sources for that but could not write an answer from them. Here is what the search returned, so you can read it yourself: citationLines joined by a blank line
Finish the run: The model returned no answer, so the sources were sent on their own.Finish the run: The model returned no answer, so the sources were sent on their own.
No
No steps
Set replyHeader to Here is what I found.Set replyHeader to Here is what I found.
Shorten answerText to 2400 characters (keeping words whole) and save it as replyBody.Shorten answerText to 2400 characters (keeping words whole) and save it as replyBody.
Set replySources to Set replySources to Sources: citationLines joined by a blank line.
Set replyText to replyHeader plus replyBody plus replySources.Set replyText to replyHeader plus replyBody plus replySources.
TelegramTelegramSend a message to chat chatId.Send a message to chat chatId.replyText
Finish the run: The answer was sent, with the pages it was written from listed under it.Finish the run: The answer was sent, with the pages it was written from listed under it.