Sending messages with Telegram - APIs or CLI?

前端 未结 6 1668
-上瘾入骨i
-上瘾入骨i 2020-12-12 17:31

I would like to be able to send a message to a group chat in Telegram. I want to run a python script (which makes some operations that already works) and then, if some param

相关标签:
6条回答
  • 2020-12-12 18:02

    First create a bash script for telegram called tg.sh:

    #!/bin/bash
    now=$(date)
    to=$1
    subject=$2
    body=$3
    tgpath=/home/youruser/tg
    LOGFILE="/home/youruser/tg.log"
    cd ${tgpath}
    ${tgpath}/telegram -k ${tgpath}/tg-server.pub -W <<EOF
    msg $to $subject
    safe_quit
    EOF
    echo "$now Recipient=$to Message=$subject" >> ${LOGFILE}
    echo "Finished" >> ${LOGFILE}
    

    Then put the script in the same folder than your python script, and give it +x permission with chmod +x tg.sh

    And finally from python, you can do:

    import subprocess
    subprocess.call(["./tg.sh", "user#****", "message here"])
    
    0 讨论(0)
  • 2020-12-12 18:05

    Telegram recently released their new Bot API which makes sending/receiving messages trivial. I suggest you also take a look at that and see if it fits your needs, it beats wrapping the client library or integrating with their MTProto API.

    import urllib
    import urllib2
    
    # Generate a bot ID here: https://core.telegram.org/bots#botfather
    bot_id = "{YOUR_BOT_ID}"
    
    # Request latest messages
    result = urllib2.urlopen("https://api.telegram.org/bot" + bot_id + "/getUpdates").read()
    print result
    
    # Send a message to a chat room (chat room ID retrieved from getUpdates)
    result = urllib2.urlopen("https://api.telegram.org/bot" + bot_id + "/sendMessage", urllib.urlencode({ "chat_id": 0, "text": 'my message' })).read()
    print result
    

    Unfortunately I haven't seen any Python libraries you can interact directly with, but here is a NodeJS equivalent I worked on for reference.

    0 讨论(0)
  • 2020-12-12 18:05

    Since version 1.05 you can use the -P option to accept messages from a socket, which is a third option to solve your problem. Sorry that it is not really the answer to your question, but I am not able to comment your question because I do not have enough reputation.

    0 讨论(0)
  • 2020-12-12 18:06

    I'm working with pytg which could be found here: A Python package that wraps around Telegram messenger CLI

    it works pretty good. I already have a python bot based on that project

    0 讨论(0)
  • 2020-12-12 18:08

    I would recommend the first option.

    Once you are comfortable with generating an AuthKey, you should start to get a handle on the documentation.

    To help, I have written a detailed step-by step guide of how I wrote the AuthKey generation code from scratch here.

    It's in vb.net, but the steps should help you do same in python.

    0 讨论(0)
  • 2020-12-12 18:13

    You can use safe_quit to terminate the connection instead since it waits until everything is done before closing the connection and termination the application

    #!/bin/bash
    cd /home/username/tg
    echo "msg user#******** messagehere\nsafe_quit\n" | ./telegram
    

    use this as a simple script and call it from python code as the other answer suggested.

    0 讨论(0)
提交回复
热议问题