/
bot_cli.py
72 lines (57 loc) · 1.73 KB
/
bot_cli.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
67
68
69
70
71
72
import os
import sys
import json
import socket
from datetime import datetime
from urllib.parse import urlencode
from urllib.request import Request, urlopen
from flask import Flask, request
##
# Assuming we've started up the bot on localhost using gunicorn,
# this program provides a CLI for testing the bot locally before
# deployment to heroku
##
def send_to_bot(msg):
url = 'http://127.0.0.1:8000'
data = {
"attachments": [],
"avatar_url": "http://i.groupme.com/123456789",
"created_at": 1302623328,
"group_id": "1234567890",
"id": "1234567890",
"sender_id": "12345",
"sender_type": "user",
"source_guid": "GUID",
"system": False,
"user_id": "1234567890",
"name": msg['author'],
"text": msg['text'],
}
params = json.dumps(data).encode('utf-8')
request = Request(url, data=params)
request.add_header('Content-Type', 'application/json')
reply = urlopen(request).read().decode()
def request_canned_message(title):
url = 'http://127.0.0.1:8000/canned'
data = {
"topic": title,
}
params = json.dumps(data).encode('utf-8')
request = Request(url, data=params)
request.add_header('Content-Type', 'application/json')
reply = urlopen(request).read().decode()
if __name__ == '__main__':
while True:
text = input('>>> ')
if text.startswith(':'):
commands = text.split()
# special commands:
if commands[0] == ':quit':
break
elif commands[0] == ':canned':
request_canned_message(commands[1])
msg = {}
msg['author'] = 'Andrew Norton'
msg['text'] = text
msg['timestamp'] = str(datetime.now())
send_to_bot(msg)