GithubHelp home page GithubHelp logo

humans-huddle / pytg Goto Github PK

View Code? Open in Web Editor NEW

This project forked from luckydonald/pytg

0.0 2.0 0.0 280 KB

Python package that wraps around Telegram messenger CLI. Send and receive messages, and more.

License: MIT License

Makefile 0.12% Python 99.88%

pytg's Introduction

PyTg

Version 0.4.5

get it from PyPI Join the chat at https://gitter.im/luckydonald/pytg

A Python package that communicates with the Telegram messenger CLI, to send and receive messages and more.

Telegram is an Whatsapp like Instant messenger, with clients for virtually every device you use.

Works with Python 2.7 and 3

I really recommend to use Python 3, because of it's build in unicode support. Python 2 uses ascii only bytestrings, causing much, much trouble when dealing with characters like öäüß or emojis. (Trust me, I've been there)
~ luckydonald

Install##

Dependencies

Pytg

Install form PyPI on PyPI
pip install pytg

To upgrade append the --upgrade flag.

Install from source

(Beta versions are in the development branch)

  • a) Get the latest pytg code from github.
    git clone https://github.com/luckydonald/pytg.git && cd pytg
  • b) To update already existing code, navigate to the root inside the pytg folder, then git pull
  • Install sudo python setup.py install
    • The dependency "DictObject" should be installed automatically by this. If not, it is available on PyPI:
      sudo pip install DictObject
    • Same goes for "luckydonaldUtils":
      sudo pip install luckydonald-utils

Done.

Usage

Note: The examples files produce syntax errors for python 3.0 - 3.2, the pytg package itself is not affacted by this!
To fix, just remove the u in front of the strings: change u"foobar" to "foobar (see issue #39 and Python 3.3 accepts u'unicode' syntax again).

Start telegram

Create a Telegram Instance. This will manage the CLI process, and registers the Sender and Receiver for you.

from pytg import Telegram
tg = Telegram(
	telegram="/path/to/tg/bin/telegram-cli",
	pubkey_file="/path/to/tg/tg-server.pub")
receiver = tg.receiver
sender = tg.sender

If you don't want pytg to start the cli for you, start it yourself with --json -P 4458 (port 4458). You can then use the Receiver and/or the Sender like this:

from pytg.sender import Sender
from pytg.receiver import Receiver
receiver = Receiver(host="localhost", port=4458)
sender = Sender(host="localhost", port=4458)

Send a message

sender.send_msg("username", "Hello World!")
# Easy huh?

Receiving messages

You need a function as main loop.

@coroutine # from pytg.utils import coroutine
def main_loop():
	while not QUIT:
		msg = (yield) # it waits until it got a message, stored now in msg.
		print("Message: ", msg.text)
		# do more stuff here!
	#
#

Last step is to register that function:

# start the Receiver, so we can get messages!
receiver.start()

# let "main_loop" get new message events.
# You can supply arguments here, like main_loop(foo, bar).
receiver.message(main_loop())
# now it will call the main_loop function and yield the new messages.

That's the basics. Have a look into the examples folder. For starters, I recommend:

  • dump.py - is usefull to see, how the messages look like.
  • ping.py - is usefull to see how to interact with pytg, send messages etc.

Documentation

The Sender object features a rich build-in help, inside the python interpreter type:

from pytg.sender import Sender
help(Sender)  # list all commands
help(Sender.get_self)  # get help for a specific command

Or you can have a look into DOCUMENTATION.md

Contribute

You can help!

Note: There is a version in the making, supporting the cli via socket (as before), the CLI via its build in python (aka. tgl) and brand new, the Telegram bot api as well. Receiving messages is already possible with all three (even simultaneously). Also it features neat classes for everything. Currently I lack the time to continue that.
See the develop branch for that. Maybe you can help make that happen.

URL Changes or "How to update?"##

Here is how to update your local git clone to this url. If you have not used pytg before, just skip to the Install part.

# navigate into the clone
cd pytg	 # not pytg/pytg!
# change to the new url
git remote set-url origin https://github.com/luckydonald/pytg.git
# download the changes
git pull
# don't forget to install the newest official cli: https://github.com/vysheng/tg

If that failes at some point, just Install it from scratch.

Look at the examples

See some example scripts to start with. They are in the examples folder

  • dump.py * is usefull to see, how the messages look like.
  • ping.py * is usefull to see how to interact with pytg, send messages etc.
  • dialog_list.py * shows you how to interact with the CLI and function returning stuff.

Thanks!

pytg's People

Contributors

luckydonald avatar efaisal avatar juanpotato avatar spikeekips avatar justingood avatar nekmo avatar juanprq avatar vhdmsm avatar

Watchers

James Cloos avatar Ali Fisher avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.