Skip to content

Commit e62eeb7

Browse files
authored
Merge pull request eternnoir#565 from uburuntu/analyzer-fixes
Some analyzer fixes
2 parents b8f442d + 8636b28 commit e62eeb7

19 files changed

Lines changed: 126 additions & 113 deletions

‎examples/deep_linking.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,38 @@
3131
# steps are not shown here. Only steps 5 to 7 are illustrated, some in pseudo-code, with this example.
3232

3333
import telebot
34-
import time
3534

3635
bot = telebot.TeleBot('TOKEN')
3736

37+
3838
def extract_unique_code(text):
3939
# Extracts the unique_code from the sent /start command.
4040
return text.split()[1] if len(text.split()) > 1 else None
4141

42+
4243
def in_storage(unique_code):
4344
# (pseudo-code) Should check if a unique code exists in storage
4445
return True
4546

47+
4648
def get_username_from_storage(unique_code):
4749
# (pseudo-code) Does a query to the storage, retrieving the associated username
4850
# Should be replaced by a real database-lookup.
4951
return "ABC" if in_storage(unique_code) else None
5052

53+
5154
def save_chat_id(chat_id, username):
5255
# (pseudo-code) Save the chat_id->username to storage
5356
# Should be replaced by a real database query.
5457
pass
5558

59+
5660
@bot.message_handler(commands=['start'])
5761
def send_welcome(message):
5862
unique_code = extract_unique_code(message.text)
59-
if unique_code: # if the '/start' command contains a unique_code
63+
if unique_code: # if the '/start' command contains a unique_code
6064
username = get_username_from_storage(unique_code)
61-
if username: # if the username exists in our database
65+
if username: # if the username exists in our database
6266
save_chat_id(message.chat.id, username)
6367
reply = "Hello {0}, how are you?".format(username)
6468
else:
@@ -67,4 +71,5 @@ def send_welcome(message):
6771
reply = "Please visit me via a provided URL from the website."
6872
bot.reply_to(message, reply)
6973

74+
7075
bot.polling()

‎examples/detailed_example/detailed_example.py‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22
This is a detailed example using almost every command of the API
33
"""
44

5+
import time
6+
57
import telebot
68
from telebot import types
7-
import time
89

910
TOKEN = '<token_string>'
1011

1112
knownUsers = [] # todo: save these in a file,
1213
userStep = {} # so they won't reset every time the bot restarts
1314

1415
commands = { # command description used in the "help" command
15-
'start': 'Get used to the bot',
16-
'help': 'Gives you information about the available commands',
17-
'sendLongText': 'A test using the \'send_chat_action\' command',
18-
'getImage': 'A test using multi-stage messages, custom keyboard, and media sending'
16+
'start' : 'Get used to the bot',
17+
'help' : 'Gives you information about the available commands',
18+
'sendLongText': 'A test using the \'send_chat_action\' command',
19+
'getImage' : 'A test using multi-stage messages, custom keyboard, and media sending'
1920
}
2021

2122
imageSelect = types.ReplyKeyboardMarkup(one_time_keyboard=True) # create the image selection keyboard
@@ -33,7 +34,7 @@ def get_user_step(uid):
3334
else:
3435
knownUsers.append(uid)
3536
userStep[uid] = 0
36-
print "New user detected, who hasn't used \"/start\" yet"
37+
print("New user detected, who hasn't used \"/start\" yet")
3738
return 0
3839

3940

@@ -45,7 +46,7 @@ def listener(messages):
4546
for m in messages:
4647
if m.content_type == 'text':
4748
# print the sent message to the console
48-
print str(m.chat.first_name) + " [" + str(m.chat.id) + "]: " + m.text
49+
print(str(m.chat.first_name) + " [" + str(m.chat.id) + "]: " + m.text)
4950

5051

5152
bot = telebot.TeleBot(TOKEN)
@@ -128,4 +129,5 @@ def command_default(m):
128129
# this is the standard reply to a normal message
129130
bot.send_message(m.chat.id, "I don't understand \"" + m.text + "\"\nMaybe try the help page at /help")
130131

132+
131133
bot.polling()

‎examples/echo_bot.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
bot = telebot.TeleBot(API_TOKEN)
99

10+
1011
# Handle '/start' and '/help'
1112
@bot.message_handler(commands=['help', 'start'])
1213
def send_welcome(message):
@@ -21,4 +22,5 @@ def send_welcome(message):
2122
def echo_message(message):
2223
bot.reply_to(message, message.text)
2324

25+
2426
bot.polling()

‎examples/inline_example.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# This example show how to write an inline mode telegram bot use pyTelegramBotAPI.
2-
import telebot
3-
import time
4-
import sys
52
import logging
3+
import sys
4+
import time
5+
6+
import telebot
67
from telebot import types
78

89
API_TOKEN = '<TOKEN>'
@@ -69,5 +70,5 @@ def main_loop():
6970
try:
7071
main_loop()
7172
except KeyboardInterrupt:
72-
print >> sys.stderr, '\nExiting by user request.\n'
73+
print('\nExiting by user request.\n')
7374
sys.exit(0)

‎examples/payments_example.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import telebot
2-
from telebot.types import LabeledPrice
3-
from telebot.types import ShippingOption
2+
from telebot.types import LabeledPrice, ShippingOption
43

54
token = '1234567890:AAAABBBBCCCCDDDDeeeeFFFFgggGHHHH'
65
provider_token = '1234567890:TEST:AAAABBBBCCCCDDDD' # @BotFather -> Bot Settings -> Payments

‎examples/step_example.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"""
33
This Example will show you how to use register_next_step handler.
44
"""
5-
import time
65

76
import telebot
87
from telebot import types
@@ -84,5 +83,4 @@ def process_sex_step(message):
8483
# WARNING It will work only if enable_save_next_step_handlers was called!
8584
bot.load_next_step_handlers()
8685

87-
8886
bot.polling()

‎examples/telebot_bot/telebot_bot.py‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
# and goes by the name 'TeleBot (@pyTeleBot)'. Join our group to talk to him!
44
# WARNING: Tested with Python 2.7
55

6-
import telebot
76
import os
87

8+
import telebot
9+
910
text_messages = {
1011
'welcome':
1112
u'Please welcome {name}!\n\n'
@@ -33,8 +34,10 @@
3334
bot = telebot.AsyncTeleBot(os.environ["TELEBOT_BOT_TOKEN"])
3435
GROUP_CHAT_ID = int(os.environ["GROUP_CHAT_ID"])
3536

37+
3638
def is_api_group(chat_id):
37-
return chat_id== GROUP_CHAT_ID
39+
return chat_id == GROUP_CHAT_ID
40+
3841

3942
@bot.message_handler(func=lambda m: True, content_types=['new_chat_participant'])
4043
def on_user_joins(message):
@@ -50,6 +53,7 @@ def on_user_joins(message):
5053

5154
bot.reply_to(message, text_messages['welcome'].format(name=name))
5255

56+
5357
@bot.message_handler(commands=['info', 'help'])
5458
def on_info(message):
5559
if not is_api_group(message.chat.id):
@@ -58,21 +62,23 @@ def on_info(message):
5862

5963
bot.reply_to(message, text_messages['info'])
6064

65+
6166
@bot.message_handler(commands=["ping"])
6267
def on_ping(message):
6368
bot.reply_to(message, "Still alive and kicking!")
6469

70+
6571
@bot.message_handler(commands=['start'])
6672
def on_start(message):
6773
if not is_api_group(message.chat.id):
6874
bot.reply_to(message, text_messages['wrong_chat'])
6975
return
7076

77+
7178
def listener(messages):
7279
for m in messages:
73-
print str(m)
80+
print(str(m))
81+
7482

7583
bot.set_update_listener(listener)
7684
bot.polling()
77-
78-

‎examples/webhook_examples/webhook_aiohttp_echo_bot.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import telebot
1313

14-
1514
API_TOKEN = '<api_token>'
1615

1716
WEBHOOK_HOST = '<ip/host where the bot is running>'
@@ -32,7 +31,6 @@
3231
WEBHOOK_URL_BASE = "https://{}:{}".format(WEBHOOK_HOST, WEBHOOK_PORT)
3332
WEBHOOK_URL_PATH = "/{}/".format(API_TOKEN)
3433

35-
3634
logger = telebot.logger
3735
telebot.logger.setLevel(logging.INFO)
3836

@@ -51,6 +49,7 @@ async def handle(request):
5149
else:
5250
return web.Response(status=403)
5351

52+
5453
app.router.add_post('/{token}/', handle)
5554

5655

@@ -72,7 +71,7 @@ def echo_message(message):
7271
bot.remove_webhook()
7372

7473
# Set webhook
75-
bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH,
74+
bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH,
7675
certificate=open(WEBHOOK_SSL_CERT, 'r'))
7776

7877
# Build ssl context

‎examples/webhook_examples/webhook_cherrypy_echo_bot.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
# This is a simple echo bot using decorators and webhook with CherryPy
55
# It echoes any incoming text messages and does not use the polling method.
66

7-
import cherrypy
8-
import telebot
97
import logging
108

9+
import cherrypy
10+
11+
import telebot
1112

1213
API_TOKEN = '<api_token>'
1314

@@ -29,7 +30,6 @@
2930
WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
3031
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)
3132

32-
3333
logger = telebot.logger
3434
telebot.logger.setLevel(logging.INFO)
3535

@@ -70,7 +70,7 @@ def echo_message(message):
7070
bot.remove_webhook()
7171

7272
# Set webhook
73-
bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH,
73+
bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH,
7474
certificate=open(WEBHOOK_SSL_CERT, 'r'))
7575

7676
# Disable CherryPy requests log
@@ -80,9 +80,9 @@ def echo_message(message):
8080

8181
# Start cherrypy server
8282
cherrypy.config.update({
83-
'server.socket_host': WEBHOOK_LISTEN,
84-
'server.socket_port': WEBHOOK_PORT,
85-
'server.ssl_module': 'builtin',
83+
'server.socket_host' : WEBHOOK_LISTEN,
84+
'server.socket_port' : WEBHOOK_PORT,
85+
'server.ssl_module' : 'builtin',
8686
'server.ssl_certificate': WEBHOOK_SSL_CERT,
8787
'server.ssl_private_key': WEBHOOK_SSL_PRIV
8888
})

‎examples/webhook_examples/webhook_cpython_echo_bot.py‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
# This is a simple echo bot using decorators and webhook with BaseHTTPServer
55
# It echoes any incoming text messages and does not use the polling method.
66

7-
import BaseHTTPServer
8-
import ssl
9-
import telebot
7+
try:
8+
# Python 2
9+
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
10+
except ImportError:
11+
# Python 3
12+
from http.server import BaseHTTPRequestHandler, HTTPServer
13+
1014
import logging
15+
import ssl
1116

17+
import telebot
1218

1319
API_TOKEN = '<api_token>'
1420

@@ -30,15 +36,14 @@
3036
WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
3137
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)
3238

33-
3439
logger = telebot.logger
3540
telebot.logger.setLevel(logging.INFO)
3641

3742
bot = telebot.TeleBot(API_TOKEN)
3843

3944

4045
# WebhookHandler, process webhook calls
41-
class WebhookHandler(BaseHTTPServer.BaseHTTPRequestHandler):
46+
class WebhookHandler(BaseHTTPRequestHandler):
4247
server_version = "WebhookHandler/1.0"
4348

4449
def do_HEAD(self):
@@ -84,12 +89,12 @@ def echo_message(message):
8489
bot.remove_webhook()
8590

8691
# Set webhook
87-
bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH,
92+
bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH,
8893
certificate=open(WEBHOOK_SSL_CERT, 'r'))
8994

9095
# Start server
91-
httpd = BaseHTTPServer.HTTPServer((WEBHOOK_LISTEN, WEBHOOK_PORT),
92-
WebhookHandler)
96+
httpd = HTTPServer((WEBHOOK_LISTEN, WEBHOOK_PORT),
97+
WebhookHandler)
9398

9499
httpd.socket = ssl.wrap_socket(httpd.socket,
95100
certfile=WEBHOOK_SSL_CERT,

0 commit comments

Comments
 (0)