This repository has been archived by the owner on Apr 12, 2021. It is now read-only.
forked from nickoala/telepot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
177 additions
and
248 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,26 @@ | ||
import sys | ||
import telepot | ||
from telepot.delegate import create_run | ||
from telepot.delegate import per_chat_id, create_open | ||
|
||
""" | ||
$ python2.7 count.py <token> | ||
$ python2.7 counter.py <token> | ||
Count number of messages. Start over if silent for 10 seconds. | ||
""" | ||
|
||
# Being a subclass of ChatHandler is very useful, for it gives you: | ||
# listener, sender, etc - a lot of facilities. | ||
class MessageCounter(telepot.helper.ChatHandler): | ||
def __init__(self, seed_tuple): | ||
super(MessageCounter, self).__init__(*seed_tuple) | ||
self.listener.timeout = 10 | ||
# conversation ends if no more messages after 10 seconds | ||
def __init__(self, seed_tuple, timeout): | ||
super(MessageCounter, self).__init__(seed_tuple, timeout) | ||
self._count = 0 | ||
|
||
def run(self): | ||
count = 1 | ||
self.sender.sendMessage(count) | ||
# `sender` lets you send messages without mentioning chat_id every time | ||
|
||
while 1: | ||
# wait for user's next message | ||
msg = self.listener.wait(chat__id=self.chat_id) | ||
count += 1 | ||
self.sender.sendMessage(count) | ||
def on_message(self, msg): | ||
self._count += 1 | ||
self.sender.sendMessage(self._count) | ||
|
||
|
||
TOKEN = sys.argv[1] # get token from command-line | ||
|
||
bot = telepot.DelegatorBot(TOKEN, [ | ||
# For each chat id, create a MessageCounter and delegate to its `run()` method | ||
(lambda msg: msg['chat']['id'], create_run(MessageCounter)), | ||
(per_chat_id(), create_open(MessageCounter, timeout=10)), | ||
]) | ||
bot.notifyOnMessage(run_forever=True) |
Oops, something went wrong.