Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. def reply(msg=None, img=None):
  2. if msg:
  3. resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
  4. 'chat_id': str(chat_id),
  5. 'text': msg.encode('utf-8'),
  6. 'disable_web_page_preview': 'true',
  7. 'reply_to_message_id': str(message_id),
  8. })).read()
  9. elif img:
  10. resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [
  11. ('chat_id', str(chat_id)),
  12. ('reply_to_message_id', str(message_id)),
  13. ], [
  14. ('photo', 'image.jpg', img),
  15. ])
  16. else:
  17. logging.error('no msg or img specified')
  18. resp = None
  19.  
  20. logging.info('send response:')
  21. logging.info(resp)
  22.  
  23. if text.startswith('/'):
  24. if text == '/start':
  25. reply('Bot enabled')
  26. setEnabled(chat_id, True)
  27. elif text == '/stop':
  28. reply('Bot disabled')
  29. setEnabled(chat_id, False)
  30. elif text == '/image':
  31. img = Image.new('RGB', (512, 512))
  32. base = random.randint(0, 16777216)
  33. pixels = [base+i*j for i in range(512) for j in range(512)] # generate sample image
  34. img.putdata(pixels)
  35. output = StringIO.StringIO()
  36. img.save(output, 'JPEG')
  37. reply(img=output.getvalue())
  38. else:
  39. reply('What command?')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement