1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

fix: python 2/3 compatibility in examples

This commit is contained in:
uburuntu 2018-08-17 12:47:44 +03:00
parent 6a4c7e731b
commit 65a272b901
4 changed files with 13 additions and 7 deletions

View File

@ -33,7 +33,7 @@ def get_user_step(uid):
else: else:
knownUsers.append(uid) knownUsers.append(uid)
userStep[uid] = 0 userStep[uid] = 0
print "New user detected, who hasn't used \"/start\" yet" print("New user detected, who hasn't used \"/start\" yet")
return 0 return 0
@ -45,7 +45,7 @@ def listener(messages):
for m in messages: for m in messages:
if m.content_type == 'text': if m.content_type == 'text':
# print the sent message to the console # print the sent message to the console
print str(m.chat.first_name) + " [" + str(m.chat.id) + "]: " + m.text print(str(m.chat.first_name) + " [" + str(m.chat.id) + "]: " + m.text)
bot = telebot.TeleBot(TOKEN) bot = telebot.TeleBot(TOKEN)

View File

@ -69,5 +69,5 @@ if __name__ == '__main__':
try: try:
main_loop() main_loop()
except KeyboardInterrupt: except KeyboardInterrupt:
print >> sys.stderr, '\nExiting by user request.\n' print('\nExiting by user request.\n')
sys.exit(0) sys.exit(0)

View File

@ -70,7 +70,7 @@ def on_start(message):
def listener(messages): def listener(messages):
for m in messages: for m in messages:
print str(m) print(str(m))
bot.set_update_listener(listener) bot.set_update_listener(listener)
bot.polling() bot.polling()

View File

@ -4,7 +4,13 @@
# This is a simple echo bot using decorators and webhook with BaseHTTPServer # This is a simple echo bot using decorators and webhook with BaseHTTPServer
# It echoes any incoming text messages and does not use the polling method. # It echoes any incoming text messages and does not use the polling method.
import BaseHTTPServer try:
# Python 2
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
except ImportError:
# Python 3
from http.server import BaseHTTPRequestHandler, HTTPServer
import ssl import ssl
import telebot import telebot
import logging import logging
@ -38,7 +44,7 @@ bot = telebot.TeleBot(API_TOKEN)
# WebhookHandler, process webhook calls # WebhookHandler, process webhook calls
class WebhookHandler(BaseHTTPServer.BaseHTTPRequestHandler): class WebhookHandler(BaseHTTPRequestHandler):
server_version = "WebhookHandler/1.0" server_version = "WebhookHandler/1.0"
def do_HEAD(self): def do_HEAD(self):
@ -88,7 +94,7 @@ bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH,
certificate=open(WEBHOOK_SSL_CERT, 'r')) certificate=open(WEBHOOK_SSL_CERT, 'r'))
# Start server # Start server
httpd = BaseHTTPServer.HTTPServer((WEBHOOK_LISTEN, WEBHOOK_PORT), httpd = HTTPServer((WEBHOOK_LISTEN, WEBHOOK_PORT),
WebhookHandler) WebhookHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, httpd.socket = ssl.wrap_socket(httpd.socket,