-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
113 lines (87 loc) · 2.17 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import telebot
from io import BytesIO
from PIL import Image
import os
# imports latest version of bincode
import requests
url = 'https://raw.githubusercontent.com/tusharhero/bincode/main/bincode.py'
bincode = requests.get(url)
open('bincode.py', 'wb').write(bincode.content)
import bincode as bc
def send_except(
reason,
chat_id,
):
print(reason)
reason_message ="""
Oops, an error occured!
Possible reason: \n
""" + reason
bot.send_message(
chat_id,
reason_message
)
# getting the API key
#f = open("API_KEY")
#API_KEY = f.read()
API_KEY = os.environ['KEY']
print(API_KEY)
# declaring the bot with API key
bot = telebot.TeleBot(API_KEY)
@bot.message_handler(
commands=[
'start',
'help'
]
)
def send_welcome(message):
chat_id = message.chat.id
welcome = """
Hello there!
To make a bincode out of your text:
`/txt2bincode <your text here>`
To read a bincode just send it!
If you want to use the camera try your best to crop it!
"""
bot.send_message(
chat_id,
welcome,
parse_mode="Markdown"
)
@bot.message_handler(
commands=[
'txt2bincode'
]
)
def send_bincode(message):
chat_id = message.chat.id
txt = message.text[13:] # slicing to remove the command part from the text
bincode = bc.txt2bincode(txt)
# send it.
bot.send_photo(chat_id, photo= bincode)
@bot.message_handler(
content_types=[
'image',
'photo'
]
)
def send_txt(message):
try:
chat_id = message.chat.id
file_info = bot.get_file(message.photo[-1].file_id)
downloaded_file = bot.download_file(file_info.file_path)
stream = BytesIO(downloaded_file)
bincode = Image.open(stream).convert("1")
bincode = bc.correctbincode(bincode)
txt = bc.bincode2txt(bincode)
bot.send_message(
chat_id,
txt
)
except:
txt = "We couldn't convert it to text but here is the raw data \n" + str(bc.rdbincodeimg(bincode))
bot.send_message(
chat_id,
txt
)
bot.infinity_polling()