title: how to make a questionnaire with a telegram bot and telepot in python
body: <p>For my research I want to use a telegram bot to fire each day at a specific moment 4 simple multiple choice questions to the personal smartphones of the 35 volunteers in my experiment. I have checke the telepot documentation and examples, but I can't construct a nice solution. The quiz example comes close, but questions and answer should be visible for my volunteers and write in a simple log file for further analyse.</p>
  
  <p>This is my modified version of quiz.py</p>
  
  <pre><code>import sys
  import time
  import random
  import telepot
  import telepot.helper
  from telepot.loop import MessageLoop
  from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
  from telepot.delegate import (
      per_chat_id, per_callback_query_origin, create_open, pave_event_space)
  
  """
  $ python3.5 qst.py &lt;token&gt;
  Send a chat message to the bot. It will give you 4 questions.
  It handles callback query by their origins. All callback query originated from
  the same chat message will be handled by the same `CallbackQueryOriginHandler`.
  Timeout on questions is not needed. How to remove them!
  """
  
  nameLogFile = 'qst_log.txt';
  
  class QstStarter(telepot.helper.ChatHandler):
      def __init__(self, *args, **kwargs):
          super(QstStarter, self).__init__(*args, **kwargs)
  
      def on_chat_message(self, msg):
          content_type, chat_type, chat_id = telepot.glance(msg)
          self.sender.sendMessage(
              'Are you ready for the first question?',
              reply_markup=InlineKeyboardMarkup(
                  inline_keyboard=[[
                       InlineKeyboardButton(text='START', callback_data='start'),
                  ]]
             )
          )
          self.close()  # let Qster take over
  
  class Qster(telepot.helper.CallbackQueryOriginHandler):
  
      def __init__(self, *args, **kwargs):
          super(Qster, self).__init__(*args, **kwargs)
          self._cnt = 0;
  
      def _show_next_question(self):
          qst = ["Question 1", "Question 2", "Question 3", "Question 4"];
          choices = ["a","b","c","d","e"];
  
          if self._cnt&lt;4 :
               self.editor.editMessageText(qst[self._cnt],
                reply_markup=InlineKeyboardMarkup(
                  inline_keyboard=[
                     list(map(lambda c: InlineKeyboardButton(text=str(c), callback_data=str(c)), choices))
                  ]
                )
              )
  
      def on_callback_query(self, msg):
          query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query')
  
          if query_data != 'start':
               # log this answer: Question is this tread safe!
               self._f = open(nameLogFile, 'a+');
               self._f.write(str(from_id) + ',' + str(msg["message"]["edit_date"]) + ',' + \
               repr(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(msg["message"]["edit_date"]))) + ',' + \
               str(self._cnt) + ',' + repr(msg["message"]["text"]) + ',' + repr(query_data) + '\n');
               self._f.close();
               # show this answer
               bot.sendMessage(from_id, msg["message"]["text"] + " " + query_data, parse_mode='HTML');
  
               self._cnt += 1
  
          if self._cnt&lt;4 :
              self._show_next_question()
          else :
              self.editor.editMessageText('\nThanks', reply_markup=None);
  
  
      def on__idle(self, event):
               #self.close()
  
  
  TOKEN = sys.argv[1]
  
  bot = telepot.DelegatorBot(TOKEN, [
      pave_event_space()(
          per_chat_id(), create_open, QstStarter, timeout=3),
      pave_event_space()(
          per_callback_query_origin(), create_open, Qster, timeout=10),
  ])
  
  MessageLoop(bot).run_as_thread()
  print('Listening ...')
  
  while 1:
     time.sleep(10)
  </code></pre>
  
  <p>I want to give my volunteers all the time of the world to answer the questions, but I don't know how to get ride of the event timeout's.</p>
  
  <p>Second question: how to start the questionnaire sequence with a timer? I want to fire once a day at a specific moment the questionnaire to 35 volunteers.</p>