Created
June 25, 2015 12:28
-
-
Save scturtle/d53ff9faf146d5388b14 to your computer and use it in GitHub Desktop.
telegram bot (long polling)
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 python3 | |
import requests | |
prefix = 'https://api.telegram.org/bot' | |
key = '' | |
geturl = prefix + key + '/getUpdates' | |
sendurl = prefix + key + '/sendMessage' | |
timeout = 60 | |
def main(): | |
offset = 0 | |
while True: | |
dt = dict(offset=offset, timeout=timeout) | |
try: | |
j = requests.post(geturl, data=dt, timeout=None).json() | |
except ValueError: # incomplete data | |
continue | |
if not j['ok'] or not j['result']: | |
continue | |
for r in j['result']: | |
m = r['message'] | |
cid = m['chat']['id'] | |
if 'text' in m: # and m['text'][0] == '/': | |
dt = dict(chat_id=cid, text=reply) | |
requests.post(sendurl, data=dt).json() | |
offset = r['update_id'] + 1 | |
if __name__ == '__main__': | |
try: | |
main() | |
except KeyboardInterrupt: | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment