-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.py
40 lines (28 loc) · 925 Bytes
/
telegram.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
import requests
import os
import time
import json
from api_key import botkey
def generate_keyboard(n):
buttons = [[], []]
for i in range(0, n):
buttons[i%2].append(str(i))
return json.dumps({"keyboard": buttons, "one_time_keyboard": True, "resize_keyboard": True})
def send_message(text, keyboard=0):
if keyboard > 0:
reply_markup = generate_keyboard(keyboard)
else:
reply_markup = json.dumps({"remove_keyboard": True})
url = "https://api.telegram.org/bot"+botkey+"/sendMessage"
params = {"chat_id": 20403805,
"text": text,
"reply_markup": reply_markup}
r = requests.get(url, params=params)
def receive_message():
while not os.path.exists("msg_rcv.txt"):
time.sleep(0.1)
with open("msg_rcv.txt", "r") as f:
msg = f.read()
print "Received:", msg
os.remove("msg_rcv.txt")
return msg.strip()