通过电报发送输出打印

时间:2018-07-17 09:12:08

标签: python telegram

我对通过电报机器人发送消息有疑问。

这是代码:

from ogn.client import AprsClient
from ogn.parser import parse, ParseError
import csv
import requests

def inRange_square(s, minLat, maxLat, minLon, maxLon, minAlt, maxAlt):
    if s.get('longitude', 0) < minLon or s['longitude'] > maxLon or s['latitude'] < minLat or s['latitude']>maxLat or s['altitude']<minAlt or s['altitude']>maxAlt :

        return False

    return True

def bot_sendtext(bot_message):

    ### Send text message
    bot_token = '698800548:AAH2GZl5cEYc1u1J-nn0izfBXJZm8nv-4uk'
    bot_chatID = '268013558'
    send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message

    requests.get(send_text)


if __name__=="__main__":
    mydict = {}
    reader = csv.reader(open("aerei.csv", "r"))
    for rows in reader:
        k = rows[0].strip('"\'')
        v = rows[1].strip('"\'')
        mydict[k] = v

def processPlane(plane):
    if(inRange_square(plane, 46.1368, 46.1796, 8.8374, 8.9057, 210, 1000)):

        print("The plane is in range")
        ground_speed = round(plane.get('ground_speed', 0.0), 0)
        altitude = round(plane.get('altitude', 0.0), 0)
        print(mydict.get(plane.get('name')[3:]), (f"{ground_speed}"), (f"{altitude}"))
        bot_sendtext ( 'prova' )
    else:

        'odd'

def process_beacon(raw_message):
   try:
       beacon = parse(raw_message)
       #print('Received  {raw_message}'.format(**beacon))
       processPlane(beacon)
   except ParseError as e:
       #print('Error, {}'.format(e.message))
       print( raw_message )


client = AprsClient(aprs_user='N0CALL')
client.connect()

try:
    client.run(callback=process_beacon, autoreconnect=True)
except KeyboardInterrupt:
    print('\nStop ogn gateway')
    client.disconnect()

主要代码:

print(mydict.get(plane.get('name')[3:]), (f"{ground_speed}"), (f"{altitude}"))

程序将输出如下所示的值:

HBPEY 13.0 365.0

我不能做的就是通过电报将上面的输出作为下面的代码

bot_sendtext ( 'output_above' )

希望结果 电报漫游器打印:

HBPEY 13.0 365.0

你能告诉我我该怎么做吗? 非常感谢您的帮助 G

1 个答案:

答案 0 :(得分:0)

要以字符串形式输出打印输出,可以对其进行格式化:

message = f"{mydict[plane['name'][3:]]} {ground_speed} {altitude}"

然后:

bot_sendtext(message)

编辑:如果在字符串格式化之前完成了值查找,则调试起来会更容易(更容易阅读):

plane_name = mydict[plane['name'][3:]]

然后:

message = f"{plane_name} {ground_speed} {altitude}"