def view_poll(poll_id): fragment = Fragment.objects.get_or_404(id=poll_id) if fragment.story.bulletin not in g.current_account.bulletins: abort(401) poll_questions = PollQuestions.objects(fragment=fragment) total = float(sum([len(i.users) for i in poll_questions])) def get_persent(n, total): if total > 0: return n / total * 100 return 0 data = [{ 'answer': q.text, 'votes': len(q.users), 'persent': get_persent(len(q.users), total) } for q in poll_questions] # add total stats data.append({'answer': 'All answers', 'votes': int(total), 'persent': 100}) fragment.text = Emoji.shortcode_to_unicode(fragment.text) return render_template('polls/questions.html', fragment=fragment, data=data)
def send_text_fragment(self, chat_id, f, show_keyboard=None): self.typing(chat_id, f.text) self.bot.sendMessage( chat_id, Emoji.shortcode_to_unicode(f.text), disable_web_page_preview=True, reply_markup=show_keyboard )
def send_poll_fragment(self, chat_id, f): self.typing(chat_id, f.text) self.bot.sendMessagePostback( chat_id, Emoji.shortcode_to_unicode(f.text), buttons=self.get_poll_keyboard(f) )
def send_text_fragment(self, chat_id, f, show_keyboard=None): self.typing(chat_id, f.text) self.bot.sendMessage( chat_id, Emoji.shortcode_to_unicode(f.text), buttons=show_keyboard )
def get_answers_keyboard(self, answers): show_keyboard = { 'keyboard': [ [Emoji.shortcode_to_unicode(f.text)] for f in answers ], 'one_time_keyboard': True, 'resize_keyboard': True } return show_keyboard
def send_poll_fragment(self, chat_id, f): self.typing(chat_id, f.text) self.bot.sendMessage( chat_id, Emoji.shortcode_to_unicode(f.text), disable_web_page_preview=True, reply_markup=self.get_poll_keyboard(f) )
def send_fragment(chat_id, fragment, reply_markup): print fragment.type, fragment.text, fragment.url if fragment.type == fragment.TYPE_PARAGRAPH: time.sleep(TYPING_TIME) bot.sendMessage(chat_id, Emoji.shortcode_to_unicode(fragment.text), reply_markup=reply_markup, disable_web_page_preview=True) else: bot.sendPhoto(chat_id, ('image.jpg', urlopen(fragment.url)), reply_markup=reply_markup)
def send_lead_answers(self, chat_id, lead, fragments): show_keyboard = { 'keyboard': [ [Emoji.shortcode_to_unicode(f.text)] for f in fragments ], 'one_time_keyboard': True, 'resize_keyboard': True } self.typing(chat_id, lead) self.bot.sendMessage(chat_id, lead, reply_markup=show_keyboard, disable_web_page_preview=True)
def start_story(bulletin, chat_user, chat_id): print 'NEW STORY' story = Story.objects( bulletin=bulletin).order_by('order')[chat_user.current_story] answer = Fragment.objects.filter( story=story, type__ne=Fragment.TYPE_IMAGE).order_by('order')[0] show_keyboard = { 'keyboard': [[Emoji.shortcode_to_unicode(answer.text), 'Next']], 'one_time_keyboard': True, 'resize_keyboard': True } bot.sendChatAction(chat_id, "typing") time.sleep(TYPING_TIME) bot.sendMessage(chat_id, story.lead, reply_markup=show_keyboard, disable_web_page_preview=True) chat_user.current_fragment += 1
def polls(): poll_fragments = [] for bulletin in g.current_account.bulletins: for story in bulletin.content: for fragment in story.content: if fragment.type == Fragment.TYPE_POLL: fragment.text = Emoji.shortcode_to_unicode(fragment.text) poll_fragments.append(fragment) polls = PollQuestions.objects(fragment__in=poll_fragments) for f in poll_fragments: for p in polls: if f == p.fragment: if not hasattr(f, 'answers'): f.answers = 0 f.answers += len(p.users) return render_template('polls/index.html', poll_fragments=poll_fragments)
def get_poll_keyboard(self, fragment, question_id=None): def get_callback_data(fragment, question): if isinstance(fragment, Document): fragment = fragment.id if isinstance(question, Document): question = question.id return '{fragment},{question}'.format(fragment=fragment, question=question) buttons = [] for q in PollQuestions.objects(fragment=fragment): text = q.text if question_id and str(q.id) == question_id: text = q.text + Emoji.shortcode_to_unicode(' :white_check_mark:') buttons.append( [InlineKeyboardButton(text=text, callback_data=get_callback_data(fragment, q))] ) return InlineKeyboardMarkup(inline_keyboard=buttons)
def send_text_fragment(self, chat_id, f): self.bot.sendMessage(chat_id, Emoji.shortcode_to_unicode(f.text), disable_web_page_preview=True)
def on_chat_message(msg): print msg content_type, chat_type, chat_id = telepot.glance(msg) # if chatuser is not registered try: chat_user = ChatUser.objects.get(chat_id=chat_id) except ChatUser.DoesNotExist: # save user id and set status # HERE GOES THE ONBOARDING on_boarding(chat_id) chat_user = ChatUser(chat_id=chat_id, name=get_user_from_msg(msg)) chat_user.save() mp.people_set(chat_id, {'$username': get_user_from_msg(msg)}) mp.track(chat_id, 'registered') return # check if chat is reading a bullettin if chat_user.current_bulletin is None: # find any unread bulletin unread_bulletins = get_unread_bulletins(chat_user) # check if there are bulletins! if len(unread_bulletins) == 0: bot.sendChatAction(chat_id, "typing") time.sleep(TYPING_TIME) bot.sendMessage(chat_id, 'No more content for today!') return bulletin = unread_bulletins[0] chat_user.current_bulletin = bulletin chat_user.current_story = 0 chat_user.current_fragment = 0 # if first fragment of the first story is an answer put it a keyboard start_story(bulletin, chat_user, chat_id) chat_user.save() mp.track(chat_id, 'read_bulletin') mp.track(chat_id, 'read_story') mp.track(chat_id, 'read_fragment') return # load current bulletin else: bulletin = Bulletin.objects.get(id=chat_user.current_bulletin.id) # next should be working in spannish or other languages if "Next" in msg['text']: # check if there are more stories print(chat_user.current_bulletin.title, chat_user.current_story, chat_user.current_fragment) if len(bulletin.content) > chat_user.current_story + 1: chat_user.current_story += 1 chat_user.current_fragment = 0 # this code is repeated start_story(bulletin, chat_user, chat_id) chat_user.save() mp.track(chat_id, 'read_story') mp.track(chat_id, 'read_fragment') return else: bcu = BulletinChatUser(bulletin=chat_user.current_bulletin, chat_user=chat_user) bcu.save() unread_bulletins = get_unread_bulletins(chat_user) if len(unread_bulletins) > 0: chat_user.current_bulletin = unread_bulletins[0] chat_user.current_story = 0 chat_user.current_fragment = 0 start_story(bulletin, chat_user, chat_id) chat_user.save() mp.track(chat_id, 'read_bulletin') mp.track(chat_id, 'read_story') mp.track(chat_id, 'read_fragment') return else: chat_user.current_bulletin = None bot.sendMessage(chat_id, 'No more news for today') chat_user.save() return is_answer = False while not is_answer: story = Story.objects(bulletin=chat_user.current_bulletin).order_by( 'order')[chat_user.current_story] story_fragments = Fragment.objects(story=story).order_by('order') fragment = story_fragments[chat_user.current_fragment] bot.sendChatAction(chat_id, "typing") # while there is content in this story if len(story.content) > chat_user.current_fragment + 1: f_next = story_fragments[chat_user.current_fragment + 1] if f_next.type == fragment.TYPE_ANSWER: # send answer is_answer = True answer = f_next show_keyboard = { 'keyboard': [[Emoji.shortcode_to_unicode(answer.text), 'Next']], 'one_time_keyboard': True, 'resize_keyboard': True } send_fragment(chat_id, fragment, show_keyboard) chat_user.current_fragment += 2 mp.track(chat_id, 'read_fragment') else: send_fragment(chat_id, fragment, None) chat_user.current_fragment += 1 mp.track(chat_id, 'read_fragment') else: # check if there are more histories of this bulletin: if len(bulletin.content) > chat_user.current_story + 1: chat_user.current_story += 1 chat_user.current_fragment = 0 start_story(bulletin, chat_user, chat_id) mp.track(chat_id, 'read_story') mp.track(chat_id, 'read_fragment') return # look for unread bulletins else: print 'NO MORE CONTENT' bcu = BulletinChatUser(bulletin=chat_user.current_bulletin, chat_user=chat_user) bcu.save() chat_user.current_bulletin = None chat_user.current_story = None chat_user.current_fragment = None bot.sendMessage(chat_id, 'No more news for today') chat_user.save() # find more content NOT IN READ unread_bulletins = get_unread_bulletins(chat_user) if len(unread_bulletins) > 0: chat_user.current_bulletin = unread_bulletins[0] chat_user.current_story = 0 chat_user.current_fragment = 0 chat_user.save() start_story(bulletin, chat_user, chat_id) return else: chat_user.current_bulletin = None bot.sendMessage("No more bulletins to show! wait a couple " "of hours") chat_user.save() return chat_user.save()
def get_answers_keyboard(self, answers): return [Emoji.shortcode_to_unicode(f.text) for f in answers]
def send_lead_answers(self, chat_id, lead, fragments): buttons = [Emoji.shortcode_to_unicode(f.text) for f in fragments] self.typing(chat_id, lead) self.bot.sendMessage(chat_id, lead, buttons=buttons)