2017-08-05 148 views
1

对于我的研究,我想用电报机器人在特定时刻开火,每天4个简单的选择题给35名志愿者的个人智能手机在我的实验问卷。我已经检查了telepot文档和示例,但我无法构建一个很好的解决方案。测验的例子接近了,但问题和答案应该可见我的志愿者,并写在一个简单的日志文件进行进一步分析。如何使电报BOT和telepot在Python

这是我quiz.py的修改版本

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 <token> 
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<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<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) 

我想给我的志愿者世界上所有的时间来回答的问题,但我不知道如何获得该事件的骑超时的。

第二个问题:如何启动一个定时器问卷序列?我想每天在特定时刻向35名志愿者发射一次调查问卷。

+1

如果您发布到目前为止,你已经尝试过的代码,它具有特定的问题你可能会得到更好的结果。您可能想要查看这篇文章:https://stackoverflow.com/help/how-to-ask – Aron

+0

嗨,阿伦,我添加了我的改编版本的quiz.py示例。 –

回答