-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtelegramFunc.py
67 lines (50 loc) · 2.14 KB
/
telegramFunc.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
# Standard imports
import requests
import os
# Non standard imports
import telegram
lastMessage = 119801179 #Used to check if the message received hasent been read already
def parse_cred():
"""Gets the bottoken and chatid from the credential file"""
file_name = "credential.txt"
with open(file_name, "r", encoding="UTF-8") as file:
lines = file.readlines()
bot_token = lines[0][:-1]
chatID = lines[1]
return bot_token, chatID
def telegram_bot_sendtext(bot_message, userID):
"""Receives the message for the bot to send, and sends it to the bot specified below"""
bot_token, bot_chatID = parse_cred()
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + str(userID) + '&parse_mode=Markdown&text=' + bot_message
response = requests.get(send_text)
return response.json()
def receive_bot_message(responseList):
"""Receives the messages coming from the user and makes sure that messages are not read twice"""
global lastMessage
currentMessage = ''
bot_token, bot_chatID = parse_cred()
link = "https://api.telegram.org/bot" + bot_token + "/getUpdates"
response = requests.get(link) #get the json data from the link
response = response.json()
response = response['result'] #go to the result part of the dictionary
if len(response) == 0:
# means that I have not received a message in a long time
return False
response = response[-1] #go to the last sent message
currentMessage = response['update_id'] #get the message code to check if we are not repeating messages
message = response['message']
message = message['text']
if not currentMessage == lastMessage:
lastMessage = currentMessage
responseList.append(response["message"]["chat"]["id"])
return message
else:
return False
def send_photo(photo, userID):
"""
Sends a photo and deletes it afterwards
"""
token, chatID = parse_cred()
bot = telegram.Bot(token=token) # Start the telegram API
bot.send_document(userID, document=open(photo, 'rb')) # Send the image
os.remove(photo) # delete file