-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python 3 support #9
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8049e9e
Changed Python version number
Half-Shot 9f86925
Added samples with testing script
Half-Shot 6a9c54f
Updated api/client to better support python3.
Half-Shot 0208fb5
Added simple example for writing a client.
Half-Shot d991fc4
Added help text for script.
Half-Shot 6a14f9d
Hide inputted password.
Half-Shot d8805dd
Stray f
Half-Shot b260830
Minor fixes to help in startSample.sh
Half-Shot 39bae50
Updated api.py to properly support both versions of python.
Half-Shot 6d05f72
Modified readme to include both python versions and sample information
Half-Shot 7243ed9
Fixed script not running correctly.
Half-Shot dc789c4
Added args listing, error codes and more verbose
Half-Shot 0803636
Neatened up the code and made _send more flexible.
Half-Shot 9672233
Removed several args from send_image and used kvargs instead.
Half-Shot d90a3b1
Removed redundant shouldRun
Half-Shot 12e15b3
Removed shell script in favor of finding the library path inside the …
Half-Shot 780ebff
Fixed sys.exit being called too soon.
Half-Shot 4b0dafe
Rogue Semicolons
Half-Shot 17622f8
Descriptive error message on upload failure.
Half-Shot 8cb251c
Misc fixes.
Half-Shot e150667
Changed sample information on readme.
Half-Shot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# A simple chat client for matrix. | ||
# This sample will allow you to connect to a room, and send/recieve messages. | ||
# Args: host:port username room | ||
# Error Codes: | ||
# 1 - Unknown problem has occured | ||
# 2 - Could not find the server. | ||
# 3 - Bad URL Format. | ||
# 4 - Bad username/password. | ||
# 11 - Wrong room format. | ||
# 12 - Couldn't find room. | ||
|
||
import sys | ||
sys.path.insert(0, "../") # add ../ to PYTHONPATH | ||
|
||
from matrix_client.client import MatrixClient | ||
from matrix_client.api import MatrixRequestError | ||
from requests.exceptions import MissingSchema | ||
|
||
from getpass import getpass | ||
|
||
# Called when a message is recieved. | ||
def on_message(event): | ||
if event['type'] == "m.room.member": | ||
if event['membership'] == "join": | ||
print("{0} joined".format(event['content']['displayname'])) | ||
elif event['type'] == "m.room.message": | ||
if event['content']['msgtype'] == "m.text": | ||
print("{0}: {1}".format(event['sender'],event['content']['body'])) | ||
else: | ||
print(event['type']) | ||
|
||
host = "" | ||
username = "" | ||
room = "" | ||
|
||
if len(sys.argv) > 1: | ||
host = sys.argv[1] | ||
else: | ||
host = input("Host (ex: http://localhost:8008 ): ") | ||
|
||
client = MatrixClient(host) | ||
|
||
if len(sys.argv) > 2: | ||
username = sys.argv[2] | ||
else: | ||
username = input("Username: ") | ||
|
||
password = getpass() #Hide user input | ||
|
||
try: | ||
client.login_with_password(username,password) | ||
except MatrixRequestError as e: | ||
print(e) | ||
if e.code == 403: | ||
print("Bad username or password.") | ||
sys.exit(4) | ||
else: | ||
print("Check your sever details are correct.") | ||
sys.exit(3) | ||
|
||
except MissingSchema as e: | ||
print("Bad URL format.") | ||
print(e) | ||
sys.exit(2) | ||
|
||
|
||
room = None | ||
|
||
if len(sys.argv) > 3: | ||
room = sys.argv[3] | ||
else: | ||
room = input("Room ID/Alias: ") | ||
|
||
try: | ||
room = client.join_room(room) | ||
except MatrixRequestError as e: | ||
print(e) | ||
if e.code == 400: | ||
print("Room ID/Alias in the wrong format") | ||
sys.exit(11) | ||
else: | ||
print("Couldn't find room.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth splatting out the entire error here (at least the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And throughout (e.g. |
||
sys.exit(12) | ||
|
||
room.add_listener(on_message) | ||
client.start_listener_thread() | ||
|
||
while True: | ||
msg = input() | ||
if msg == "/quit": | ||
break | ||
else: | ||
room.send_text(msg) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put spaces after
: