對於我的研究,我想用電報機器人在特定時刻開火,每天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名志願者發射一次調查問卷。
如果您發佈到目前爲止,你已經嘗試過的代碼,它具有特定的問題你可能會得到更好的結果。您可能想要查看這篇文章:https://stackoverflow.com/help/how-to-ask – Aron
嗨,阿倫,我添加了我的改編版本的quiz.py示例。 –