Answers questions asked in a Telegram chat by searching the web, and replies with the answer and the pages it came from.
Uses
OpenAI
Serper
Telegram
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.
incomingMessage.When a new message is received. Save it as incomingMessage.subjectsToAnswer to blank.Set subjectsToAnswer to blank.sourcesToUse to 3.Set sourcesToUse to 3.minimumQuestionLength to 12.Set minimumQuestionLength to 12.sourcesWanted to sourcesToUse multiplied by 1.Set sourcesWanted to sourcesToUse multiplied by 1.sourcesWanted is less than 1If sourcesWanted is less than 1sourcesWanted is greater than 5If sourcesWanted is greater than 5chatId to incomingMessage.chat_id.Set chatId to incomingMessage.chat_id.chatId is emptyIf chatId is emptyquestion to blank.Set question to blank.messageErrorIf a step below fails, save the error as messageErrorincomingMessage.text and save it as question.Trim incomingMessage.text and save it as question.question starts with Here is what I foundIf question starts with Here is what I foundquestionFloor to minimumQuestionLength multiplied by 1.Set questionFloor to minimumQuestionLength multiplied by 1.question is less than questionFloorIf the length of question is less than questionFloor 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.searchResults to searchResult.results.Set searchResults to searchResult.results.citationLines.Start an empty list and save it as citationLines.sourceBlocks.Start an empty list and save it as sourceBlocks.sourceNumber to 0.Set sourceNumber to 0.row in searchResults:For each row in searchResults:sourceNumber is greater than or equal to sourcesWantedIf sourceNumber is greater than or equal to sourcesWantedresultLink to blank.Set resultLink to blank.resultTitle to blank.Set resultTitle to blank.resultSnippet to blank.Set resultSnippet to blank.linkErrorIf a step below fails, save the error as linkErrorrow.link and save it as resultLink.Trim row.link and save it as resultLink.resultLink is emptyIf resultLink is emptytitleErrorIf a step below fails, save the error as titleErrorrow.title and save it as resultTitle.Trim row.title and save it as resultTitle.resultTitle to blank.Change resultTitle to blank.resultTitle is emptyIf resultTitle is emptyresultTitle to Untitled page.Change resultTitle to Untitled page.snippetErrorIf a step below fails, save the error as snippetErrorrow.snippet and save it as resultSnippet.Trim row.snippet and save it as resultSnippet.resultSnippet to blank.Change resultSnippet to blank.sourceNumber.Add 1 to sourceNumber.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.sourceNumber] resultTitleAdd [sourceNumber] resultTitle
resultLink
resultSnippet truncated to 700 characters using ... as the ellipsis to the end of sourceBlocks and save it as sourceBlocks.citationLines is emptyIf citationLines is emptychatId.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.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
.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.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.promptSources to Sources:Set promptSources to Sources:
sourceBlocks joined by a blank line
.answerPrompt to promptRole@promptRules@promptRulesTwo@promptSourcesQuestion:Set answerPrompt to promptRole@promptRules@promptRulesTwo@promptSourcesQuestion:
question
.answerPrompt. Save it as modelResult.Ask OpenAI: answerPrompt. Save it as modelResult.answerText to blank.Set answerText to blank.answerErrorIf a step below fails, save the error as answerErrormodelResult.response_text and save it as answerText.Trim modelResult.response_text and save it as answerText.answerText to blank.Change answerText to blank.answerText is emptyIf answerText is emptychatId.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 linereplyHeader to Here is what I found.Set replyHeader to Here is what I found.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.replySources to Set replySources to
Sources:
citationLines joined by a blank line.replyText to replyHeader plus replyBody plus replySources.Set replyText to replyHeader plus replyBody plus replySources.chatId.Send a message to chat chatId.replyText