1

对于我的研究,我想使用电报机器人在每天的特定时刻向我实验中的 35 名志愿者的个人智能手机发送 4 个简单的多项选择题。我已经检查了 Telepot 文档和示例,但我无法构建一个好的解决方案。测验示例很接近,但问题和答案应该对我的志愿者可见,并写入一个简单的日志文件以供进一步分析。

这是我修改后的 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 名志愿者发放一次问卷。

4

1 回答 1

1

丹尼森。您可以通过调度程序ADScheduler或许多其他人调用任何函数。

还有更简单的方法使用了一个python-telegram-bot模块,其中包括 ConversationHandler方法。

https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/conversationbot.py

因此,您只需entry_point通过调度程序运行功能并获得快乐。

Othervice 也telepot支持这个动作,但是更硬更脏。不是吗?

于 2017-09-12T01:30:46.330 回答