Skip to content

Commit 7ec8cb2

Browse files
authored
Create Bot.py
1 parent 5aceff6 commit 7ec8cb2

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

Telegram/Bot.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import json
2+
import requests
3+
import time
4+
import urllib
5+
import urllib.request
6+
#from BrainFalcFury import data
7+
#import config
8+
import random
9+
#from chatbotLayout import iinput
10+
import os
11+
12+
TOKEN = "555183754:AAFbkdurKthflTQTpn6-HNpkFO5UA7VuBHk"
13+
URL = "https://api.telegram.org/bot{}/".format(TOKEN)
14+
15+
def get_url(url):
16+
response = requests.get(url)
17+
content = response.content.decode("utf8")
18+
return content
19+
20+
def get_json_from_url(url):
21+
content = get_url(url)
22+
js = json.loads(content)
23+
return js
24+
25+
26+
def get_updates(offset=None):
27+
url = URL + "getUpdates"
28+
if offset:
29+
url += "?offset={}".format(offset)
30+
js = get_json_from_url(url)
31+
return js
32+
33+
34+
def get_last_update_id(updates):
35+
update_ids = []
36+
for update in updates["result"]:
37+
update_ids.append(int(update["update_id"]))
38+
return max(update_ids)
39+
40+
41+
def echo_all(updates):
42+
try:
43+
for update in updates["result"]:
44+
text = update["message"]["text"]
45+
chat = update["message"]["chat"]["id"]
46+
#text = iinput(text, chat)
47+
#print(text)
48+
#keyboard = build_keyboard()
49+
send_message("Hii", chat)
50+
except Exception as e:
51+
print(e)
52+
53+
def get_last_chat_id_and_text(updates):
54+
num_updates = len(updates["result"])
55+
last_update = num_updates - 1
56+
text = updates["result"][last_update]["message"]["text"]
57+
chat_id = updates["result"][last_update]["message"]["chat"]["id"]
58+
return (text, chat_id)
59+
60+
61+
def send_message(text, chat_id, reply_markup=None):
62+
text = urllib.parse.quote_plus(text)
63+
url = URL + "sendMessage?text={}&chat_id={}".format(text, chat_id)
64+
if reply_markup:
65+
url += "&reply_markup={}".format(reply_markup)
66+
get_url(url)
67+
68+
def build_keyboard():
69+
keyboard = [['Cricket', 'Football'],
70+
['Reminder', 'News', 'Dictionary'],
71+
['/photo','Weather']]
72+
reply_markup = {"keyboard":keyboard, "one_time_keyboard": True}
73+
return json.dumps(reply_markup)
74+
75+
76+
def main():
77+
last_update_id = None
78+
print("Stage - 1")
79+
while True:
80+
updates = get_updates(last_update_id)
81+
if len(updates["result"]) > 0:
82+
last_update_id = get_last_update_id(updates) + 1
83+
print("Stage - 2")
84+
echo_all(updates)
85+
time.sleep(0.5)
86+
87+
88+
if __name__ == '__main__':
89+
main()

0 commit comments

Comments
 (0)