import sys
import asyncio
import telepot
from telepot.delegate import per_chat_id
from telepot.async.delegate import create_open

"""
$ python3.4 countera.py <token>
Count number of messages. Start over if silent for 10 seconds.
"""


class MessageCounter(telepot.helper.ChatHandler):
    def __init__(self, seed_tuple, timeout):
        super(MessageCounter, self).__init__(seed_tuple, timeout)
        self._count = 0

    @asyncio.coroutine
    def on_message(self, msg):
        self._count += 1
        yield from self.sender.sendMessage(self._count)

TOKEN = '148865285:AAHvwDHJGVrSzEGJ_ToGUxk1RWclvX2L_W4'

bot = telepot.async.DelegatorBot(TOKEN, [
    (per_chat_id(), create_open(MessageCounter, timeout=10)),
])

loop = asyncio.get_event_loop()
loop.create_task(bot.messageLoop())
print('Listening ...')

loop.run_forever()
import telepot
from telepot.delegate import per_chat_id, create_open


class VeryCruel(telepot.helper.ChatHandler):

    def __init__(self, seed_tuple, timeout):
        super(VeryCruel, self).__init__(seed_tuple,timeout)

    def reply_to_serjant(self,m):
        self.sender.sendMessage('Сообщение получено от %s c id %s' % (m.from_, m.from_.id))

    def on_message(self, msg):
        content_type, chat_type, chat_id = telepot.glance2(msg)
        m = telepot.namedtuple(msg, 'Message')

        if chat_id < 0:
            # public chat
            self.reply_to_serjant(m)
        else:
            # private conversation
            if content_type == 'text':
                if 'хуй' in m.text:
                    self.sender.sendMessage('НЕ МАТЕРИСЬ, ПИДАРАС!', reply_to_message_id=m.message_id)

TOKEN = '148865285:AAHvwDHJGVrSzEGJ_ToGUxk1RWclvX2L_W4'

bot = telepot.DelegatorBot(TOKEN, [
    (per_chat_id(), create_open(VeryCruel, timeout=15)),
])
bot.notifyOnMessage(run_forever=True)

Rewrite bot to answer private or public messages
Rewrite bot to answer private or public messages

- import sys
- import asyncio
  import telepot
- from telepot.delegate import per_chat_id
+ from telepot.delegate import per_chat_id, create_open
- from telepot.async.delegate import create_open
- 
- """
- $ python3.4 countera.py <token>
- Count number of messages. Start over if silent for 10 seconds.
- """
  
  
- class MessageCounter(telepot.helper.ChatHandler):
+ class VeryCruel(telepot.helper.ChatHandler):
+ 
      def __init__(self, seed_tuple, timeout):
-         super(MessageCounter, self).__init__(seed_tuple, timeout)
+         super(VeryCruel, self).__init__(seed_tuple,timeout)
-         self._count = 0
  
-     @asyncio.coroutine
+     def reply_to_serjant(self,m):
+         self.sender.sendMessage('Сообщение получено от %s c id %s' % (m.from_, m.from_.id))
+ 
      def on_message(self, msg):
-         self._count += 1
-         yield from self.sender.sendMessage(self._count)
+         content_type, chat_type, chat_id = telepot.glance2(msg)
+         m = telepot.namedtuple(msg, 'Message')
+ 
+         if chat_id < 0:
+             # public chat
+             self.reply_to_serjant(m)
+         else:
+             # private conversation
+             if content_type == 'text':
+                 if 'хуй' in m.text:
+                     self.sender.sendMessage('НЕ МАТЕРИСЬ, ПИДАРАС!', reply_to_message_id=m.message_id)
  
  TOKEN = '148865285:AAHvwDHJGVrSzEGJ_ToGUxk1RWclvX2L_W4'
  
- bot = telepot.async.DelegatorBot(TOKEN, [
+ bot = telepot.DelegatorBot(TOKEN, [
-     (per_chat_id(), create_open(MessageCounter, timeout=10)),
+     (per_chat_id(), create_open(VeryCruel, timeout=15)),
  ])
+ bot.notifyOnMessage(run_forever=True)
  
- loop = asyncio.get_event_loop()
- loop.create_task(bot.messageLoop())
- print('Listening ...')
- 
- loop.run_forever()
Rewrite bot to answer private or public messages
## Code Before:
import sys
import asyncio
import telepot
from telepot.delegate import per_chat_id
from telepot.async.delegate import create_open

"""
$ python3.4 countera.py <token>
Count number of messages. Start over if silent for 10 seconds.
"""


class MessageCounter(telepot.helper.ChatHandler):
    def __init__(self, seed_tuple, timeout):
        super(MessageCounter, self).__init__(seed_tuple, timeout)
        self._count = 0

    @asyncio.coroutine
    def on_message(self, msg):
        self._count += 1
        yield from self.sender.sendMessage(self._count)

TOKEN = '148865285:AAHvwDHJGVrSzEGJ_ToGUxk1RWclvX2L_W4'

bot = telepot.async.DelegatorBot(TOKEN, [
    (per_chat_id(), create_open(MessageCounter, timeout=10)),
])

loop = asyncio.get_event_loop()
loop.create_task(bot.messageLoop())
print('Listening ...')

loop.run_forever()
## Instruction:
Rewrite bot to answer private or public messages
## Code After:
import telepot
from telepot.delegate import per_chat_id, create_open


class VeryCruel(telepot.helper.ChatHandler):

    def __init__(self, seed_tuple, timeout):
        super(VeryCruel, self).__init__(seed_tuple,timeout)

    def reply_to_serjant(self,m):
        self.sender.sendMessage('Сообщение получено от %s c id %s' % (m.from_, m.from_.id))

    def on_message(self, msg):
        content_type, chat_type, chat_id = telepot.glance2(msg)
        m = telepot.namedtuple(msg, 'Message')

        if chat_id < 0:
            # public chat
            self.reply_to_serjant(m)
        else:
            # private conversation
            if content_type == 'text':
                if 'хуй' in m.text:
                    self.sender.sendMessage('НЕ МАТЕРИСЬ, ПИДАРАС!', reply_to_message_id=m.message_id)

TOKEN = '148865285:AAHvwDHJGVrSzEGJ_ToGUxk1RWclvX2L_W4'

bot = telepot.DelegatorBot(TOKEN, [
    (per_chat_id(), create_open(VeryCruel, timeout=15)),
])
bot.notifyOnMessage(run_forever=True)
