This repository was archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path#04-commands.py
More file actions
118 lines (87 loc) · 3.49 KB
/
Copy path#04-commands.py
File metadata and controls
118 lines (87 loc) · 3.49 KB
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
114
115
116
117
118
import requests
import json
from time import sleep
global OFFSET
OFFSET = 0
botToken = ""
global requestURL
global sendURL
standardURL = "http://api.telegram.org/bot" + botToken
requestURL = "http://api.telegram.org/bot" + botToken + "/getUpdates"
sendURL = "http://api.telegram.org/bot" + botToken + "/sendMessage"
commandList = [{"command":"sayhi","description":"Says Hi!"},{"command":"like","description":"Drops a Like"},{"command":"help","description":"Should post a little bit of help, but doesn't"}]
def update (url):
global OFFSET
try:
update_raw = requests.get(url + "?offset=" + str(OFFSET))
update = update_raw.json()
result = extract_result(update)
if result != False:
OFFSET = result['update_id'] + 1
return result
else:
return False
except requests.exceptions.ConnectionError:
pass
def extract_result (dict):
result_array = dict['result']
if result_array == []:
return False
else:
result_dic = result_array[0]
return result_dic
def send_message (chatId, message):
requests.post(sendURL + "?chat_id=" + str(chatId) + "&text=" + message)
# --- functions for command behaviour ---
def get_botCommands ():
commandsraw = requests.get(standardURL + "/getMyCommands")
commands = commandsraw.json()
return commands
def set_botCommands (commandList):
try:
requests.post(standardURL + "/setMyCommands?commands=" + json.dumps(commandList))
except Exception as e:
return e
def is_command (messageData):
if 'entities' in messageData:
enitities = messageData['entities']
entity = enitities[0]
if entity['type'] == 'bot_command':
spliced_command = splice_command(messageData['text'], entity['offset'], entity['length'])
return spliced_command
return False
def splice_command (text, offset, length):
a = text[offset:]
b = a[:length]
return b
def handle_command (command, messageData):
userchatid = messageData['message']['chat']['id']
username = messageData['message']['chat']['first_name']
if command == "/sayhi":
send_message(userchatid, "Hi! I'm a Telegram-Bot. Everytime someone sends the command '/sayhi', I am forced to say 'Hi!' back. So, there you go:\nHi, " + username + "!")
elif command == "/like":
send_message(userchatid, "Go and like my latest YouTube-Video!")
elif command == "/help":
send_message(userchatid, "Nope.")
else:
send_message(userchatid, "Sorry, I don't understand that language.")
#print (get_botCommands())
set_botCommands(commandList)
while True:
newmessage = update (requestURL)
if newmessage != False:
commandcheck = is_command(newmessage['message'])
if commandcheck != False:
handle_command(commandcheck, newmessage)
#optional kann man das "Papageienspiel" in einen Else-Fall schieben
else:
userchatid = newmessage['message']['chat']['id']
usertext = newmessage['message']['text']
username = newmessage['message']['chat']['first_name']
if usertext.lower() == "hello":
send_message(userchatid, "Hi " + username)
pass
else:
send_message(userchatid, "You said: " + usertext)
pass
sleep (1)