Skip to content
This repository has been archived by the owner on Apr 12, 2021. It is now read-only.

Commit

Permalink
Updated to use latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
nickoala committed Oct 14, 2015
1 parent 5d300fe commit c7fa78c
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 24 deletions.
4 changes: 2 additions & 2 deletions examples/chatbox_nodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def _read_messages(self, messages):
self.sender.sendMessage(msg['text'])

def _handle(self, msg):
msg_type, from_id, chat_id = telepot.glance(msg)
content_type, chat_type, chat_id = telepot.glance2(msg)

if msg_type != 'text':
if content_type != 'text':
self.sender.sendMessage("I don't understand")
return

Expand Down
4 changes: 2 additions & 2 deletions examples/chatboxa_nodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def _read_messages(self, messages):

@asyncio.coroutine
def _handle(self, msg):
msg_type, from_id, chat_id = telepot.glance(msg)
content_type, chat_type, chat_id = telepot.glance2(msg)

if msg_type != 'text':
if content_type != 'text':
yield from self.sender.sendMessage("I don't understand")
return

Expand Down
37 changes: 37 additions & 0 deletions examples/counter.py
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)
2 changes: 1 addition & 1 deletion examples/diceyclock.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"""

def handle(msg):
chat_id = msg['from']['id']
chat_id = msg['chat']['id']
command = msg['text']

print 'Got command: %s' % command
Expand Down
8 changes: 4 additions & 4 deletions examples/emodi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
"""

def handle(msg):
msg_type, from_id, chat_id = telepot.glance(msg)
content_type, chat_type, chat_id = telepot.glance2(msg)
m = telepot.namedtuple(msg, 'Message')

if chat_id < 0:
# group message
print 'Received a %s from %s, by %s' % (msg_type, m.chat, m.from_)
print 'Received a %s from %s, by %s' % (content_type, m.chat, m.from_)
else:
# private message
print 'Received a %s from %s' % (msg_type, m.chat) # m.chat == m.from_
print 'Received a %s from %s' % (content_type, m.chat) # m.chat == m.from_

if msg_type == 'text':
if content_type == 'text':
reply = ''

# For long messages, only return the first 10 characters.
Expand Down
8 changes: 4 additions & 4 deletions examples/emodia.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@

@asyncio.coroutine
def handle(msg):
msg_type, from_id, chat_id = telepot.glance(msg)
content_type, chat_type, chat_id = telepot.glance2(msg)
m = telepot.namedtuple(msg, 'Message')

if chat_id < 0:
# group message
logger.info('Received a %s from %s, by %s' % (msg_type, m.chat, m.from_))
logger.info('Received a %s from %s, by %s' % (content_type, m.chat, m.from_))
else:
# private message
logger.info('Received a %s from %s' % (msg_type, m.chat)) # m.chat == m.from_
logger.info('Received a %s from %s' % (content_type, m.chat)) # m.chat == m.from_

if msg_type == 'text':
if content_type == 'text':
if msg['text'] == '/start':
yield from bot.sendMessage(chat_id, # Welcome message
"You send me an Emoji"
Expand Down
6 changes: 3 additions & 3 deletions examples/guess.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def run(self):
# wait for user's guess
msg = self.listener.wait(chat__id=self.chat_id)

msg_type, from_id, chat_id = telepot.glance(msg)
content_type, chat_type, chat_id = telepot.glance2(msg)

if msg_type != 'text':
if content_type != 'text':
self.sender.sendMessage('Give me a number, please.')
continue

Expand All @@ -65,7 +65,7 @@ def run(self):
from telepot.delegate import per_chat_id, create_run

bot = telepot.DelegatorBot(TOKEN, [
# For each chat_id, create a Player and delegate to its `run()` method.
# For each chat_id, create a Player and delegate to its `run` method.
(per_chat_id(), create_run(Player)),
])
bot.notifyOnMessage(run_forever=True)
4 changes: 2 additions & 2 deletions examples/guessa.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def run(self):
# wait for user's guess
msg = yield from asyncio.wait_for(self.listener.wait(chat__id=self.chat_id), self.WAIT_TIMEOUT)

msg_type, from_id, chat_id = telepot.glance(msg)
content_type, chat_type, chat_id = telepot.glance2(msg)

if msg_type != 'text':
if content_type != 'text':
yield from self.sender.sendMessage('Give me a number, please.')
continue

Expand Down
6 changes: 3 additions & 3 deletions examples/skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"""

def handle(msg):
msg_type, from_id, chat_id = telepot.glance(msg)
print msg_type, from_id, chat_id
# Do your stuff according to `msg_type` ...
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
Expand Down
26 changes: 26 additions & 0 deletions examples/skeleton_extend.py
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)
6 changes: 3 additions & 3 deletions examples/skeletona.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
# Add this decorator if you have `yield from` inside the function.
# @asyncio.coroutine
def handle(msg):
msg_type, from_id, chat_id = telepot.glance(msg)
print(msg_type, from_id, chat_id)
# Do your stuff according to `msg_type` ...
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
Expand Down

0 comments on commit c7fa78c

Please sign in to comment.