Last active
August 29, 2015 14:24
-
-
Save SteffenDE/aefd273ddef47999873b to your computer and use it in GitHub Desktop.
badtelegrambot.py
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import requests | |
import time | |
import json | |
import urllib | |
apikey = "XXXXXXXXXX12345678909876543XXXXXXXX" | |
apiurl = "https://api.telegram.org/bot" | |
def getoffset(): | |
req = requests.get(apiurl+apikey+'/getUpdates') | |
json_data_g = req.text | |
pythonobjg = json.loads(json_data_g) | |
result = len(pythonobjg["result"]) | |
if result > 0: | |
offsetid = pythonobjg["result"][result-1]["update_id"] | |
print "OFFSET: "+unicode(offsetid) | |
else: | |
offsetid = None | |
return offsetid | |
while True: | |
try: | |
if offsetid is None: | |
offsetid = getoffset() | |
except: | |
offsetid = getoffset() | |
if offsetid is not None: | |
r = requests.get(apiurl+apikey+'/getUpdates'+"?offset="+unicode(offsetid)) | |
json_data = r.text | |
pythonobj = json.loads(json_data) | |
result = len(pythonobj["result"]) | |
if result == 0: | |
time.sleep(0.5) | |
else: | |
for item in range(result): | |
try: | |
text = pythonobj["result"][item]["message"]["text"] | |
except: | |
text = "" | |
requests.get(apiurl+apikey+"/sendMessage?chat_id="+unicode(pythonobj["result"][item]["message"]["chat"]["id"])+"&text="+urllib.quote_plus("Wrong messagetype...")) | |
if "/start" in text or "/help" in text: | |
instructions = """\ | |
Usage... | |
...""" | |
requests.get(apiurl+apikey+"/sendMessage?chat_id="+unicode(pythonobj["result"][item]["message"]["chat"]["id"])+"&text="+urllib.quote_plus(instructions)) | |
else: | |
pass | |
print offsetid | |
offsetid += result | |
time.sleep(0.5) | |
else: | |
time.sleep(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment