-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessageBot.py
65 lines (42 loc) · 1.81 KB
/
messageBot.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
apiCall = "https://api.telegram.org/{YOUR BOT TOKEN}"
static_commands = {'lights on', 'lights off', '/help'}
userList = {'{YOUR USER CHAT ID}'} # can get this by sending the bot a message and looking at the http endpoint url
def ChatBot(event, context): # event holds the content of the messages
print("Received event: " + json.dumps(event, indent=2))
parse_message(event)
print("event ends")
return {'statusCode': 200, 'headers': { 'Content-Type': 'application/json' },'body': 'successful'} # confirm event occurred successfully
def get_url(url): #interacts with the HTTP endpoint
response = requests.get(url)
urlContent = response.content
return urlContent
def parse_message(update): # parses the message
text = update["message"]["text"] #gets the message
chatId = update["message"]["chat"]["id"] #unique chat id
if str(chatId) not in userList: # only grands access to specific users
print "Unauthorized Access ", chatId
text = "Unauthorized Access"
elif text.lower() not in static_commands:
text = "not a valid command"
elif text.lower() == '/help':
text = "The current valid commands, \n 1) lights on \n 2) lights off"
elif text.lower() == 'lights on':
lightsOn() # add to this later
text = "The lights are now on"
elif text.lower() == 'lights off':
lightsOff()
text = "The lights are now off"
send_Message(text, chatId)
def send_Message(text, chatId): # sets up the send url
sendUrl = apiCall +"/sendMessage?chat_id={}&text={}".format(chatId,text)
get_url(sendUrl)
def lightsOn(): #add to this later
print("lights turn on")
def lightsOff(): #add to this later
print("lights turn off")
if __name__ == '__main__': #only if not imported by another script
main()