3

My telegram bot is a dialog and it needs to keep the questions and the answers (like TriviaBot). What is the best (most efficient) method to do this? A database with the user id as key?

There is a lot of telegram bots, but where are examples with the source code to get ideas?

Charles Okwuagwu
  • 10,538
  • 16
  • 87
  • 157
Martin Weissenboeck
  • 683
  • 3
  • 8
  • 22

3 Answers3

4

Your question is not really related to the telegram bot API. You are essentially asking: I have an application that has to keep an history of user interactions, how to do this (efficient)?

To answer that: you could use a database and insert an entry for each conversation using an unique identifier. Since telegram has a chat_id for each conversation, you could use that. Depending on what you are exactly trying to store, you should choose how to store it. (an entry for each answer, or for each conversation or ...)

If you program in python, you can use the python wrapper called python-telegram-bot to make things easier

Examples are here: https://github.com/leandrotoledo/python-telegram-bot#examples

Sebastian
  • 5,471
  • 5
  • 35
  • 53
1

You can use force_reply and frequently ask whatever you want and store answers in any kind of database. Please refer to its doc : refrence
and a simple answer on : force reply description

Hamed
  • 565
  • 3
  • 17
0

Well, I didn't found any example of force_reply, and had to ask an ai about it, honestly I dont know if it works, I just hope that it helps, here we go...

    function survey(data) {
  var Q1 = {
    'chat_id': data.message.chat.id,
    'text': 'how are you?'
  }
  var method = 'sendMessage';
  var options = {
    'method': 'post',
    'contentType': 'application/json',
    'payload': JSON.stringify(Q1)
  }
  var response = UrlFetchApp.fetch('https://api.telegram.org/bot' + telegramToken + '/' + method, options);
  var text = data.message.text;

  // Start loop
  for (var i = 0; i < 3; i++) {
    if (i == 0) {
      // First iteration
      if (text == "") {
        // Get response from user
        currentstep = '3';
        var dataForceReply = {
          method: "post",
          payload: {
            method: "sendMessage",
            chat_id: String(data.message.chat.id),
            text: "how are you?",
            reply_markup: JSON.stringify({
              "force_reply": true
            })
          }
        };
        UrlFetchApp.fetch(telegramAPIURL + "/", dataForceReply);
      }
    } else if (i == 1) {
      // Second iteration
      if (text != "") {
        // Get response from user
        // Store response in variable
        var response1 = text;
        var dataForceReply2 = {
          method: "post",
          payload: {
            method: "sendMessage",
            chat_id: String(data.message.chat.id),
            text: "What do you think about this?",
            reply_markup: JSON.stringify({
              "force_reply": true
            })
          }
        };
        UrlFetchApp.fetch(telegramAPIURL + "/", dataForceReply2);
      }
    } else if (i == 2) {
      // Third iteration
      if (text != "") {
        // Get response from user
        // Store response in variable
        var response2 = text;
        var dataForceReply3 = {
          method: "post",
          payload: {
            method: "sendMessage",
            chat_id: String(data.message.chat.id),
            text: "Do you have any suggestions?",
            reply_markup: JSON.stringify({
              "force_reply": true
            })
          }
        };
        UrlFetchApp.fetch(telegramAPIURL + "/", dataForceReply3);
      }
    } else {
      // Get response from user
      // Store response in variable
      var response3 = text;
      // Store all responses in variables
      var response1 = response1;
      var response2 = response2;
      var response3 = response3;
    }
  }
}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 02 '23 at 01:54