Telegram bot stops working after couple of months/weeks - telegram-bot

My Telegram bot has php backend. I've set the hook based on the official guide. Many times the bot stops responding and I realize Telegram server doesn't trigger assigned web-hook.
When I delete that bot and make a new bot with the same ID, problem remains unless I create a bot with different ID.
Has anyone experienced such problem?

So I got it now. First of all, it's not custom certificate. It's because it had took more than 60 seconds for your server to reply to Telegram servers.
you have previous messages stacked on Telegram servers. So you have to let them go by deleteWebHook and then setWebook again.
After that, copy this code to the top of your code:
<?php
set_time_limit(0);
ignore_user_abort(true);
$out = json_encode([
'method'=>'sendMessage',
'chat_id'=>$my_chat_id,
'text'=> "Starting process..."
]);
echo $out;
header('Connection: close');
header('Content-Length: '.strlen($out));
header("Content-type:application/json");
flush();
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
}
It replies to Telegram servers so they stop waiting. Taken from here.

Related

Telegram Bot - Send Info message

currently I am working to creating telegram bot. Now I required to know is there is any API to send info messages (like the one we get during a user is added r deleted in a group chat)
Thanks in advance
What you want to do can be done using answerCallbackQuery method.
But first of all you have to create a CallbackQuery using InlineKeyboardMarkup to create inline keyboards you can follow the steps in this link.
After creating the callback query you have to answer it using one of the codes below:
if (update.CallbackQuery.Data == "CQ1")
{
await Bot.AnswerCallbackQueryAsync(update.CallbackQuery.Id,"Text",true,null, 0);
}
This way the message will look like a message box and will disappear after the user taps on OK. But if you use the code below, the message will show up and disappear automatically after a few seconds.
else if (update.CallbackQuery.Data == "CQ2")
{
await Bot.AnswerCallbackQueryAsync(update.CallbackQuery.Id, "Text", false,null, 30);
}
By the way, messages that are shown at times like adding a user to a
group are service messages and theses kinds of message can only be
sent by telegram server.
If my suggestion does not fix your issue you can use pinMessage method
that sticks a single message to the top of the page in groups and
channels. But note that you can only pin one message to a channel
or group and for pinning another message firstly you should unpin the
previous one.
Currently there is no way to send such info messages.
And in my opinion, this feature is unlikely to be added in future because:
info messages usually tell you information about your chat; they are managed by telegram servers
therefore they should not be sent by Users
bot is an instance of User

Is it possible to read bot Telegram messages

I have successfully created a bot with and am able to fetch messages from a chat using the getupdates method (long polling).
The getUpdates method is only showing user posted messages (clientside). When I post messages directly using the sendmessage method (serverside) these messages do appear in the chat, but do not in the getUpdates log.
This page https://github.com/LibreLabUCM/teleg-api-bot/wiki/Getting-started-with-the-Telegram-Bot-API#getupdates
states it logs only when "An user messages your bot, either directly or in a group." and some other ways, but the sendMessage way is not mentioned.
I've read a bit on the setwebhook method (push) but am not sure this will fix my issue.
Is this possible?
According to Bot FAQ, bots will not be able to see messages from other bots regardless of mode.
The getUpdates method shows only updates from users, not from the bot itself. This means that when you fetch the new messages with the getUpdates method, the Telegram API will list only the messages sent by the users, not the messages sent by the bot via any method (e.g sendMessage, sendPhoto ...).
To get old messages you can store the entire update (or only the parts of the update you need) for each message (even those sent by the bot with the sendMessage method) in a file or in a database and when you need an old message you can simply fetch it form the database or the files.
I managed to get the bot messages using two bots.
One does the sendMessage method and the other one does the getUpdates method.
#Giolacca9 answer inspired me to try this workaround and it works, "not from the bot itself" :)

How to integrate slack with IronWorker tasks to get its status

I would like to get the notification about the status of the IronWorker task after its finished.
I tried to setup and incoming-webhook, but could not find any way to achieve this.
Update
I know how to setup incoming webhook in slack. I am finding a way to trigger this webhook by IronWorker after its completed. I just don't want to integrate the request code in my worker code.
Any help would be appreciated.
IronWorkers allow you to configure a UDP log feed. They tend to send logs to papertrailapp over this UDP feed. If you have ELK stack then try pointing to that. Most log aggregation frameworks have a detect and notify feature built in. So logentries or papertrail or ELK could then look for a log statement from your worker like DONE and notify you in email/slack/text etc.
If your worker has reached the end of its business logic safely then perhaps it is safe to assume that it can also send a REST request to slack on its own saying i'm done! And that such an action wouldn't be an extra burden or cause any additional failures ... try & see ... then share!
(a) you could queue a notification task in a "notification worker" queue as the last step in your workers ... if you want to reduce the chances of failures or retries caused by the notification code itself.
The current API doesn't show a way to register and receive notifications about worker status from iron.io itself ... it seems only polling based: http://dev.iron.io/worker/reference/api/
So you want to set up incoming webhook in slack. And you want to trigger them when the task is complete.
After registering the incoming webhook in slack, you will get the Webhook URL. Its of the form - https://hooks.slack.com/services/SECRET/SECRET
Now we have to make a post request to this url along with the data.
import requests
import json
url = 'https://hooks.slack.com/services/SECRET/'
payload = {'text': 'Random test',"username": "A slack bot","icon_url": "https://slack.com/img/icons/app-57.png","channel": "#abhinav_rai"}
r = requests.post(url, data=json.dumps(payload))
print r.text
print r.status_code
The Following is the python code to make request to the webhook url. This will post your data in the desired channel.
For more information: Visit https://api.slack.com/incoming-webhooks or comment below.

Facebook Messenger Delivery callback randomly missing

My bot can send a response in multiple messages. I use the delivery callback to know that a message is delivered and the next can be sent. (https://developers.facebook.com/docs/messenger-platform/webhook-reference#message_delivery)
It works great except that sometimes I didn't receive Facebook's callback. I see no exact schema, for the exact same conversation it can works perfectly or miss a callback.
Have you ever noticed this problem ?
Opened a bug on Facebook plateform, answer is 'well do not rely on this delivery callback as it works mysteriously'
Facebook ticket
Update
The day after I closed this bug, new callbacks have been available on Messenger Platform, and the echo callback works exactly as intended.
Documentation of Echo callback here

Laravel Api response

I am new to Laravel and Api development, i am facing a problem, the workflow of my api is, a user sends post data to api, then api takes that data and processes the data to databases, now there is a process in which php waits for 30 min. while inserting data into two different tables.
The problem is as far as i know after that process is complete then only i can send json response back to user. but this way user has to wait for 30 minute.
Is there a way that process that takes 30 min do work in background and send the response json immediately when that process started ?
1) I studied about queues but the web server i will be hosting will not give me access to server as a whole to install something, it will only give me space for my files.
I am confused how to achieve this functionality, so that user do not have to wait much for Response.
I will really appreciate.
Thanks,
You can use the queue without any server installation. All your configuration goes in the config/queue.php file.
You can use
Amazon SQS: aws/aws-sdk-php ~3.0
Beanstalkd: pda/pheanstalk ~3.0
Redis: predis/predis ~1.0
Read more here: https://laravel.com/docs/5.2/queues#introduction