Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 设置没有服务器的电报机器人_Json_Webhooks_Telegram Bot - Fatal编程技术网

Json 设置没有服务器的电报机器人

Json 设置没有服务器的电报机器人,json,webhooks,telegram-bot,Json,Webhooks,Telegram Bot,我对网络技术不太熟悉,我想知道是否有一种方法——一种想法是使用这种方法——在不设置服务器的情况下制作一个简单的东西(比如只要在有人发送消息时一遍又一遍地重复相同的消息) 我想可能没有办法解决这个问题,因为我需要解析JSON对象来获取聊天id以便能够发送消息。。。但我希望这里有人知道一个方法 e、 g https://api.telegram.org/bot/setWebHook?url=https://api.telegram.org/bot/sendMessage?text=Hello%26c

我对网络技术不太熟悉,我想知道是否有一种方法——一种想法是使用这种方法——在不设置服务器的情况下制作一个简单的东西(比如只要在有人发送消息时一遍又一遍地重复相同的消息)

我想可能没有办法解决这个问题,因为我需要解析JSON对象来获取聊天id以便能够发送消息。。。但我希望这里有人知道一个方法

e、 g

https://api.telegram.org/bot/setWebHook?url=https://api.telegram.org/bot/sendMessage?text=Hello%26chat_id=

我用一个硬编码的聊天id对它进行了测试,它可以工作。。。但是当然,它总是只向同一个聊天室发送消息,不管它在哪里收到消息。

这确实很有趣,但您肯定需要一个服务器来解析JSON值并从中获取聊天id。

这里是一个非常简单的Python机器人示例,您可以在PC上运行它,无需服务器

import requests
import json
from time import sleep

# This will mark the last update we've checked
last_update = 0
# Here, insert the token BotFather gave you for your bot.
token = 'YOUR_TOKEN_HERE'
# This is the url for communicating with your bot
url = 'https://api.telegram.org/bot%s/' % token

# We want to keep checking for updates. So this must be a never ending loop
while True:
    # My chat is up and running, I need to maintain it! Get me all chat updates
    get_updates = json.loads(requests.get(url + 'getUpdates').content)
    # Ok, I've got 'em. Let's iterate through each one
    for update in get_updates['result']:
        # First make sure I haven't read this update yet
        if last_update < update['update_id']:
            last_update = update['update_id']
            # I've got a new update. Let's see what it is.
            if 'message' in update:
                # It's a message! Let's send it back :D
                requests.get(url + 'sendMessage', params=dict(chat_id=update['message']['chat']['id'], text=update['message']['text']))
    # Let's wait a few seconds for new updates
    sleep(3)
导入请求
导入json
从时间上导入睡眠
#这将标记我们检查的最后一次更新
上次更新=0
#在这里,插入BotFather为您的机器人提供的令牌。
令牌='您的令牌\u此处'
#这是与您的机器人通信的url
url='1〕https://api.telegram.org/bot%s/“%token”
#我们想继续检查更新。所以这一定是一个永无止境的循环
尽管如此:
#我的聊天已启动并运行,我需要维护它!给我所有的聊天更新
get_updates=json.load(requests.get(url+'getUpdates').content)
#好的,我已经准备好了。让我们逐一重复一遍
对于get_更新['result']中的更新:
#首先,确保我还没有阅读此更新
如果上次更新


您看过OpenShift吗?三个小的“齿轮”是免费的,使它很容易与服务器实验。。。感谢你的介绍,我决定在谷歌应用程序引擎上安装一个服务器,目前运行良好。我知道这很愚蠢,但这是我运行的两个机器人。没有SSL认证的情况下,是否有任何选择?
import requests
import json
from time import sleep

# This will mark the last update we've checked
last_update = 0
# Here, insert the token BotFather gave you for your bot.
token = 'YOUR_TOKEN_HERE'
# This is the url for communicating with your bot
url = 'https://api.telegram.org/bot%s/' % token

# We want to keep checking for updates. So this must be a never ending loop
while True:
    # My chat is up and running, I need to maintain it! Get me all chat updates
    get_updates = json.loads(requests.get(url + 'getUpdates').content)
    # Ok, I've got 'em. Let's iterate through each one
    for update in get_updates['result']:
        # First make sure I haven't read this update yet
        if last_update < update['update_id']:
            last_update = update['update_id']
            # I've got a new update. Let's see what it is.
            if 'message' in update:
                # It's a message! Let's send it back :D
                requests.get(url + 'sendMessage', params=dict(chat_id=update['message']['chat']['id'], text=update['message']['text']))
    # Let's wait a few seconds for new updates
    sleep(3)