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
11 changed files
with
87 additions
and
24 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import sys | ||
import telepot | ||
from telepot.delegate import create_run | ||
|
||
""" | ||
$ python2.7 count.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 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) | ||
|
||
|
||
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)), | ||
]) | ||
bot.notifyOnMessage(run_forever=True) |
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
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
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import sys | ||
import time | ||
import telepot | ||
|
||
""" | ||
$ python3.2 skeleton_extend.py <token> | ||
A skeleton for your telepot programs - extend from `Bot` and define a `handle` method. | ||
""" | ||
|
||
class YourBot(telepot.Bot): | ||
def handle(self, msg): | ||
content_type, chat_type, chat_id = telepot.glance2(msg) | ||
print(content_type, chat_type, chat_id) | ||
# Do your stuff according to `content_type` ... | ||
|
||
|
||
TOKEN = sys.argv[1] # get token from command-line | ||
|
||
bot = YourBot(TOKEN) | ||
bot.notifyOnMessage() | ||
print('Listening ...') | ||
|
||
# Keep the program running. | ||
while 1: | ||
time.sleep(10) |
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