Created
August 7, 2017 07:58
-
-
Save toracle/e6750f201d9cad3ca79fc49aca479a42 to your computer and use it in GitHub Desktop.
bothub-tutorial-wordbook-3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from bothub_client.bot import BaseBot | |
| from bothub_client.messages import Message | |
| class Bot(BaseBot): | |
| def handle_message(self, event, context): | |
| content = event['content'] | |
| if content.startswith('/'): | |
| if content.startswith('/list'): | |
| self.send_word_list(event) | |
| def send_word_list(self, event): | |
| content = event['content'] | |
| splitted = content.split() | |
| words_to_count = self.get_user_data() | |
| sorted_list = sorted(words_to_count.items(), key=lambda d: d[1], reverse=True) | |
| start_pos = int(splitted[1]) if len(splitted) > 1 else 0 | |
| paged_list = sorted_list[start_pos:start_pos+10] | |
| word_list = '\n'.join([ | |
| '{}, {}'.format(word, count) | |
| for word, count | |
| in paged_list | |
| ]) | |
| has_next = len(paged_list) == 10 | |
| message = Message(event).set_text(word_list) | |
| if has_next: | |
| message.add_postback_button('Next list', '/list {}'.format(start_pos+10)) | |
| self.send_message(message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment