Guest User

Telegram Bot: Welcome new user

a guest
Jun 30th, 2015
902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import json
  2. import logging
  3. import urllib
  4. import urllib2
  5.  
  6. from google.appengine.api import urlfetch
  7. from google.appengine.ext import ndb
  8. import webapp2
  9.  
  10. TOKEN = 'YOUR_BOT_TOKEN_HERE'
  11.  
  12. BASE_URL = 'https://api.telegram.org/bot' + TOKEN + '/'
  13.  
  14.  
  15. # ================================
  16.  
  17. class EnableStatus(ndb.Model):
  18.     # key name: str(chat_id)
  19.     enabled = ndb.BooleanProperty(indexed=False, default=False)
  20.  
  21.  
  22. # ================================
  23.  
  24. def setEnabled(chat_id, yes):
  25.     es = EnableStatus.get_or_insert(str(chat_id))
  26.     es.enabled = yes
  27.     es.put()
  28.  
  29. def getEnabled(chat_id):
  30.     es = EnableStatus.get_by_id(str(chat_id))
  31.     if es:
  32.         return es.enabled
  33.     return False
  34.  
  35. # ================================
  36.  
  37. class MeHandler(webapp2.RequestHandler):
  38.     def get(self):
  39.         urlfetch.set_default_fetch_deadline(60)
  40.         self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'getMe'))))
  41.  
  42.  
  43. class GetUpdatesHandler(webapp2.RequestHandler):
  44.     def get(self):
  45.         urlfetch.set_default_fetch_deadline(60)
  46.         self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'getUpdates'))))
  47.  
  48.  
  49. class SetWebhookHandler(webapp2.RequestHandler):
  50.     def get(self):
  51.         urlfetch.set_default_fetch_deadline(60)
  52.         url = self.request.get('url')
  53.         if url:
  54.             self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'setWebhook', urllib.urlencode({'url': url})))))
  55.  
  56. class WebhookHandler(webapp2.RequestHandler):
  57.     def post(self):
  58.         urlfetch.set_default_fetch_deadline(60)
  59.         body = json.loads(self.request.body)
  60.         logging.info('request body:')
  61.         logging.info(body)
  62.         self.response.write(json.dumps(body))
  63.  
  64.         update_id = body['update_id']
  65.         message = body['message']
  66.         message_id = message.get('message_id')
  67.         date = message.get('date')
  68.         text = message.get('text')
  69.         fr = message.get('from')
  70.         chat = message['chat']
  71.         chat_id = chat['id']
  72.  
  73.         def reply(msg):
  74.             resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
  75.                 'chat_id': str(chat_id),
  76.                 'text': msg,
  77.                 'disable_web_page_preview': 'true',
  78.                 'reply_to_message_id': str(message_id),
  79.             })).read()
  80.             logging.info('send response:')
  81.             logging.info(resp)
  82.  
  83.         def send(msg):
  84.             resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
  85.                 'chat_id': str(chat_id),
  86.                 'text': msg,
  87.             })).read()
  88.             logging.info('send msg:')
  89.             logging.info(resp)
  90.  
  91.         if 'new_chat_participant' in message:
  92.             new_user = message.get('new_chat_participant')
  93.             send('Welcome {0}!'.format(new_user['username']))
  94.  
  95.         if not text:
  96.             logging.info('no text')
  97.             return
  98.  
  99.         if text.startswith('/'):
  100.             if text == '/start':
  101.                 reply('Bot enabled')
  102.                 setEnabled(chat_id, True)
  103.             elif text == '/stop':
  104.                 reply('Bot disabled')
  105.                 setEnabled(chat_id, False)
  106.             elif text == '/getChatId':
  107.                 reply(str(chat_id))
  108.             else:
  109.                 reply('What command?')
  110.  
  111.         # CUSTOMIZE FROM HERE
  112.  
  113.         elif 'who are you' in text:
  114.             reply('telebot starter kit, created by yukuku: https://github.com/yukuku/telebot')
  115.         elif 'what time' in text:
  116.             reply('look at the top-right corner of your screen!')
  117.         else:
  118.             if getEnabled(chat_id):
  119.                 resp1 = json.load(urllib2.urlopen('http://www.simsimi.com/requestChat?lc=en&ft=1.0&req=' + urllib.quote_plus(text.encode('utf-8'))))
  120.                 back = resp1.get('res')
  121.                 if not back:
  122.                     reply('okay...')
  123.                 elif 'I HAVE NO RESPONSE' in back:
  124.                     reply('you said something with no meaning')
  125.                 else:
  126.                     reply(back)
  127.             else:
  128.                 logging.info('not enabled for chat_id {}'.format(chat_id))
  129.  
  130.  
  131. class TestMessage(webapp2.RequestHandler):
  132.     def get(self):
  133.         resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
  134.             'chat_id': str(1337), # REPLACE 1337 WITH YOUR CHAT ID
  135.             'text': 'Hi there! Test test test!'
  136.         })).read()
  137.  
  138. app = webapp2.WSGIApplication([
  139.     ('/me', MeHandler),
  140.     ('/updates', GetUpdatesHandler),
  141.     ('/set_webhook', SetWebhookHandler),
  142.     ('/webhook', WebhookHandler),
  143.     ('/testmsg', TestMessage),
  144. ], debug=True)
Advertisement
Add Comment
Please, Sign In to add comment